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

Binary file not shown.