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

C Programming: In mathematics, a rational number is defined as a quotient of two

ID: 3837002 • Letter: C

Question

C Programming:

In mathematics, a rational number is defined as a quotient of two integers where the denominator is not equal to zero. For example, 1/2, 1/6, -5/4, 12/3, and 468/(-325) are all rational numbers.

a. Declare a data structure that will contain a rational number.

b. Write a functions that will add, subtract, multiply and divide rational numbers. In all functions, pass in three parameters, each pointing to a data structure of the type you declared in 'part a'. Use two of the parameters for the operands, and the third for the result.

c. Write a function that takes a pointer to your data structure as a parameter and returns the greatest common divisor of the numerator and denominator.

d. Use your function from part c to write a function that will reduce a fraction(rational number) to lowest terms. Pass in a pointer to the fraction and have the fraction modified by the function.

e. Write a input and output functions so that a user can enter a fraction in the form

[ ex: 2/3, -5/6, 4/-5, or -12/-33 ]

and the output is in the same form. Have your input function check for errors in the input and return a value of -1 in case an error was sensed. The error might consist of a zero denominator as in 3/0, or an error in form as in 4a/5 or 4%5. Both the input function and the output function should take a single pointer to your data structure from part as a parameter.

f. Combine your functions from parts b through e to write a program that will perform rational number arithmetic for the user. The user should be allowed to enter any number of problems, and the program should output the answer in lowest terms. A sample dialogue is below. The user's input is in boldface.

FRACTION ARITHMETIC PROGRAM

--------------- ------------------ ----------------

Enter your problem: 2/3 + 1/5

The answer us 13/15. Another problem (y/n)? y

Enter your problem: 4/2 - 5/0

Illegal input!! Another problem (y/n)? y

Enter your problem: 2/9 * 3/4

The answer is 1/6. Another problem (y/n)? n

Goodbye and thank you.

Explanation / Answer

Hi there the question was long for one chegg question, I tried to answer the first four subquestions. Repost the e, f with following code. This code has the answers for a, b, c, d. If you have any problems understanding please comment I'll reply.

#include <stdio.h>
#include <stdlib.h>

struct RationalNumber
{
   int numerator;
   int denominator;
};

void add(
       struct RationalNumber *, struct RationalNumber *,
      struct RationalNumber * );

void sub(
       struct RationalNumber *, struct RationalNumber *,
      struct RationalNumber * );

void mul(
       struct RationalNumber *, struct RationalNumber *,
      struct RationalNumber * );

void divide(
       struct RationalNumber *, struct RationalNumber *,
      struct RationalNumber * );

int gcd(int , int);

void reduce(struct RationalNumber *);

int main()
{
struct RationalNumber r4;
   r4.numerator = 2;
   r4.denominator = 3;

struct RationalNumber r5;
   r5.numerator = -3;
   r5.denominator = 4;

  
struct RationalNumber r6;

   add(&r4, &r5, &r6);

   printf(
          "Value of numerator: %d, denominator: %d." ,          
           r6.numerator,
           r6.denominator );

   int res = gcd(10, 12);
   printf("10 / 12: %d", res);
   return 0;
}

void add(
       struct RationalNumber *r1, struct RationalNumber *r2,
       struct RationalNumber *r3 )
{
   int top, bottom;
   top = (r1->numerator * r2->denominator) +
               (r2->numerator * r1->denominator);
   bottom = r1->denominator * r2->denominator;
   r3->numerator = top;
   r3->denominator = bottom;
}

void sub(
       struct RationalNumber *r1, struct RationalNumber *r2,
       struct RationalNumber *r3 )
{
   int top, bottom;
   top = (r1->numerator * r2->denominator) -
               (r2->numerator * r1->denominator);
   bottom = r1->denominator * r2->denominator;
   r3->numerator = top;
   r3->denominator = bottom;
}

void mul(
       struct RationalNumber *r1, struct RationalNumber *r2,
       struct RationalNumber *r3 )
{
   int top, bottom;
   top = r1->numerator * r2->numerator;
   bottom = r1->denominator * r2->denominator;
   r3->numerator = top;
   r3->denominator = bottom;
}

void divide(
       struct RationalNumber *r1, struct RationalNumber *r2,
       struct RationalNumber *r3 )
{
   int top, bottom;
   top = r1->numerator * r2->denominator;
   bottom = r1->denominator * r2->numerator;
   r3->numerator = top;
   r3->denominator = bottom;
}

int gcd(int a, int b)
{
   a = abs(a);
   b = abs(b);
const int MIN = (a < b) ? a : b ;
   for (int i = MIN; i >= 0; --i)
       if (a % i == 0 && b % i == 0)
           return i;
   return 1;
}

void reduce(struct RationalNumber *r1)
{
   int top, bottom, my_gcd;
   my_gcd = gcd(r1->numerator, r1->denominator);
  
   r1->numerator = r1->numerator / my_gcd;
   r1->denominator = r1->denominator / my_gcd;
}

Cheers,

PH