From 1e5a5b9148d0831e15b83b722a5f6a4e0c204596 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 20 Sep 2022 17:36:08 +0200 Subject: [PATCH] hw0: done ex1+ex2, make works --- .../src/fraction_summing/fraction_toolbox.cpp | 19 +++++++------------ .../src/fraction_summing/fraction_toolbox.hpp | 4 ++-- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/Project0/src/fraction_summing/fraction_toolbox.cpp b/Project0/src/fraction_summing/fraction_toolbox.cpp index d1a3324..d3a29ca 100644 --- a/Project0/src/fraction_summing/fraction_toolbox.cpp +++ b/Project0/src/fraction_summing/fraction_toolbox.cpp @@ -20,10 +20,7 @@ void print_fraction_array(fraction frac_array[], int n) fraction square_fraction(fraction frac) { - struct fraction square = { - .num = frac.num * frac.num; - .denom = frac.denom * frac.denom; - }; + struct fraction square = { frac.num * frac.num, frac.denom * frac.denom }; return square; } @@ -81,16 +78,13 @@ fraction add_fractions(fraction frac1, fraction frac2) frac2.denom *= fac2; } - struct fraction total = { - .num = frac1.num + frac2.num; - .denom = g; - }; - return total; + struct fraction result = { frac1.num + frac2.num, g }; + return result; } 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++) { struct fraction frac = frac_array[i]; 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) { fraction temp_frac; - temp_frac.num = 1; - for (int i = 1; i <= n; i++) + temp_frac.num = 0; + temp_frac.denom = 1; + for (int i = 0; i <= n; i++) { temp_frac.denom = i * (i+1); frac_array[i-1] = temp_frac; diff --git a/Project0/src/fraction_summing/fraction_toolbox.hpp b/Project0/src/fraction_summing/fraction_toolbox.hpp index 9d3b205..3fa79bd 100644 --- a/Project0/src/fraction_summing/fraction_toolbox.hpp +++ b/Project0/src/fraction_summing/fraction_toolbox.hpp @@ -3,7 +3,7 @@ struct fraction { int num; int denom; -} +}; void print_fraction(fraction frac); @@ -19,7 +19,7 @@ int gcd(int a, int b); int gcd(fraction frac); -//TODO: add function declaration for function 7 +void reduce_fraction_inplace(fraction& frac); fraction add_fractions(fraction frac1, fraction frac2);