hw0: EX2 done
This commit is contained in:
parent
d3ed218ee2
commit
845727edb7
2 changed files with 66 additions and 19 deletions
|
@ -20,46 +20,90 @@ void print_fraction_array(fraction frac_array[], int n)
|
||||||
|
|
||||||
fraction square_fraction(fraction frac)
|
fraction square_fraction(fraction frac)
|
||||||
{
|
{
|
||||||
//TODO: implement function 2
|
struct fraction square = {
|
||||||
|
.num = frac.num * frac.num;
|
||||||
|
.denom = frac.denom * frac.denom;
|
||||||
|
};
|
||||||
|
return square;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: implement function 3
|
void square_fraction_inplace(fraction& frac)
|
||||||
|
{
|
||||||
|
frac.num *= frac.num;
|
||||||
|
frac.denom *= frac.denom;
|
||||||
|
}
|
||||||
|
|
||||||
double fraction2double(fraction frac)
|
double fraction2double(fraction frac)
|
||||||
{
|
{
|
||||||
//TODO: implement function 4
|
return (double) frac.num / (double) frac.denom;
|
||||||
}
|
}
|
||||||
|
|
||||||
int gcd(int a, int b)
|
int gcd(int a, int b)
|
||||||
{
|
{
|
||||||
//TODO: implement function 5
|
return b == 0 ? a : gcd(b, a % b);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: implement function 6
|
int gcd(fraction frac) {
|
||||||
|
int a = frac.num, b = frac.denom;
|
||||||
|
while (b != 0) {
|
||||||
|
int t = b;
|
||||||
|
b = a % b;
|
||||||
|
a = t;
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
void reduce_fraction_inplace(fraction & frac)
|
void reduce_fraction_inplace(fraction & frac)
|
||||||
{
|
{
|
||||||
//TODO: implement function 7
|
// the called function is the one implementing the iterative algorithm
|
||||||
|
// because gcd is called with one argument of type fraction (the reference
|
||||||
|
// is implicitly copied and passed by value), matching the signature of the
|
||||||
|
// iterative implementation
|
||||||
|
int g = gcd(frac);
|
||||||
|
|
||||||
//TODO: add short comment to explain which of the gcd() functions your code is calling
|
frac.num /= g;
|
||||||
|
frac.denom /= g;
|
||||||
}
|
}
|
||||||
|
|
||||||
fraction add_fractions(fraction frac1, fraction frac2)
|
fraction add_fractions(fraction frac1, fraction frac2)
|
||||||
{
|
{
|
||||||
//TODO: implement function 8
|
int g = gcd(frac1.denom, frac2.denom);
|
||||||
|
|
||||||
|
{
|
||||||
|
int fac1 = frac1.denom / g;
|
||||||
|
frac1.num *= fac1;
|
||||||
|
frac1.denom *= fac1;
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
int fac2 = frac2.denom / g;
|
||||||
|
frac2.num *= fac2;
|
||||||
|
frac2.denom *= fac2;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct fraction total = {
|
||||||
|
.num = frac1.num + frac2.num;
|
||||||
|
.denom = g;
|
||||||
|
};
|
||||||
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
double sum_fraction_array_approx(fraction frac_array[], int n)
|
double sum_fraction_array_approx(fraction frac_array[], int n)
|
||||||
{
|
{
|
||||||
//TODO: implement function 9
|
struct fraction sum = { .num = 0, .denom = 0 };
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
//TODO: add short comment to explain why this function is approximate
|
struct fraction frac = frac_array[i];
|
||||||
|
sum = add_fractions(sum, frac);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: implement function 10
|
return fraction2double(sum);
|
||||||
|
|
||||||
|
// the approximation in this function is given by the fact that floating
|
||||||
|
// point numbers cannot represent precisely rational numbers due to the
|
||||||
|
// fixed size of significant digits they can hold in the mantissa. This
|
||||||
|
// implementation minimizes the approximation by computing the sum into a
|
||||||
|
// fraction FIRST before performing the double conversion.
|
||||||
|
}
|
||||||
|
|
||||||
void fill_fraction_array(fraction frac_array[], int n)
|
void fill_fraction_array(fraction frac_array[], int n)
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
//TODO: implement fraction datatype
|
struct fraction {
|
||||||
|
int num;
|
||||||
|
int denom;
|
||||||
|
}
|
||||||
|
|
||||||
void print_fraction(fraction frac);
|
void print_fraction(fraction frac);
|
||||||
|
|
||||||
|
@ -8,7 +11,7 @@ void print_fraction_array(fraction frac_array[], int n);
|
||||||
|
|
||||||
fraction square_fraction(fraction frac);
|
fraction square_fraction(fraction frac);
|
||||||
|
|
||||||
//TODO: add function declaration for function 3
|
void square_fraction_inplace(fraction& frac);
|
||||||
|
|
||||||
double fraction2double(fraction frac);
|
double fraction2double(fraction frac);
|
||||||
|
|
||||||
|
|
Reference in a new issue