C+ Programming Homework Help (Not C++)

  • Context: Comp Sci 
  • Thread starter Thread starter vladittude0583
  • Start date Start date
  • Tags Tags
    Homework Programming
Click For Summary

Discussion Overview

The discussion revolves around a programming homework assignment in C that requires writing a program to accept two integers representing the numerator and denominator of a fraction. The program should display the fraction in its lowest form with a sign only in the numerator, as well as the fraction in signed decimal form. Participants are seeking help with specific coding challenges related to function implementation and output formatting.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant inquires about how to enforce a positive or negative sign for the decimal form of the fraction.
  • Another participant suggests using the printf format specifier %+f to display the sign in the decimal output.
  • There is a question regarding how to write a function that prints the numerator and denominator as a fraction without performing any calculations.
  • Some participants discuss the need to implement a loop that terminates when the user inputs a sentinel value or EOF, with suggestions on how to handle scanf return values.
  • A participant expresses confusion about how to integrate the sign formatting into their existing decimal function.
  • Another participant notes that the decimal function call may be unnecessary and suggests directly performing the division within the printf statement.

Areas of Agreement / Disagreement

Participants generally agree on the need for specific functions to handle the output formatting and calculations, but there are differing opinions on the implementation details and whether certain function calls are necessary. The discussion remains unresolved regarding the best approach to achieve the desired output.

Contextual Notes

Participants have not reached a consensus on the implementation of the function to print the fraction in its lowest form or how to handle the loop termination effectively. There are also varying interpretations of the requirements for displaying the decimal form.

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);
}
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
Replies
9
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K