hw0: done ex1+ex2, make works

This commit is contained in:
Claudio Maggioni 2022-09-20 17:36:08 +02:00
parent a991cb3b15
commit 1e5a5b9148
2 changed files with 9 additions and 14 deletions

View file

@ -20,10 +20,7 @@ void print_fraction_array(fraction frac_array[], int n)
fraction square_fraction(fraction frac) fraction square_fraction(fraction frac)
{ {
struct fraction square = { struct fraction square = { frac.num * frac.num, frac.denom * frac.denom };
.num = frac.num * frac.num;
.denom = frac.denom * frac.denom;
};
return square; return square;
} }
@ -81,16 +78,13 @@ fraction add_fractions(fraction frac1, fraction frac2)
frac2.denom *= fac2; frac2.denom *= fac2;
} }
struct fraction total = { struct fraction result = { frac1.num + frac2.num, g };
.num = frac1.num + frac2.num; return result;
.denom = g;
};
return total;
} }
double sum_fraction_array_approx(fraction frac_array[], int n) double sum_fraction_array_approx(fraction frac_array[], int n)
{ {
struct fraction sum = { .num = 0, .denom = 0 }; struct fraction sum = { 0, 0 };
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
struct fraction frac = frac_array[i]; struct fraction frac = frac_array[i];
sum = add_fractions(sum, frac); sum = add_fractions(sum, frac);
@ -108,8 +102,9 @@ double sum_fraction_array_approx(fraction frac_array[], int n)
void fill_fraction_array(fraction frac_array[], int n) void fill_fraction_array(fraction frac_array[], int n)
{ {
fraction temp_frac; fraction temp_frac;
temp_frac.num = 1; temp_frac.num = 0;
for (int i = 1; i <= n; i++) temp_frac.denom = 1;
for (int i = 0; i <= n; i++)
{ {
temp_frac.denom = i * (i+1); temp_frac.denom = i * (i+1);
frac_array[i-1] = temp_frac; frac_array[i-1] = temp_frac;

View file

@ -3,7 +3,7 @@
struct fraction { struct fraction {
int num; int num;
int denom; int denom;
} };
void print_fraction(fraction frac); void print_fraction(fraction frac);
@ -19,7 +19,7 @@ int gcd(int a, int b);
int gcd(fraction frac); int gcd(fraction frac);
//TODO: add function declaration for function 7 void reduce_fraction_inplace(fraction& frac);
fraction add_fractions(fraction frac1, fraction frac2); fraction add_fractions(fraction frac1, fraction frac2);