diff --git a/Project0/src/fraction_summing/fraction_toolbox.cpp b/Project0/src/fraction_summing/fraction_toolbox.cpp index d3a29ca..b3963c7 100644 --- a/Project0/src/fraction_summing/fraction_toolbox.cpp +++ b/Project0/src/fraction_summing/fraction_toolbox.cpp @@ -20,7 +20,7 @@ void print_fraction_array(fraction frac_array[], int n) fraction square_fraction(fraction frac) { - struct fraction square = { frac.num * frac.num, frac.denom * frac.denom }; + fraction square = { frac.num * frac.num, frac.denom * frac.denom }; return square; } @@ -64,33 +64,33 @@ void reduce_fraction_inplace(fraction & frac) fraction add_fractions(fraction frac1, fraction frac2) { - int g = gcd(frac1.denom, frac2.denom); + int d = frac1.denom; - { - int fac1 = frac1.denom / g; - frac1.num *= fac1; - frac1.denom *= fac1; - } + frac1.num *= frac2.denom; + frac1.denom *= frac2.denom; - { - int fac2 = frac2.denom / g; - frac2.num *= fac2; - frac2.denom *= fac2; - } + frac2.num *= d; + frac2.denom *= d; - struct fraction result = { frac1.num + frac2.num, g }; + fraction result = { frac1.num + frac2.num, frac1.denom }; + reduce_fraction_inplace(result); return result; } +fraction sum_fraction_array(fraction frac_array[], int n) { + fraction sum = { 0, 1 }; + print_fraction_array(frac_array, n); + for (int i = 0; i < n; i++) { + fraction frac = frac_array[i]; + sum = add_fractions(sum, frac); + print_fraction(sum); + } + return sum; +} + double sum_fraction_array_approx(fraction frac_array[], int n) { - struct fraction sum = { 0, 0 }; - for (int i = 0; i < n; i++) { - struct fraction frac = frac_array[i]; - sum = add_fractions(sum, frac); - } - - return fraction2double(sum); + return fraction2double(sum_fraction_array(frac_array, n)); // the approximation in this function is given by the fact that floating // point numbers cannot represent precisely rational numbers due to the @@ -102,7 +102,7 @@ double sum_fraction_array_approx(fraction frac_array[], int n) void fill_fraction_array(fraction frac_array[], int n) { fraction temp_frac; - temp_frac.num = 0; + temp_frac.num = 1; temp_frac.denom = 1; for (int i = 0; i <= n; i++) { diff --git a/Project0/src/fraction_summing/main.cpp b/Project0/src/fraction_summing/main.cpp index e65eaed..d263207 100644 --- a/Project0/src/fraction_summing/main.cpp +++ b/Project0/src/fraction_summing/main.cpp @@ -26,17 +26,50 @@ static void readcmdline(fraction & frac, int argc, char* argv[]) static void test23467(int argc, char* argv[]) { - //TODO: implement function + fraction frac; + readcmdline(frac, argc, argv); + + cout << "#2: squared fraction" << endl; + print_fraction(square_fraction(frac)); + + cout << "#3: squared fraction in-place" << endl; + fraction frac2 = frac; + square_fraction_inplace(frac2); + print_fraction(frac2); + + cout << "#4: fraction to double" << endl; + cout << fraction2double(frac) << endl; + + cout << "#6: gcd of fraction" << endl; + cout << gcd(frac) << endl; + + cout << "#7: fraction reduction in-place" << endl; + fraction frac3 = frac; + reduce_fraction_inplace(frac3); + print_fraction(frac3); } static void test5() { - //TODO: implement function + int a, b; + cout << "#5: recursive gcd of two integers" << endl; + cout << "Input first number: "; + cin >> a; + cout << "Input second number: "; + cin >> b; + cout << "GCD is: " << gcd(a, b) << endl; } static void test_array_functions(int n) { - //TODO: implement function + 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; //TODO: find n for which sum function breaks. Explain what is happening. } @@ -56,5 +89,5 @@ static void test_toolbox(int argc, char* argv[]) int main(int argc, char* argv[]) { - -} \ No newline at end of file + test_toolbox(argc, argv); +}