Thread: need help with fractions in C++

  1. #16
    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, x;
     
        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;
    	x=gcd(e3,f3);
    	simplify(e3,f3,x);
     
        cout<<"("<< one3 << "/"<< two3 <<") * ("<< three3 <<"/"<< four3 <<") = " << e3 << "/" << f3;
        cout << "\n";
        
    } 
    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;    
    }
     
    int simplify(int x2 , int y2 ,int gcd )
    {
        int a;
        int b;
        a= x2/gcd;
        b= y2/gcd;
    	return (a,b);
    }
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

  2. #17
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    gcd is working

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Very good. Now you are light years ahead of your class mates.
    Now you need to learn how to use references in order to pass back two values:

    Code:
    int simplify(int x2 , int y2 ,int gcd, int& a, int& b)
    {
        a= x2/gcd;
        b= y2/gcd;
        return (a,b);
    }
    
    Call as
    int a, b;
    simplify(x2, t2, gcd, a, b);
    // a and b will have the results
    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.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Agnes Roguska
    gcd is working
    That's good, though I note that there's a better algorithm (search the Web for Euclid's algorithm).

    So, what else do you need help with?

    Here's a tip: use descriptive variable names. Instead of one3, two3, three3, and four3, it would be better to name them numerator1, denominator1, numerator2, and denominator2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2;
        int x2,t2, x;
     
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
     
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
     
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
    int a, b;
    	x=gcd(x2,t2);
    	simplify(x2, t2,x, a, b);
    	
     
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
        cout << "\n";
        
    } 
    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;    
    }
     
    int simplify(int x2 , int y2 ,int gcd , int a , int b )
    {
        
        a= x2/gcd;
        b= y2/gcd;
    	return (a,b);
    
    
    }
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

    why is showing always that 1>fdgfd.cpp.cpp(22): error C3861: 'gcd': identifier not found
    1>fdgfd.cpp.cpp(23): error C3861: 'simplify': identifier not found

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Learn function prototypes.
    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.

  7. #22
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    And the pass by reference that's been repeatedly pointed out to you. You seem to struggle to heed the advice given to you.

  8. #23
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    
    int 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;    
    }
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2;
        int x2,t2, x;
     
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
     
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
     
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
        int a, b;
    	x=gcd( x2, t2);
    	simplify( x2, t2, x, a, b);
    	
     
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
        cout << "\n";
        return 0;
    } 
    
    
     
    
    
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

    hmm the result is 0 hahah i know u are giving me tips but im so useless

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Read up on references again.
    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.

  10. #25
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    pass by the references - can u give me an easy example .. cos im searching on google and its showing me hard examples.
    sorry guys but really programming is too hard for me ..cos im mixing everything

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I already did a while ago.
    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.

  12. #27
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
     
    int 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;    
    }
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2;
        int x2,t2, x;
      
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
      
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
      
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
        int a, b;
        x=gcd( x2, t2);
        simplify( x2, t2, x, a, b);
         
      
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " <<a << "/" << b;
        cout << "\n";
        return 0;
    } 
     
     
      
     
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

  13. #28
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    can somebody do it for me !!!so i will not disturb u anymore .

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is absolutely no need for pointers. You are only complicating this more than it needs to be.
    Go back to your previous code, then go back look at my example on how to use references. If needs be, start a new project and try to get it working, then return to your current project once you got it working.
    And no - no one will do it for you! You are the one who needs to learn, not us!
    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. #30
    Registered User
    Join Date
    Feb 2012
    Posts
    20
    Code:
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
     
     
    int simplify(int x2 ,int y2 ,int gcd ,int a ,int b )
    {
         
        a= x2/gcd;
        b= y2/gcd;
    
    
        return simplify;
    }
     
     
     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;    
    }
    int multiply() 
    {
        int numerator,denominator,numerator2,denominator2 ;
        int x;
    	 int x2, t2;
    	 int c;
    	 int d;
    
    
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator;
        cin>>denominator;
      
        cout<<"Input the numerator and denominator of a fraction separated by spaces :"<<endl;
        cin>>numerator2;
        cin>>denominator2;
      
        cout<<"Input instruction*_" << endl;
        x2 = numerator*numerator2;
        t2 =denominator*denominator2;
        
        x=gcd( x2, t2);
        simplify(  x2 ,  t2 ,x , c ,  d);
         
      
        cout<<"("<< numerator << "/"<<denominator <<") * ("<< numerator2 <<"/"<< denominator2 <<") = " << c << "/" << d;
        cout << "\n";
        return 0;
    } 
     
     
     
    int main()           
    {
        multiply();
        system ("PAUSE");
    }

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, 07:51 AM