Comp Sci C+ Programming Homework Help (Not C++)

Here are the homework instructions:

Write a program that will accept two integers representing the numerator and denominator of a fraction, and display:

1. The fraction in "lowest form" with a sign only in the numerator.
2. The fraction is signed decimal form.

Write your program with three functions in addition to main(): one to print the fraction in fraction form, one to print it in decimal form, and one to find the greatest common denominator (gcd) to allow you to reduce the fraction to lowest form. (You will want to search the Internet for Euclid's algorithm for this.)

Here is what I have coded so far:

double decimal(int, int); // function prototype of fraction's decimal form
int gcd(int, int); // function prototype for greatest common denominator

# include <stdio.h>

int main()
{
int n; // represents numerator
int d; // represents denominator
int fraction; // represents numerator & denominator as a fraction

printf("Hello, this program is designed to accept two integers \n"
"representing the numerator and denominator of a fraction. \n"
"It is also designed to display the decimal form, \n"
"GCD, and the fractional form of the fraction! \n\n");

printf("What is the integer value of your numerator? ");
scanf("%d", &n);
printf("What is the integer value of your denominator? ");
scanf("%d", &d);

printf("\nThe GCD of your fraction is: %d ", gcd(n, d));
printf("\nThe decimal form of your fraction is: %.6lf \n", decimal(n, d));

printf("\nProgram Author 1: Vladimir S. Lolinco\n");
printf("Program Author 2: Glenda Jacobs\n\n");

system("pause");
return 0;

}

double decimal(int n, int d)
{
return (double)n/d;
}

int gcd(int n, int d)
{
if (d == 0)
return n;
else
return gcd(d, n % d);
}

My problems/questions:

1) How do I go about enforcing a positive or negative sign for the decimal form of my fraction?

2) How do I go about writing a function that prints the numerator and denominator as an actual fraction without doing any math and also attaching either a plus or minus sign next to the numerator?

3) I need to make this loop and I have tried using SENTINEL values or EOF (aka Ctrl+Z) and it still does not work - how would I make it loop so that when the user enters Ctrl+Z for either the numerator or denominator that the program exits out and the loop terminates?

Thanks.

Related Engineering and Comp Sci Homework Help News on Phys.org

mgb_phys

Homework Helper
I assume you mean 'c' there is no such language as 'c+'

To output a +/- simply put a + in the printf format string ie. %+f
See the printf man page http://linux.die.net/man/3/printf

To print a fraction you would simply store the numerator and denominator as two integer values and print them like: printf(" %u/%u ",num,denom); // use %+ to print a +/-

3, scanf returns the number of arguments matched so you could do
if ( 0==scanf("%d", &n) ) {
exit(1);
}
and simply enter a non number+return when prompted for the number.

Last edited:

So, how would I encode that into my "decimal" function because I am confused as to how to print it via the main function?

mgb_phys

Homework Helper
Sorry not sure what you mean.
Do you mean how would you write the result of n/d as a fraction? - You could simply printf("%u/%u",n,d)
If you want the fraction n/d as a reduced fraction you will have to write a function to find the common multiple.

ps. there is no need for the decimal() function call, you could just do the sum in the printf.
eg printf("%f",(double)n/d);

Below is what I have coded so far; My problem is that I need to write one more function outside of "main" to display the integers in fraction form without actually computing it. Please, I need your help in coding the last function. Thanks

double decimal(int, int); // function prototype of fraction's decimal form
int gcd(int, int); // function prototype for greatest common denominator

# include <stdio.h>

int main()
{
int n; // represents numerator
int d; // represents denominator
int fraction; // represents numerator & denominator as a fraction

printf("Hello, this program is designed to accept two integers \n"
"representing the numerator and denominator of a fraction. \n"
"It is also designed to display the decimal form, \n"
"GCD, and the fractional form of the fraction! \n\n");

printf("What is the integer value of your numerator? ");
scanf("%d", &n);
printf("What is the integer value of your denominator? ");
scanf("%d", &d);

printf("\nThe GCD of your fraction is: %+d ", gcd(n, d));

printf("\nThe decimal form of your fraction is: %+lf \n", decimal(n, d));

system("pause");
return 0;
}

double decimal(int n, int d)
{
return (double)n/d;
}

int gcd(int n, int d)
{
if (d == 0)
return n;
else
return gcd(d, n % d);
}

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving