Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fraction.c : This file should contain the implementations of all the functions s

ID: 3555073 • Letter: F

Question

Fraction.c: This file should contain the implementations of all the functions specified in Fraction.h. You are to supply any missing implementations.

#include
#include // provides the constant INT_MAX
#include // provides abs function and malloc
#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);
   }
   a = abs(a);
   b = abs(b);
   if (a == 0)
      return b;
   if (b == 0)
      return a;
   return gcd(b,a%b);
}

Fraction string_to_fraction(const char *S)
{ // THIS CODE IS INCORRECT - FIX IT!
   Fraction result = {0,1};
   return result;
}

const char * fraction_to_string(Fraction R)
{ // THIS CODE IS INCORRECT - FIX IT!
   const char * answer = malloc(1);
   return answer;
}

int compare_fractions(Fraction L, Fraction R)
{
// THIS CODE IS INCORRECT - FIX IT! return 0;
}

void reduce_fraction(Fraction *R)
{ // THIS CODE IS INCORRECT - FIX IT!

}

Fraction add_fractions(Fraction L, Fraction R)
{ // THIS CODE IS INCORRECT - FIX IT!
   Fraction result = {0,1};
   return result;
}

Fraction.h: Do not modify this file(the one below)!!!! It provides the definition of the type Fraction and the prototypes of the various functions you must implement.

#include

#ifndef __FRACTIONH__
#define __FRACTIONH__

typedef struct fraction {
      int numer;
      int denom;
} Fraction;

Fraction string_to_fraction(const char *S);
// Input string must have the form /, where is
// an integer literal. The second integer must be present even if it is 1
// The Fraction represented by the string may or may not be in reduced form
// However, the Fraction returned by the function must be reduced
// You should use sscanf, which behaves like fscanf but gets its input
// from a string rather than a file

char * fraction_to_string(Fraction R);
// Returns a pointer to a dynamically allocated array
// containing the string representation of the reduced form of R,
// except that, if R.denom is 1, only the numerator is represented
// Remark: the largest positive integer has 10 digits

int compare_fractions(Fraction L, Fraction R);
// Returns -1 if L < R, 0 if L == R, +1 is L > R

void reduce_fraction(Fraction *R);
// A fraction is reduced if its denominator is positive
// and its numerator and denominator have no common factors other than 1

Fraction add_fractions(Fraction L, Fraction R);
// returns L+R in reduced form

#endif

Explanation / Answer

#include
#include // provides the constant INT_MAX
#include // provides abs function and malloc
#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);
   }
   a = abs(a);
   b = abs(b);
   if (a == 0)
      return b;
   if (b == 0)
      return a;
   return gcd(b,a%b);
}

void reduce_fraction(Fraction *R)
{
    int r = gcd(R->numer, R->denom);
    *(R->numer) /= r;
    *(R->denom) /= r;
}


Fraction string_to_fraction(const char *S)
{ // FIXED
   int i;
   int num=0, den=0;
   int ok=0;
   for(int i=0; i<strlen(S); ++i) {
    if(s[i] == '/') {
        for(int j=0; j<i; ++j) {
            num = num*10 + (int)(S[j]-'0');
        }
        for(int j=i+1; j<strlen(S); ++j) {
            den = den*10 + (int)(S[j]-'0');
        }
        ok=1; // to check if we get the /
        break;
    }
  
   }
   Fraction result;
   result.numer = num;
   result.denom = den;
   if(ok==0) den = 1; //if / is not found then denominator is 1

   reduce_fraction(&result);
   return result;
}

const char * fraction_to_string(Fraction R)
{ //FIXED
    reduce_fraction(&R);
   char *num = (char *) malloc(42 * sizeof(char));
   char *den = (char *) malloc(22 * sizeof(char));
   sprintf(num,"%d",R.numer); //include stdlib.h
   sprintf(den,"%d",R.denom); //include stdlib.h

   if(R.denom == 1) return num; //if the denominator is one then only numerator
                                //shown
                              
   strcat(num, "/"); //add a slash
   strcat(num, den); //add the denominator to the numerator/
   return num;
}


int compare_fractions(Fraction L, Fraction R)
{
    Fraction a=L, b=R;
    reduce_fraction(&a);
    reduce_fractions(&b);
  
    if(a.numer == b.numer && a.denom == b.denom) return 0;
    if(a.numer * b.denom < a.denom * b.numer) return -1;
    return 1;
}

Fraction add_fractions(Fraction L, Fraction R)
{ // FIXED
   Fraction result;
   result.denom = L.denom * R.denom;
   result.numer = (L.numer * R.denom) + (R.numer * L.denom);
   reduce_fraction(&result); //reduce the result
   return result;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote