You are to write a program that requests that the user inputs a fraction of the
ID: 3620817 • Letter: Y
Question
You are to write a program that requests that the user inputs a fraction of the form n/d with d nonzero. Your program then prints out the fraction in reduced form. The program then asks if the user would like to process another fraction and continues to do so as long as the user replies with Y.A fraction n/d is in reduced form provided:
1. If n is 0, then d is 1;
2. d > 0;
3. the greatest common divisor of n and d is 1.
The contents of the incomplete file reduceFraction.c are shown on the next page. You are not to modify the functions gcd and main. Instead, you are to supply the definitions for the functions
void inputFraction(int *numerator, int *denominator);
void reduceFraction(int *numerator, int *denominator);
void printReducedFraction(int numerator, int denominator);
#include <stdio.h>
#include <string.h>
/* put any other #includes you think you may need here*/
void inputFraction(int *numerator, int *denominator); /* Prompts for and inputs a fraction with input verification */
void reduceFraction(int *numerator, int *denominator);
/* Modifies the fraction so that it is in reduced form */
void printReducedFraction(int numerator, int denominator);
/* Reduces the fraction numerator/denominator and prints the result.
If the denominator is 1, it just prints the numerator,
otherwise prints the fraction in the form
numerator/denominator.
Calls reduceFraction.
*/
int gcd(int x, int y)
{
if ( x % y == 0)
return y;
return gcd(y,x%y);
}
int main()
{
int num, denom;
char response;
printf(" This function inputs fractions and prints the equivalent fraction in reduced form. ");
do {
inputFraction(&num, &denom);
printf(" ");
printReducedFraction(num,denom);
/* First clear the buffer, especially of any residual newline! */
while (getchar() != ' ');
printf(" Do you wish to process another fraction (Y for yes, N for no): ");
response = getchar();
printf(" ");
} while (toupper(getOneChar()) = = 'Y');
return 0;
}
/* You must supply the definitions of the functions above main except for gcd */
Your output must match exactly what is shown in the example run below. User input is shown in bold.
This function inputs fractions and prints the equivalent fraction in reduced form.
Enter the numerator of your fraction: 25
Enter the nonzero denominator of your fraction: 15
You entered 25/15
The fraction you entered in reduced form is 5/3
Do you wish to process another fraction (Y for yes, N for no): Y
Enter the numerator of your fraction: 25
Enter the nonzero denominator of your fraction: -15
You entered 25/-15
The fraction you entered in reduced form is -5/3
Do you wish to process another fraction (Y for yes, N for no): Y
Enter the numerator of your fraction: -3
Enter the nonzero denominator of your fraction: -9
You entered -3/-9
The fraction you entered in reduced form is 1/3
Do you wish to process another fraction (Y for yes, N for no): Y
Enter the numerator of your fraction: -25
Enter the nonzero denominator of your fraction: 0
The denominator cannot be zero. Please reenter your denominator: 0
The denominator cannot be zero. Please reenter your denominator: 5
You entered -25/5
The fraction you entered in reduced form is -5
Do you wish to process another fraction (Y for yes, N for no): Y
Enter the numerator of your fraction: 25
Enter the nonzero denominator of your fraction: 5
You entered 25/5
The fraction you entered in reduced form is 5
Do you wish to process another fraction (Y for yes, N for no): N
Explanation / Answer
please rate - thanks I had to move where the definition of gcd was (just moved it so it would be defined where it was used) main had an error it input response then said toupper(getOneChar()) instead of youpper(response) I corrected thatRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.