Like Tree5Likes

need help with fractions in C++

This is a discussion on need help with fractions in C++ within the C++ Programming forums, part of the General Programming Boards category; I need to make fractions to be in the simplest form. how to do it .i know i need to ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    20

    need help with fractions in C++

    I need to make fractions to be in the simplest form. how to do it .i know i need to call and pass fraction to gcd and simplify but i dont now how to do it so it will work..

    Code:
    int divide()
    
    
    {
    
    
    int one4,two4,three4,four4;
    int e4,f4;
    
    
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cin>>one4;
    cin>>two4;
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cin>>three4;
    cin>>four4;
    
    
    cout<<"Input instruction/_" << endl;
    
    
    e4 = one4*four4;
    f4 = two4*three4;
    
    
    
    
    cout<<"("<< one4 << "/"<< two4 <<") / ("<< three4 <<"/"<< four4 <<") = " << e4 << "/" << f4;
    
    
    cout << "\n";
    return 0;
    } 
    
    
    
    
    
    void simplify(int x2 , int y2 ,int gcd )
    
    
    
    
     {
    
    
    int a;
    int b;
    
    
    
    
    
    
    cout<< "Input the nominator and denomiantor of a fraction separated by spaces :" << x2 <<"  "<< y2 << endl; 
     
    a= x2/gcd;
    b= y2/gcd;
    
    
    cout<<"This fraction equals ("<< a <<"/"<< b << ")"<< endl;
    
    
    
    
    
    
    
    
    }
    
    
    
    
    
    
    int gcd(int x2, int y2)
    {
        int i;
        int gcd2;
    
    
        for (i=1; i<=x2; i++)
        {
    
    
            if((x2%i==0) && (y2%i==0))
                gcd2 = i;
        }
        
                return gcd2;
    
    
        
        
    }
    
    
    
    
    void input()
    {
        int x,y,k;
        cout << "Enter first integer: ";
        cin >> x;
        cout << "Enter second integer: ";
        cin >> y;
        k=gcd(x,y) ;
    
    
        cout <<"GCD is "<< k << endl;
    
    
    
    
        simplify( x , y , k); //calls
    
    
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    First, fix the layout of your code. Far too many empty lines and no indentation; it's completely unreadable.

    Second, state exactly what you want to happen and what actually happens with your code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    i want the fraction result in multiply to be simpler.thats why i was trying to send result from multiply to gcd and after to simplify but im doing something wrong

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As CornedBee said:
    First, fix the layout of your code.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    int multiply()
    
    
    {
    int one3,two3,three3,four3;
    int e3,f3;
    
    
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cin>>one3;
    cin>>two3;
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cin>>three3;
    cin>>four3;
    cout<<"Input instruction*_" << endl;
    e3 = one3*three3;
    f3 = two3*four3;
    
    
    cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
    cout << "\n";
    return 0;
    } 
    int gcd(int x2, int y2)
    {
    int i;
    int gcd2;
    for (i=1; i<=x2; i++)
    {
    if((x2%i==0) && (y2%i==0))
    gcd2 = i;
    }
    return gcd2;
    		
    }
    
    void simplify(int x2 , int y2 ,int gcd )
    {
    int a;
    int b;
    a= x2/gcd;
    b= y2/gcd;
    }
    
    
    
    void main()
    
    
    {
    	multiply();
    
    
    	system ("PAUSE");
    }

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You seem to have some extra formatting (e.g., bolding) in the code you posted. And very little indentation. Properly formatted code looks more like this:
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int multiply()  /// Should be void multiply() if it only ever returns 0.
    {
        /// Why do your variables all end with the number 3?
        int one3,two3,three3,four3;
        int e3,f3;
    
        /// It's "numerator" not "nominator".
        cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
        cin>>one3;
        cin>>two3;
    
        cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
        cin>>three3;
        cin>>four3;
    
        cout<<"Input instruction*_" << endl;
        e3 = one3*three3;
        f3 = two3*four3;
    
        cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
        cout << "\n";
        return 0;
    } 
    
    /// This doesn't seem to be a proper gcd implementation.
    /// Didn't you look at the pseudocode I gave in your original thread?
    int gcd(int x2, int y2)
    {
        int i;
        int gcd2;
        for (i=1; i<=x2; i++)
        {
            if((x2%i==0) && (y2%i==0))
                gcd2 = i;
        }
        return gcd2;	
    }
    
    /// This function doesn't actually accomplish anything. (And is never called.)
    void simplify(int x2 , int y2 ,int gcd )
    {
        int a;
        int b;
        a= x2/gcd;
        b= y2/gcd;
    }
    
    void main()            /// should be int main() and return 0 at the end
    {
    	multiply();
    	system ("PAUSE");
    }

  7. #7
    Registered User
    Join Date
    Feb 2012
    Posts
    20

    Code:
    
    int multiply()
    
    
    {
    int one3,two3,three3,four3;
    int e3,f3;
    
    
    cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
    cin>>one3;
    cin>>two3;
    cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
    cin>>three3;
    cin>>four3;
    cout<<"Input instruction*_" << endl;
    
    
    e3 = one3*three3;
    f3 = two3*four3;
    gcd(e3,f3); //call 
    cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3<<this  e3 and f3 i want to be simpler
    cout << "\n";
    return 0;
    } 
    
    
    void simplify(int x2 , int y2 ,int gcd )
    
    
     {
    
    
    int a;
    int b; 
    a= x2/gcd;
    b= y2/gcd;
    return a,b;
    
    
    
    
    }
    
    
    int gcd(int x2, int y2)
    {
    int i;
    int gcd2;
    for (i=1; i<=x2; i++)
    {
    if((x2%i==0) && (y2%i==0))
    gcd2 = i;
    }
    return gcd2;
    
    
    	
    	
    }
    
    
    void main()
    
    
    {
    	multiply();
    
    
    	system ("PAUSE");
    }


    its only part of my assigment ..thats why u can see 3 in the end of letters.. my assigment was to divide multiply add substract fractions and after make them simpler... so forexample 5/10 will be 1/2 .... thats why first i did multiply after i want the integers to move to gcd and simplify and come back simpler to multiply in cout...

    in simplify i cant move a,b to my cout in multiply... please help me Im so confussed..

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Code:
    return a,b;
    You can't return two values from a function like that. Look into passing-by-reference.
    Last edited by rags_to_riches; 02-18-2012 at 04:57 PM. Reason: C++ forum. My bad.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    im going crazy here

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    int divide()
    
    
    {
    int one4,two4,three4,four4;
    int e4,f4;
    int x;
    
    
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cin>>one4;
    cin>>two4;
    cout<<"Input the nominator and denominator of a fraction separated by spaces :"<<endl;
    cout<<"Input instruction/_" << endl;
    
    
    
    
    e4 = one4*four4;
    f4 = two4*three4;
    
    
    x=gcd(e4,f4);
    simplify(e4,f4,x);
    cout<<x<<endl;
    cout<<"("<< one4 << "/"<< two4 <<") / ("<< three4 <<"/"<< four4 <<") = " << e4 << "/" << f4;
    cout << "\n";
    return 0;
    } 
    
    
    int multiply()
    
    
    {
    
    
    int one3,two3,three3,four3;
    int e3,f3,p;
    
    
    cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
    cin>>one3;
    cin>>two3;
    cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
    cin>>three3;
    cin>>four3;
    cout<<"Input instruction*_" << endl;
    e3 = one3*three3;
    f3 = two3*four3;
    p=gcd(e3,f3);  here is telling me(1>fdgfd.cpp.cpp(24): error C3861: 'gcd': identifier not found
    1>fdgfd.cpp.cpp(25): error C3861: 'simplify': identifier not found
    1>fdgfd.cpp.cpp(48): error C3861: 'gcd': identifier not found
    1>fdgfd.cpp.cpp(49): error C3861: 'simplify': identifier not found
    1>
    simplify(e3,f3,p);
    
    
    cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
    
    
    cout << "\n";
    return 0;
    } 
    void simplify(int x2 , int y2 ,int gcd )
     {
    
    
    int a;
    int b;
    a= x2/gcd;
    b= y2/gcd;
    return (a,b);
    
    
    }
    int gcd(int x2, int y2)
    {
        int i;
        int gcd2;
    
    
        for (i=1; i<=x2; i++)
        {
    
    
            if((x2%i==0) && (y2%i==0))
    gcd2 = i;
        }
    return gcd2;
    }
    void main()
    
    
    {
    multiply();
    divide();
    
    
    system ("PAUSE");
    }


    MAybe now somebody can help me ....

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,663
    Lookup function prototypes
    Function prototype - Wikipedia, the free encyclopedia

    The above is a guess since your code is not readable.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    You've been told to indent your code. You've even been shows how it should look like.
    You've even been told main should return int.

    Yet, I still do not see you have fixed any of this. Why?
    If you expect any more help, then fix what you've already been told to fix!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    if i will know how to do it I will be not on this forum.... Yes people told me that my code is unreadable but in my class everybody is writing like that.. so im not catching u guys.. im new to it .. all of u know it ...
    my gcd function works
    simplify tooo
    only thing im asking is for somebody to write me how to connect it together.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by Agnes Roguska View Post
    if i will know how to do it I will be not on this forum.... Yes people told me that my code is unreadable but in my class everybody is writing like that.. so im not catching u guys.. im new to it .. all of u know it ...
    Don't blame it on others. Because someone else is doing something is not an excuse for you to do it.
    We do not expect you to know everything here. But we do expect you to put an effort into learning what you're told, not ignoring it.
    If you don't understand how to do something, google it, or if you can't find any good resources that help your understanding, ask for help about it.
    We will help you achieve the understanding as long as you are willing. But do not ignore advice or comments if you don't understand them.

    only thing im asking is for somebody to write me how to connect it together.
    First things first. Fix what you've been told.
    If you don't understand something, ask about it.
    How difficult is it to change the return type of main?
    What is it that is so difficult about indenting?
    At least you can become better than your class mates who all write unreadable code and who will not get a programming or will be a pain in the ass to work with.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by Agnes Roguska
    Yes people told me that my code is unreadable but in my class everybody is writing like that.. so im not catching u guys.. im new to it .. all of u know it ...
    oogabooga gave you an example in post #6. The basic idea is simple: when your code is in a block of scope (e.g., within the body of a function, an if statement, or a loop), you indent by one level. If within the block there is an inner block, you indent again by one level. After the block ends, you decrease the indentation by one level. An indentation can be either a few spaces (my editor is set to produce 4 spaces when I press the tab) or a tab character.

    The other thing is blank lines: as a rule of thumb, you should not have more than two consecutive blank lines in your code. Usually, a single blank line to denote some logical separation of the lines of code will suffice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fractions are just = to 0
    By Macca in forum C Programming
    Replies: 1
    Last Post: 05-10-2011, 05:41 AM
  2. Fractions
    By madmax2006 in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 11:58 PM
  3. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  4. Fractions
    By Aakash Datt in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2003, 07:36 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 06:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21