# How do i make output answers into fractions?

• 09-14-2011
iluvanimestyle
How do i make output answers into fractions?
Code:

``` #include <iostream> #include <cmath> using namespace std; int main (double x1 , double y1 , double x2 , double y2) {     double midpointx;     double midpointy;     cout << "First Coordinate.\n" << endl << "Enter x " << endl;     cin >> x1;     cout << "Enter y " << endl;     cin >> y1;     cout << "Second Coordinate.\n" << endl << "Enter x " << endl;     cin >> x2;     cout << "Enter y " << endl;     cin >> y2;     midpointx = (x1 + x2)/2;     midpointy = (y1 + y2)/2;     cout << "The midpoint of your two coordinates is: ( " << (midpointx) << " , " << (midpointy) << " ). " << endl;     cin.get();     return 0; }```
i made this basic program and i tested it and it works but i have a problem. If the answer is in a decimal such as doing the equation (5 divided by 2). The answer will come out as 2.5. Is there a way i can make the answer in a fration such as 5 over 2?. Also preferably the answer should be in fraction form if the equation does not divide evenly and should be a whole number if it does divide evenly.

EX : (5 divided by 2 answer should be 5/2)
(6 divided by 2 answer should be 3 and not 3/1)

Thanks!
• 09-14-2011
manasij7479
Quote:

Is there a way i can make the answer in a fration such as 5 over 2
You have to manually do it.
Take the double...
Depending on the no of significant figures, multiply by powers of 10.
Now the fraction is (d*100..)/100..
Use the HCF of the Numerator and Denominator to divide both.
• 09-14-2011
AndrewHunter
Additionally, what are you doing with main()? I don't think I have seen that before, read How to define main()-FAQ
• 09-14-2011
iluvanimestyle
Sorry im really really really really new to C++ and i've only been learning for 3 days. Could you explain to me in simpler terms please? =\
• 09-14-2011
manasij7479
Quote:

Originally Posted by AndrewHunter
Additionally, what are you doing with main()? I don't think I have seen that before, read How to define main()-FAQ

I thought about that too, but is it applicable in certain scenarios ? ..like say...main receiving arguments from places other than the terminal ?
• 09-14-2011
manasij7479
Quote:

Originally Posted by iluvanimestyle
Sorry im really really really really new to C++ and i've only been learning for 3 days. Could you explain to me in simpler terms please? =\

There was nothing specific to programming in my explanation... It is just a little arithmetic.
• 09-14-2011
iluvanimestyle
Code:

```#include <iostream> #include <cmath> using namespace std; int main (void) {     double midpointx;     double midpointy;     double x1;     double y1;     double x2;     double y2;     cout << "First Coordinate.\n" << endl << "Enter x " << endl;     cin >> x1;     cout << "Enter y " << endl;     cin >> y1;     cout << "Second Coordinate.\n" << endl << "Enter x " << endl;     cin >> x2;     cout << "Enter y " << endl;     cin >> y2;     midpointx = (x1 + x2)/2;     midpointy = (y1 + y2)/2;     cout << "The midpoint of your two coordinates is: ( " << (midpointx) << " , " << (midpointy) << " ). " << endl;     cin.get();     return 0; }```
so the code should be changed to this?
• 09-14-2011
CommonTater
The second version is better...

This...
Code:

`int main (double x1 , double y1 , double x2 , double y2)`
... is NOT how you define variables.
• 09-14-2011
iluvanimestyle
Quote:

Originally Posted by CommonTater
The second version is better...

This...
Code:

`int main (double x1 , double y1 , double x2 , double y2)`
... is NOT how you define variables.

i just have a few questions
what does putting variables inside the parenthesis in int main mean?
I did it and it was able to run so whats wrong with my first version?
• 09-14-2011
manasij7479
Quote:

what does putting variables inside the parenthesis in int main mean?
That main gets them as arguments.... so they are valid identifiers.