hw0: fixed submission

This commit is contained in:
Claudio Maggioni 2022-09-25 18:06:55 +02:00
parent 407b326e13
commit 6a756894be
5 changed files with 5 additions and 10 deletions

View file

@ -90,6 +90,7 @@ double sum_fraction_array_approx(fraction frac_array[], int n)
{ {
double sum = 0; double sum = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
fraction& frac = frac_array[i];
sum += fraction2double(frac); sum += fraction2double(frac);
} }
return sum; return sum;

Binary file not shown.

View file

@ -64,15 +64,15 @@ static void test_array_functions(int n)
{ {
fraction* a = (fraction*) malloc(sizeof(fraction) * n); fraction* a = (fraction*) malloc(sizeof(fraction) * n);
fill_fraction_array(a, n); fill_fraction_array(a, n);
cout << "Sum of array as fraction: " << endl; cout << "Sum of array as fraction: " << endl;
print_fraction(sum_fraction_array(a, n)); print_fraction(sum_fraction_array(a, n));
cout << "Sum of array as double (approx.): " << endl; cout << "Sum of array as double (approx.): " << endl;
cout << sum_fraction_array_approx(a, n) << endl; cout << sum_fraction_array_approx(a, n) << endl;
// find n for which sum function breaks. Explain what is happening. n=4448, // find n for which sum function breaks. Explain what is happening. n=1290
// this is the first number where the numerator overflows and thus // this is the first number where the denominator overflows and thus
// erroneoulsly becomes a negative number. The overflow is caused by the // erroneoulsly becomes a negative number. The overflow is caused by the
// sum_fractions function, specifically in the multiplication between // sum_fractions function, specifically in the multiplication between
// denominators. The approximation function doesn't overflow since fractions // denominators. The approximation function doesn't overflow since fractions
@ -94,13 +94,7 @@ static void test_toolbox(int argc, char* argv[])
test5(); test5();
cout << "\n========== test_array_functions ========= " << endl; cout << "\n========== test_array_functions ========= " << endl;
#if 1 int n = 1290;
cout << "Input n: " << endl;
int n;
cin >> n;
#else
int n = 5;
#endif
test_array_functions(n); test_array_functions(n);
} }

Binary file not shown.