Thread: a small program

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    17

    a small program

    I wrot a code for the following program but it seem doesn't work right Please show me my mistakes, Thanks

    A program that reads in the numerators and denominators of two fractions. The program should print the product of the two fractions as fraction and as a percent.

    my code is
    [cod]

    #include <iostream>
    using namespace std;


    int main()

    {
    int Fn ,Fd, Sn, Sd;

    int FracAns=0;
    float PercAns =0;

    cout<<"enter the numerator and the denomenator for the first fraction"<<endl;
    cin>>Fn;
    cin>>Fd;
    cout<<"Enter the numerator and the denomentaor"<<endl;
    cout<<"for the second fraction"<<endl;
    cin>>Sn;
    cin>>Fd;
    if (Fd || Sd==0)
    {

    cout<<"You cant not divide by zero, try again"<<endl;
    }

    FracAns = Fn*Sn / Fd*Sd ;
    PercAns = FracAns / 100;
    cout<< "the procuct of the two fraction as a fraction is: "<<endl;
    cout<<FracAns<<endl;
    cout<< " as percentage is "<<PercAns<<endl;

    return 0 ;
    }

    [\code]

    Help would be apprciated.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Here are a couple fixes.

    Code:
    cout<<"enter the numerator and the denomenator for the first fraction"<<endl;
    	cin>>Fn;
    	cin>>Fd;
    	cout<<"Enter the numerator and the denomentaor"<<endl;
    	cout<<"for the second fraction"<<endl;
    	cin>>Sn;
    	cin>>Sd; // fixed here
    	if (Fd == 0 || Sd==0) // fixed here
    	{
    What do you mean by fractional answer? Should it 1/2 * 3/4 print 3/8?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    int Fn ,Fd, Sn, Sd;

    int FracAns=0;
    float PercAns =0;

    cout<<"enter the numerator and the denomenator for the first fraction"<<endl;
    cin>>Fn;
    cin>>Fd;
    cout<<"Enter the numerator and the denomentaor"<<endl;
    cout<<"for the second fraction"<<endl;
    cin>>Sn;
    cin>>Sd; // you might want to get the second denominater
    // instead of the first one again

    if ((Fd*Fn*Sn*Sd) == 0) // better way of doing it
    {
    cout<<"You cant not divide by zero, try again"<<endl;
    return 0; // dont keep running the program if input
    // is wrong
    }

    FracAns = (Fn*Sn) / (Fd*Sd) ; //your math was messed up
    Last edited by D-Man; 11-10-2002 at 09:07 PM.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Except your better way of doing it is wrong. Consider adding a fraction with a zero numerator but non-zero denomatior (IE 0/1), your if will incorrectly say that is divison by zero.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    Hes not adding fractions hes dividing them. 1/4 divided by 0/4 is what? If you were adding then you would have to do

    if((Fd*Sd) == 0)

    but that wasnt the case.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    He is multiplying. It is still the case that 0's in numerators are valid.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    14
    FracAns = Fn*Sn / Fd*Sd ;
    PercAns = FracAns / 100;

    he is infact trying to divide the fractions... the reason I put

    if ((Fd*Fn*Sn*Sd) == 0)

    Was to check if any of the numbers are equal to zero If they were zero then stop the program ... ANYTHING TIMES 0 is 0 right?

    Either my math really sucks or yours does.

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Suck is relative.

    He says "The program should print the product of the two fractions as fraction and as a percent."

    product is multiplication.

    Anything times 0 is indeed 0. However, (0/1) * (5/2) is quite legal, and equal to 0, but your condition would flag it as illegal. (1/0) * (5/2) is not legal.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Thank you guys for all the answers, I appreciate them all.
    I think I made a typo when I wrote Sd in the second fraction it should be Sd.
    I fixed the typo and I put Sd instead of Fd and I ran the program but all what I get as result is 0.
    I just need the product of two fraction as
    1) fraction like m/n * k/l = d/b and as
    2) percetage like .02

    it legal if you use 0/m the result will be zero.
    but it is illegal to use m/0 because it is undefined.
    I wanted to make a condition that it is illegal to use 0 in either fraction as a denominator.
    and if the test is true( which means the den. is 0) I should return to the beginning of the program but not exit it.

    Thanks again and waiting for any suggestion.

  10. #10
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    I agree with SilentStrike.
    D-Man, you can say this if ((Fd*Sd*Sn) == 0)
    Because first numerators can be 0.
    Absy,
    I think you can make a class for the fraction, where yo can make a function to simplify, and you can overload the stream extraction and stream insertion operator, which will help you organize the input and output, and you can also overload the mathimatical operators...
    Try it, it's not that hard
    your program will be much better.

  11. #11
    ..............
    Guest
    Originally posted by Absy

    it legal if you use 0/m the result will be zero.
    but it is illegal to use m/0 because it is undefined.
    I wanted to make a condition that it is illegal to use 0 in either fraction as a denominator.
    and if the test is true( which means the den. is 0) I should return to the beginning of the program but not exit it.

    Thanks again and waiting for any suggestion. [/B]
    Blah. And by that I meant:
    Code:
    do
    {
         cout << "Enter the numerator and denominator for the first fraction." << endl;
         // etc.
    }
    while(Fd == 0 || Sd == 0);
    If you like something a little less compact, but nicer:

    Code:
    cout << "Enter the numerator and denominator for the first fraction." << endl;
         // etc.
    while(Fd == 0 || Sd == 0)
    {
         cout << "You can't divide by zero.  Try again." << endl;
         cout << "Enter the numerator and denominator for the first fraction." << endl;
         // etc.
    
    }
    And one very important mistake, which someone pointed out already, was that you can't do if(Fd || Sd == 0)...these compund statements are not like in English, where you can say, "Susy and Jim went to the mall." In C++ you have to say, "Susy went to the mall and Jim went to the mall." You can't compare more than one value at once. Hope I helped some.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    int gcd(int a, int b)
    {
      if (b == 0)
         return a;
      return gdc(b, a % b);
    }
    
    void simplifyFraction(int& n, int& d)
    {
      int g = gcd(n,d);
      n /= g;
      d /= g;
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    17
    Hi everybody.
    Please read the requirement very carefully. The program I want is not that complicated it should be easy but I'm making a mistake somewhere and I'm not getting the result I want the program is;

    A program that reads in the numerators and denominators of two fractions. The program should print the product of the two fractions as fraction and as a percent.


    my code is
    Code:
    
    #include <iostream>
    using namespace std;
    
    
    int main()
    
    {
    	int Fn ,Fd, Sn, Sd;
    	
    	int FracAns;
    	float PercAns;
    
    	cout<<"enter the numerator and the denomenator for the first fraction"<<endl;
    	cin>>Fn;
    	cin>>Fd;
    	cout<<"Enter the numerator and the denomentaor"<<endl;
    	cout<<"for the second fraction"<<endl;
    	cin>>Sn;
    	cin>>Fd;
    	if (Fd==0 || Sd==0)
    		{
    		
    			cout<<"You cant not divide by zero, try again"<<endl;
    		}
    	else
    
    		FracAns =  Fn*Sn / Fd*Sd ;
    		PercAns = FracAns / 100;
    	cout<< "the procuct of the two fraction as a fraction is: "<<endl;
    		cout<<FracAns<<endl;
    	cout<< " as percentage is "<<PercAns<<endl;
    
    	return ;
    }
    //I hope I'm using the right code tags here.

    my program does not getting the result I want either for math. problem or for programming one please I just want to know what is wrong I'm doing. I'm just doing that for fun and as a chanllenge for me and for everybody in this beautiful forum. Thanks.

  14. #14
    ..............
    Guest
    Uh, part of the proble might be that you haven't taken a lot of the advice given. First off, FracAns in your program will wind up as a decimal, not a fraction. If you make FracAns a float instead of an int, that is, and to do that you would also have to make Fd and Sd floats. Is this what you want to do? If PercAns is 50, for example, would you want FracAns to be .5 or 1/2? Because you're not doing either correctly.

    Another problem that you talked about earlier, and that I offered a solution to earlier, was this:
    Code:
    if (Fd==0 || Sd==0)
    		{
    		
    			cout<<"You cant not divide by zero, try again"<<endl;
    This will not allow the user to enter valid values for Fd and Sd. Loops are you friends.

    Another thing is that you need brackets around just about everything after else.

    A typo that someone else pointed out earlier, you have not corrected. It's here: cin>>Sn; cin>>Fd; That's a bit of a problem, now isn't it? Not only is Fd not the value you intended, but you are performing operations on Sd without ever having given it a value.

    One more thing that has been pointed out to you already is this: FracAns = Fn*Sn / Fd*Sd ; You should put parenthesis around Fn*Sn and Fd*Sd.

    Now, next time, could you please listen to the suggestions people already gave you before asking for more?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with small program
    By cjohnman in forum C Programming
    Replies: 11
    Last Post: 04-14-2008, 09:59 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 10:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM