C+ Programming Homework Help (Not C++)

In summary, the fractional form of a fraction is displayed as:Hello, this program is designed to accept two integers representing the numerator and denominator of a fraction. It is also designed to display the decimal form, GCD, and the fractional form of the fraction.What is the integer value of your numerator? 1What is the integer value of your denominator? 2The GCD of your fraction is: 1The decimal form of your fraction is: 1.6
  • #1
vladittude0583
40
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
  • #2
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:
  • #3
So, how would I encode that into my "decimal" function because I am confused as to how to print it via the main function?
 
  • #4
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);
 
  • #5
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);
}
 

1. What is C+ Programming?

C+ Programming is a high-level programming language that is used to develop various software applications and systems. It has a rich set of features and is widely used in industries such as gaming, operating systems, and embedded systems.

2. Why is C+ Programming important?

C+ Programming is important because it is a widely used language in the software industry. It is known for its performance, efficiency, and portability, making it a popular choice for developing complex and resource-intensive applications.

3. What are the key features of C+ Programming?

Some of the key features of C+ Programming include its ability to handle low-level memory management, its object-oriented programming capabilities, and its support for generic programming. It also has a large standard library and is highly portable.

4. How can C+ Programming homework help benefit me?

C+ Programming homework help can benefit you by providing assistance with understanding the language's syntax, concepts, and best practices. It can also help you develop problem-solving skills and improve your overall programming abilities.

5. Can I learn C+ Programming on my own?

Yes, it is possible to learn C+ Programming on your own. However, it is recommended to have a strong foundation in basic programming concepts and to use resources such as textbooks, online tutorials, and practice exercises to supplement your learning.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
838
  • Engineering and Comp Sci Homework Help
Replies
4
Views
879
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
Back
Top