You are going to implement functions to manipulate Fractions. You are also going
ID: 649846 • Letter: Y
Question
You are going to implement functions to manipulate Fractions. You are also going to thoroughly test your functions with both your own test data. There will be strict rules regarding the format for a string that represents a Fraction.
Output format and addition rules
A fraction a/b is said to be in reduced form if a and b have no common factors and b is positive. The result of adding two fractions must always be in reduced form.
The string representation of a Fraction always uses the reduced form of the Fraction. Moreover, if the denominator is 1, only the numerator should appear in the representation.
These rules do not apply to fraction initializers, only to fraction sums, and to the string representation of a Fraction.
We are given this struct:
typedef struct fraction {
int numer;
int denom;
} Fraction;
Here is the code we must modify:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include "Fraction.h"
int gcd(int a, int b)
{
if (a == 0 && b == 0) {
printf("Illegal args to gcd: %d, %d ",a,b);
exit(1);
}
int aa = abs(a);
int bb = abs(b);
if (aa == 0)
return bb;
if (bb == 0)
return aa;
return gcd(bb,aa%bb);
}
Fraction string_to_fraction(const char *S)
{
Fraction result = {0,1};
/* Place your code here to initialize the Fraction result
as specified in the string S (using sscanf)
*/
return result;
}
void fraction_to_string(Fraction R,char repr[])
{
repr[0] = 0;
/* Place your code here to place the string representation
of the Fraction R in the character array repr using
sprintf.
*/
}
int compare_fractions(Fraction L, Fraction R)
{
/* Place your code here to return 1 if L and R
have the same value as Fractions, 0 if not
*/
}
void reduce_fraction(Fraction *R)
{
/* Place your code here to modify the Fraction being passed
in by reference so that it is reduced according to the
specifications in Instructions.pdf
*/
}
Fraction add_fractions(Fraction L, Fraction R)
{
Fraction result = {0,1};
/* Place your code here to make result a reduced
Fraction representing the sum of the Fractions L and R
*/
return result;
}
Could someone help me with the psudeocode, because I have a small idea of what to do, but I am not 100% sure.
Explanation / Answer
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include "fraction.h"
int gcd(int a, int b)
{
if (a == 0 && b == 0) {
printf("Illegal args to gcd: %d, %d ",a,b);
exit(1);
}
int aa = abs(a);
int bb = abs(b);
if (aa == 0)
return bb;
if (bb == 0)
return aa;
return gcd(bb,aa%bb);
}
Fraction string_to_fraction(const char *S)
{
Fraction result; //= {0,1};
float fl = strtof(S, NULL); // Convert string to float
/* Convert decimal to fraction and assign to result */
return result;
}
void fraction_to_string(Fraction R,char repr[])
{
repr[0] = 0;
float fl = (float)R.numer / R.denom;
}
int compare_fractions(Fraction L, Fraction R)
{
int z = L.numer*R.denom - L.denom*R.numer;
if (z == 0)
return 0;
else if (z > 0)
return 1;
else
return -1;
}
void reduce_fraction(Fraction *R)
{
for (int i = R->denom * R->numer; i > 1; i--)
{
if ((R->denom % i == 0) && (R->numer % i == 0))
{
R->denom /= i;
R->numer /= i;
}
}
}
Fraction add_fractions(Fraction L, Fraction R)
{
Fraction result={(L.numer * R.denom) + (R.numer * L.denom), L.denom * R.denom};
return result;
}
int main()
{
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.