Write a program that asks the user to enter a fraction, and then reduces the fra
ID: 3617082 • Letter: W
Question
Write a program that asks the user to enter a fraction, and then reduces the fractions to its lowest terms. To reduce a fraction to its lowest terms, divide both the numerator and the denominator by their greatest common divisor. Function get_fraction() gets both numerator and denominator from the user. ?? Write a function, get_int() to get a single positive integer from the keyboard. ?? Function get_fraction() should call that function two times. ?? Output prompt before calling get_int(). ?? If an input is not valid, get_int should output an error message and ask the user to try again for that input.?? Be sure your input function works correctly with invalid inputs. ?? Even inputs that are not integers ?? 123.456 (Should return 123) ?? abc (Should output error message and repeat input.) ?? Clear the keyboard input buffer after each call to scanf.
This is what i got sofar #include <stdio.h>
/* * Asks the user to enter afraction, and then reduces the * fractions to its lowestterms. * Returns the integer results tothe caller. */ int get_fraction (original_num,original_denom);
if (original_num % original_denom ==0) { returnoriginal_denom; } returngcd(original_denom, original_num % original_denom);
/* Get a postive integer from thekeyboard. */ int get_int() { int original_num =0; while (original_num< 1) { scanf("%d", &original_num); if(original_num < 1) { printf ("Please enter an integer greater than0 "); while (getchar() != ' '); /* Clear keyboard inputbuffer.*/ } } returnoriginal_num; }
int main()
{ int original_num =0; int original_denom =0; int reduced_num =0; int reduced_denom =0;
printf ("This programreduces a fraction that you enter "); printf ("to its lowestterms ");
get_fraction(&original_num,&original_denom); reduce(original_num,original_denom, &reduced_num, &reduced_denom); printf ("%d/%d reduced toits lowest terms is %d/%d ",original_num, original_denom,reduced_num, reduced_denom);
return 0; }
gcd= get_fraction(original_num, original_denom); so i need to divide original_num and original_denom by gcd and return reduced_num and reduced_denom
I cant change the int main () function. I dont know if the get_int and get_function is right
This is what i got sofar #include <stdio.h>
/* * Asks the user to enter afraction, and then reduces the * fractions to its lowestterms. * Returns the integer results tothe caller. */ int get_fraction (original_num,original_denom);
if (original_num % original_denom ==0) { returnoriginal_denom; } returngcd(original_denom, original_num % original_denom);
/* Get a postive integer from thekeyboard. */ int get_int() { int original_num =0; while (original_num< 1) { scanf("%d", &original_num); if(original_num < 1) { printf ("Please enter an integer greater than0 "); while (getchar() != ' '); /* Clear keyboard inputbuffer.*/ } } returnoriginal_num; }
int main()
{ int original_num =0; int original_denom =0; int reduced_num =0; int reduced_denom =0;
printf ("This programreduces a fraction that you enter "); printf ("to its lowestterms ");
get_fraction(&original_num,&original_denom); reduce(original_num,original_denom, &reduced_num, &reduced_denom); printf ("%d/%d reduced toits lowest terms is %d/%d ",original_num, original_denom,reduced_num, reduced_denom);
return 0; }
gcd= get_fraction(original_num, original_denom); so i need to divide original_num and original_denom by gcd and return reduced_num and reduced_denom
I cant change the int main () function. I dont know if the get_int and get_function is right How do I finish the it ?
Explanation / Answer
please rate - thanks try these the functions should have been void, and I moved them #include /* * Asks the user to enter a fraction, and then reduces the * fractions to its lowest terms. * Returns the integer results to the caller. */ void get_fraction (int *original_num, int *original_denom); int get_int(); /* Get a postive integer from the keyboard. */ void reduce(int a, int b,int *newa,int *newb) ; /*Euclids algorithm */ int main() { int original_num = 0; int original_denom = 0; int reduced_num = 0; int reduced_denom = 0; printf ("This program reduces a fraction that youenter "); printf ("to its lowest terms "); get_fraction(&original_num,&original_denom); reduce(original_num, original_denom,&reduced_num, &reduced_denom); printf ("%d/%d reduced to its lowest terms is %d/%d ",original_num, original_denom, reduced_num, reduced_denom); return 0; } void get_fraction (int *original_num, int *original_denom) { printf("Please enter a fraction as two positiveintegers "); printf("Numerator: "); *original_num=get_int(); printf("Denominator: "); *original_denom=get_int(); } int get_int() { int original_num = 0; char ch; scanf ("%c", &ch); while (ch!=' ') { if(ch'9') { printf ("Pleaseenter an integer greater than 0 "); while (getchar()!= ' '); /* Clear keyboard input buffer.*/ original_num=0; } else original_num=original_num*10+(ch-48); scanf ("%c", &ch); } return original_num; } /* Get a postive integer from the keyboard. */ void reduce(int a, int b,int *newa,int*newb) /*Euclids algorithm */ { *newa=a; *newb=b; while(a!=b) { if(a>b) a -= b; else b -= a; } *newa=*newa / a; *newb=*newb / a; }Related 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.