Thread: How do i make output answers into fractions?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    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!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.
    You've got your fraction.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Additionally, what are you doing with main()? I don't think I have seen that before, read How to define main()-FAQ
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    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? =\

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by AndrewHunter View Post
    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 ?

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iluvanimestyle View Post
    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.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    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?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The second version is better...

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

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Quote Originally Posted by CommonTater View Post
    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?

  10. #10
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    what does putting variables inside the parenthesis in int main mean?
    That main gets them as arguments.... so they are valid identifiers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect make output to some other file..
    By jgtech in forum Linux Programming
    Replies: 1
    Last Post: 08-07-2011, 11:35 PM
  2. Trying to make this counter output something different
    By NinjaFish in forum C Programming
    Replies: 14
    Last Post: 02-16-2011, 05:13 AM
  3. How can I make random answers to user inputs in C++?
    By Catfish0221 in forum C++ Programming
    Replies: 3
    Last Post: 01-31-2011, 08:47 PM
  4. can any one make this program run.. in proper output?
    By randyjhon143 in forum C++ Programming
    Replies: 3
    Last Post: 12-03-2009, 09:22 AM
  5. Replies: 1
    Last Post: 03-12-2002, 06:31 AM