Oct 22, 2011 at 5:36pm UTC
I am having a minor problem getting the correct output on the following program. I am sure it is just a simple quick fix but I can't figure it out..any help would be greatly appreciated.
Desired Output:
Enter n1:1
Enter d1:2
Enter n2:1
Enter d2:2
1/2 + 1/2 = 1 0/4
PROGRAM ENDS
Here is the output I am getting with my program:
Enter n1:1
Enter d1:2
Enter n2:1
Enter d2:2
1/2 + 1/2 = 1
PROGRAM ENDS
Here is my program...again, any help is greatly appreciated
5 //Progammingexcercise2.cpp
6 //Date: 10/22/11
7
8
9 #include <iostream>
10 using namespace std;
11
12 int main()
13 {
14 int a, b, c, d, e;
15
16 cout<<"Enter n1:";
17 cin>>a;
18
19 cout<<"Enter d1:";
20 cin>>b;
21
22 cout<<"Enter n2:";
23 cin>>c;
24
25 cout<<"Enter d2:";
26 cin>>d;
27
28 a/b + c/d;
29 e = (a*d + b*c)/(b*d);
30
31 cout<< a << "/" << b << " + " << c << "/" << d << " = " <<e<<endl;
32 cout<<"PROGRAM ENDS"<<endl;
33
34 }
Oct 22, 2011 at 7:10pm UTC
Funny requirement, but anyway, is below what you want?
int f;
e = a*d + b*c;
f = b*d;
cout<< a << "/" << b << " + " << c << "/" << d << " = ";
cout << e/f << " " << e%f << "/" << f << endl;
Oct 22, 2011 at 7:53pm UTC
No. The answer is actually 1 0/4
Another example would be:
Enter n1:3
Enter d1:4
Enter n2:5
Enter d2:8
3/4 + 5/8 = 1 12/32
PROGRAM ENDS
I can't figure it out. Thanks for trying!
Oct 22, 2011 at 8:11pm UTC
Does this work?
1 2 3
whole = (a*d+b*c)/(b*d);
numer = (a*d+b*c)%(b*d);
denom = b*d;
EDIT: I realized that Eric Dus solution gives the same result. His solution should work fine.
Last edited on Oct 23, 2011 at 1:59am UTC
Oct 22, 2011 at 9:06pm UTC
jhotz30 wrote:No. The answer is actually 1 0/4
How do you know the following code doesn't work, have you tested it? What result you got from it?
cout<< a << "/" << b << " + " << c << "/" << d << " = ";
cout << e/f << " " << e%f << "/" << f << endl;
Last edited on Oct 22, 2011 at 9:07pm UTC
Oct 22, 2011 at 11:43pm UTC
to jhotz30:
Why isn't 3/4 + 5/8 = 1 3/8 instead of 1 12/32?
Oct 23, 2011 at 2:13pm UTC
to fun2code...I am not looking for the answer 104...I need it to read 1 "space" 0/4
to bufbill:
I have no clue why. we have to match our instructers code. another example would be....
Enter n1:2
Enter d1:8
Enter n2:1
Enter d2:3
2/8 + 1/3 = 0 14/24
PROGRAM ENDS
PLease help!
Thanks
Oct 23, 2011 at 2:40pm UTC
You've got the digits you need, just add the space and slash as characters. that part is easy (and done above by EricDu).
Oct 23, 2011 at 3:13pm UTC
AWESOME...yes it worked. Thanks everyone for the help!