Question

In: Computer Science

IN C++ Rather than clutter up the source code with blocks of essentially the same calculations...

IN C++

Rather than clutter up the source code with blocks of essentially the same calculations or blocks of code controlled by IF then…else logic, we can move these blocks of code into one location that can be called from anywhere and anytime during the application’s execution, the “function”.

Functions can be passed “arguments”, one or more data values and return zero or one value to the calling routine Functions can call other separate functions or even themselves recursively.

  1. User can manually enter two decimal values
  2. Application will allow user to decide whether to:
    1. Add the two numbers, return the total (i.e. n1 + n2) and display in the main
    2. Subtract Second number from the First number (i.e. n1 – n2), return the difference and display it in the main
    3. Multiply the two numbers, return the product (i.e. n1 * n2) and display it in the main
    4. Divide the First number by the Second number (i.e. n1 / n2), return the dividend and display it in the main.

If the divisor, n2, is zero, display a message, “Cannot divide by zero” in the function, default n2 to 1, return the dividend and display it in the main.

    1. Call all the above functions (add, subtract, multiply, divide) from one “combined” function. Display results in this function (why is this?). Do not call the add, subtract, multiply, divide functions separately in the main. Call them from one function (“function calling other functions”)
    2. Change the values WITHOUT quitting the application
  1. Allow the user to run this application until they decide to quit.

Example Output:

1 of 2 - Enter First Number: 12.34

2 of 2 - Enter Second Number: 8.4

1 - Add (12.34 + 8.4)

2 - Subtract (12.34 - 8.4)

3 - Multiplication (12.34 X 8.4)

4 - Divide (12.34 / 8.4)

5 – ALL

6 – Change Values

7 - Quit

        Enter an Option (1 to 7): 3

12.34 X 8.4 = 103.656

Hit Any Key to continue...

1 - Add (2.4 + -3.4)

2 - Subtract (2.4 - -3.4)

3 - Multiplication (2.4 X -3.4)

4 - Divide (2.4 / -3.4)

5 - ALL

6 - Quit

        Enter an Option (1 to 7): 5

2.4 + -3.4 = -1

2.4 - -3.4 = 5.8

-3.4 - 2.4 = -5.8

2.4 X -3.4 = -8.16

2.4 / -3.4 = -0.705882

Hit Any Key to continue...

Solutions

Expert Solution

Code

#include<iostream>
using namespace std;
void menu(double n1,double n2)
{
   cout<<"\n1 - Add ( "<<n1<<" + "<<n2<<" )"<<endl;
   cout<<"2 - Subtract ( "<<n1<<" - "<<n2<<" )"<<endl;
   cout<<"3 - Multiplication ( "<<n1<<" X "<<n2<<" )"<<endl;
   cout<<"4 - Divide ( "<<n1<<" / "<<n2<<" )"<<endl;
   cout<<"5 - ALL"<<endl;
   cout<<"6 - Change Values"<<endl;
   cout<<"7 - Quit"<<endl;
   cout<<"\tEnter an Option (1 to 7):";
}
double Add(double n1,double n2)
{
   return n1+n2;
}
double Sub(double n1,double n2)
{
   return n1-n2;
}
double Mul(double n1,double n2)
{
   return n1*n2;
}
double Div(double n1,double n2)
{
   if(n2==0)
   {
       cout<<"Cannot divide by zero"<<endl;
       n2=1;
   }
   return n1/n2;
}
int main()
{
   double num1,num2;

   cout<<"1 of 2 - Enter First Number: ";
   cin>>num1;
   cout<<"2 of 2 - Enter First Number: ";
   cin>>num2;
   int choice;
   while (true)
   {
       menu(num1,num2);
       cin>>choice;
       cout<<endl;
       if(choice==1)
       {
           cout<<num1<<" + "<<num2<<" = "<<Add(num1,num2)<<endl;
       }
       else if(choice==2)
       {
           cout<<num1<<" - "<<num2<<" = "<<Sub(num1,num2)<<endl;
       }
       else if(choice==3)
       {
           cout<<num1<<" * "<<num2<<" = "<<Mul(num1,num2)<<endl;
       }
       else if(choice==4)
       {
           cout<<num1<<" / "<<num2<<" = "<<Div(num1,num2)<<endl;
       }
       else if(choice==5)
       {
           cout<<num1<<" + "<<num2<<" = "<<Add(num1,num2)<<endl;
           cout<<num1<<" - "<<num2<<" = "<<Sub(num1,num2)<<endl;
           cout<<num1<<" * "<<num2<<" = "<<Mul(num1,num2)<<endl;
           cout<<num1<<" / "<<num2<<" = "<<Div(num1,num2)<<endl;
       }
       else if(choice==6)
       {
           cout<<"1 of 2 - Enter First Number: ";
           cin>>num1;
           cout<<"2 of 2 - Enter First Number: ";
           cin>>num2;
       }
       else if(choice==7)
           break;
       else
           cout<<"Invalid choice\n";
   }
   return 1;
}

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Please answer with a written response rather than calculations: -describe conservation of momentum in elastic AND...
Please answer with a written response rather than calculations: -describe conservation of momentum in elastic AND inelastic collisions -describe the path of the center of mass for two colliding objects -explain the relationship between conservation of energy in a collision and coefficient of restitution -explain conversions between kinetic, gravitational potential, and magnetic potential energy thanks
why is 3-sulfolene rather than 1,3-butadiene itself to be used as a source of the diene?
why is 3-sulfolene rather than 1,3-butadiene itself to be used as a source of the diene?
Two firms, A and B, produce complementary products. That is, rather than competing for the same...
Two firms, A and B, produce complementary products. That is, rather than competing for the same customers, demand for each firm’s output increases if the other firm produces more. Specifically, the inverse demand for Firm A’s product is given by pA = 110 − qA + qB. The inverse demand for Firm B’s product is given by pB = 50 − qB + qA. Both firms have marginal cost of 10 and choose quantities to maximize their profit. 5. If...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of computers sold by three vendors in four different zones. Is required:  Request the sale amount of each seller by zone, the values must be entered through the keyboard and validate that it does not accept negative numbers.  Menu that requests the operation to be carried out. In case of entering an invalid data, send an error message and request the operation again.  Option...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained...
In a C language program CODE BLOCKS !!!!!!!! Program that stores the number of votes obtained by 3 amounts in five different zones. Is required: + Request the total of votes by zone of each candidate, the values must be entered on the keyboard and validate that it does not accept negative numbers + Menu that requests the operation to be performed, in case of entering an invalid data send an error message and request the operation again + Option...
Carefully work through the sample calculations for this lab ON PAPER. Then, set up the same...
Carefully work through the sample calculations for this lab ON PAPER. Then, set up the same equations on a separate sheet of paper, but replace the values in the ‘Sample Calculations’ folder with the ones provided below and complete the calculations: Mass of Zinc = 0.0928 g H2 volume reading from the eudiometer tube = 35.98 mL Water bath temperature = 19.9 °C Barometric pressure = 768.2 mm Hg Level (pressure) difference from the meter stick = 41.20 cm H2O...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes...
Programming language: C++   suggested software: Code::Blocks Develop an algorithm and write a C++ program that computes the final score of a baseball game. Use a loop to read the number of runs scored by both teams during each of nine innings. Display the final score afterward. Submit your design, code, and execution result via file, if possible
I want the code below to be edited: Rather than giving the input string inside the...
I want the code below to be edited: Rather than giving the input string inside the code, I want the program to ask the user for an input and calculate and complete this code. I have pasted the actual code below, Please edit the input section only so that I can input any string or any sentence as I like. The program must ask the user that "Enter a string/sentence" and take the data to calculate the Huffman code. #include...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment,...
Source code with comments explaining your code in C# Program 2: Buh-RING IT! For this assignment, you’re going to simulate a text-based Role-Playing Game (RPG). Design (pseudocode) and implement (source) for a program that reads in 1) the hero’s Hit Points (HP – or health), 2) the maximum damage the hero does per attack, 3) the monster’s HP and 4) the maximum monster’s damage per attack. When the player attacks, it will pick a random number between 0 and the...
I am a believer in individuality rather than any outside source for my illness. I do...
I am a believer in individuality rather than any outside source for my illness. I do a lot of reading about health and am aware of the different ways in which I can fall sick. Yet I know that science cannot answer all the questions regarding the cause of illness. I believe in the power of God and power of karma but I think illness has many reasons beyond it. Bad eating habits especially the addiction to junk food has...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT