Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: To implement functions in the C++ programing language and to decompos

ID: 3808991 • Letter: O

Question

Objective: To implement functions in the C++ programing language and to decompose a program into logica function blocks. To further our understanding of loops, their logic and the correct use of the whilel, do while() and forcontrol structures in the C++ programming language. Specifically to show an understanding of the logically appropriate use of nested loops and embedded conditional statements. To reinforce our understanding of data types and variables. Namely, how to create, initialize, display and perform basic arithmetic operations on those variables. Assignment: Write a program that simulates a simple calculator to perform basic arithmetic computations. Th program should support the following operations: Addition Subtraction Multiplication Division The program should continue to prompt the user to enter an equation in the form of a b a-b a*b where a represents the first operand, l+, represents the operation addition, subtraction, multiplication and division respectively and b represents the second operand. Each time a valid equation is entered, the equation should be computed and the results of the computation should be displayed also in the form of an equation (i.e. 5*4 20). The program should continue requesting equations to compute until the user enters some sentinal value or sentinal equation. Example: -999 as the first operand or txx as the operation, etc.

Explanation / Answer

void calc (int x, char op,int y);

double result;

double n1,n2;

char q,operation;

int main()               

{

   cout<<"Input an equation" <<endl;            

   cout<<"To quit, select 'q'"<<endl;

   cin>>n1>>operation>>n2;

   calc(n1,operation,n2);

   cout<<"The answer is:"<<result<<endl;

   std::cin>>q;

   return 0;}

void calc(int x,char op,int y)

    {            x=n1;

                 y=n2;

                 op=operation;

    switch(operation)                                                          

    {case '+':

       int addition(int ,int );

        break;

    case '-':

       int subtraction(int int);

    case '*':

int multiplication(int int);

break;

    case '/':

double division(int ,int);

  break;

default:

        cout<<"Improper equation. Please input a valid mathmatical equation"<<endl;

        cin>>n1>>operation>>n2;

        calc (n1,n2);

    }

void int addition(int x,int y)

{

x=n1;

y=n2;

result=x+y;

cout<<result;

}

void int subtraction(int x,int y)

{

x=n1;

y=n2;

result=x-y;

cout<<result;

}

void int multiplication(int x,int y)

{

x=n1;

y=n2;

result=x*y;

cout<<result;

}

void double(int x,int y)

{

x=n1;

y=n2;

result=x/y;

cout<<result;

}

}