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

AI Thread Summary
The homework assignment requires writing a C program that accepts two integers as a numerator and denominator, displaying the fraction in lowest terms and its decimal form. Key functions needed include one for printing the fraction, another for its decimal representation, and a third to calculate the greatest common divisor (gcd) for reducing the fraction. Participants are seeking help on enforcing a sign for the decimal output, printing the fraction without calculations, and implementing a loop that exits on specific user inputs. Solutions provided include using formatted output for signs and suggestions for handling user input effectively.
vladittude0583
Messages
40
Reaction score
0
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.
 
Physics news on Phys.org
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?
 
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. Thanksdouble 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);
}
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Replies
3
Views
1K
Replies
12
Views
2K
Replies
12
Views
2K
Replies
3
Views
1K
Replies
4
Views
1K
Replies
1
Views
10K
Replies
9
Views
4K
Back
Top