Friday, July 31, 2009

C++ Programming Trouble?

I started the problem but I am stock. I do not know what to do. Please help!





Write a program that asks for two positive fractions, displays the fractions, and returns both the exact sum and


the exact product of the fractions in lowest terms.


To display a fraction “a / b”, where a and b are the numerator and the denominator, you may use a cout statement


that uses ...%26lt;%26lt; a %26lt;%26lt;" / " %26lt;%26lt; b %26lt;%26lt; ...








#include %26lt;iostream%26gt;


using namespace std;





struct frac{int numer, denom;};





frac f_add( frac x, frac y );





int main( ){


frac r, s, sum;


cout %26lt;%26lt; "Enter the numerator and the denominator of the first positive fraction separated by a space=%26gt; " %26lt;%26lt; endl;


cin %26gt;%26gt; r.numer %26gt;%26gt; r.denom;


cout %26lt;%26lt; "Enter the numerator and the denominator of the second positive fraction separate by a space=%26gt; " %26lt;%26lt; endl;


cin %26gt;%26gt; s.numer %26gt;%26gt; s.denom;


sum = f_add(r,s);


cout %26lt;%26lt; "The sum of " %26lt;%26lt; r.numer %26lt;%26lt;"/" %26lt;%26lt; r.denom %26lt;%26lt; " and "%26lt;%26lt; s.numer %26lt;%26lt;"/" %26lt;%26lt; s.denom %26lt;%26lt;" in lowest term is " %26lt;%26lt; sum.numer %26lt;%26lt;"/" %26lt;%26lt; sum.denom %26lt;%26lt;".\n";


system ("Pause");


return 0;


}





frac f_add(frac x, frac y){





}

C++ Programming Trouble?
It seems you were given the template of the program, now you have to fill in the f_add function that takes two frac structures as input (x and y) and returns the sum.





Within f_add you have x.numer, x.denom and y.numer, y.denom. If you were to *multiply* x and y, them, you'd just do:





frac result;


result.numer = x.numer * y.numer;


result.denom = x.denom * y.denom;


return result;





Of course, you have to add them, which is just a bit harder (just remember how to add fractions), I'll let you do it hoping you get the idea.

tropical flowers

No comments:

Post a Comment