somebody help, time is limited till 10pm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • README
    New Member
    • Nov 2008
    • 9

    somebody help, time is limited till 10pm

    ok i have the following c++ and need help to develope a function of the following description :

    void addRational(int & ansNum, int& ansDenom)

    This function will get 2 rational numbers from the user by calling getRational(), addsthose values and sets the result in ansNum (answer numerator) and ansDenom. It finally
    calls displayRational to display the answer.

    now i have created the getRational and displayFunction () , i just need to create the function body of addRational(). In my case it is the case 5: on the switch function.


    so here is my c++, of what i have so far:

    #include<iostre am> //Needed for cout and cin
    using namespace std;
    void menu(); //Menu-function prototype
    void getRational(int &,int&); //Function1 protype
    void displayRational (int&,int&);
    void addRational(int &,int&);
    void decimalEquivale nt(int&, int&);
    int main(){

    //Declare variables
    int choice1,x,y;
    static int numer;
    static int denom;

    do{ // Reiterates switch command until exited

    menu();//Call function menu for menu

    //Prompt the user for menu choice
    cout<<endl<< "Choose one of the option listed above"<<endl;
    cin>>choice1;


    switch(choice1) {

    case 0: cout<<endl<<end l<<"The program will now exit"<<endl<<en dl;
    exit(1);
    break;
    case 1: getRational(num er,denom);//Prompts the user to input numerator and denominator
    break;

    case 2:
    { if(numer<=0 || denom<=0)//Prompt user to input rational
    getRational(num er,denom);
    displayRational (numer,denom);//Displays the Rational
    break;

    }
    case 3: addRational(num er,denom);
    break;



    }

    }while(choice1! =0);



    return 0;
    }

    void menu()//Menu Function--displays main menu
    {
    cout<<endl<<end l<<"1) GetRational "<<endl<<"2 ) Display Rational "<<endl<<"3 ) Add Rational "<<endl;
    cout<<"4) Subtract Rational "<<endl<<"5 ) Decimal Equivalent "<<endl<<"0 ) Exit "<<endl<<endl<< endl;

    return;
    }

    void getRational(int & numer, int& denom)//This function prompts user for inputs of numerator and denominator.
    {

    cout<<endl<<" Please enter the numerator and then the denominator: "<<endl;
    cin>>numer>>den om;

    return;

    }
    void displayRational (int& numer, int& denom)
    {
    cout<<"Your number in rational form is : ";
    cout<<numer<<"/"<<denom<<e ndl;


    return;
    }
    void addRational(int & ansNum, int& ansDenom){

    return;
    }



    void decimalEquivale nt(int& numer, int& denom)
    {
    cout<<"The decimial equivalent of your rational is :";
    cout<<(double)n umer/denom<<endl;
    return;
    }
  • ashitpro
    Recognized Expert Contributor
    • Aug 2007
    • 542

    #2
    Code:
    void addRational(int &ansNum,int &ansDeno)
    {
    	int firstNum,firstDenom,secondNum,secondDenom;
    	getRational(firstNum,firstDenom);
    	getRational(secondNum,secondDenom);
    	ansNum = <snipped>;
    	ansDenom = <snipped>;
    }
    This should work for you....
    Resulted rational number is not in normal form. i.e. You must implement logic for reducing rational number, Of course if needed.

    Regards,
    Ash
    Last edited by Banfa; Nov 3 '08, 01:26 PM. Reason: Code Solution Removed

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      README,

      Do you know how to add fractions? Can you add 1/3 + 1/5 on paper without resorting to using a calculator?

      If not then I suggest you start by reading this which will explain how addition of fractions works. You will need to follow the links at the bottom of the page to learn how to combine the denominators.

      Having read that (or if you can already do the sum I gave earlier) then examine how you add 2 fractions and write a computer algorithm that does the same thing.

      Comment

      Working...