Question

In: Computer Science

Write a C++ program which consists of several functions besides the main() function. The main() function,...

Write a C++ program which consists of several functions besides the main() function.

  1. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface.
  2. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference.
  3. A value-returning function called Power(int a, int b) that computes a raised to the b power. Design and implement your own power function using an iterative control structure, or even recursion. Do not simply write a wrapper around the C++ function called pow().
  4. There should be a user loop and a menu so that the user can select either SumProductDifference, Power, or Quit. Then ProcessCommand() should ask the user for two values to compute SumProductDifference or Power. ProcessCommand() should then also output the answer.

I've got the below one. Can someone help me improve it absolutely as the requirement? I am not good at user loop and menu function. Can you help me add that? And please help me put main function to the top. When I want to put it to the top, the project can't run. I don't know what's the wrong with that.


// ===================
#include <iostream>   
using namespace std;
// ===================

// ==========================================================================
void SumProductDifference(int x, int y, int& sum, int& product, int& diff){
  
sum = x + y;
product = x * y;
diff = x - y;
} // Function SumProductDifference ()
// ====================================
  

// =========================
int power(int a, int b) {

int r = 1;
for (int i = 1; i <= b; i++)
r = r * a;

return r;

} // Function power ()
// ======================

// =====================================
void ProcessCommand(int& x, int& y) {
  
cout << "Enter two numbers :";
cin >> x >> y;

} // Function ProcessCommand()
// ==============================

// ============
int main() {

int x, y, sum, product, diff;
ProcessCommand(x, y);
SumProductDifference(x, y, sum, product, diff);
cout << "Sum = " << sum << "\nProduct= " << product << "\nDifference =" << diff;
cout << "\nPower of " << x << "to " << y << " is :" << power(x, y);

return 0;

} // Function main()
// =====================

Solutions

Expert Solution

Hi,
Please find the answer below:
----------------------------------------------------

To bring the main function to the top, we need to add the function prototypes used in the program
at the top.

//Function prototypes
void SumProductDifference(int , int , int& , int& , int& );
int power(int , int );
void ProcessCommand(int& , int& );

If we dont add the prototypes , we will get errors in the project.

error: 'ProcessCommand' was not declared in this scope|


Fixed C++ Program

// ===================
#include <iostream>
using namespace std;
// ===================

//Function prototypes
void SumProductDifference(int, int, int&, int&, int& );
int power(int, int );
void ProcessCommand(int&, int& );

// Function main()
// =====================
int main()
{
    int x, y, sum, product, diff;
    int selection;

    do
    {
        cout<<"\n\n ------ Program Menu ------"<<endl;
        cout<<"1: SumProductDifference" << endl;
        cout<<"2: Power" << endl;
        cout<<"3: Quit" << endl;
        cout<<"Enter your selection: ";
        cin>>selection;
        if(selection == 1)
        {
            ProcessCommand(x, y);
            SumProductDifference(x, y, sum, product, diff);
            cout << "Sum = " << sum << "\nProduct= " << product << "\nDifference =" << diff;
        }
        else if(selection ==2)
        {
            ProcessCommand(x, y);
            cout << "\nPower of " << x << "to " << y << " is :" << power(x, y);
        }
        else
        {
            //do nothing - Quit option
        }
    }
    while((selection >= 1) && (selection <= 2));

    return 0;
}

// Function SumProductDifference ()
// ==========================================================================
void SumProductDifference(int x, int y, int& sum, int& product, int& diff)
{
    sum = x + y;
    product = x * y;
    diff = x - y;
}


// Function power ()
// =========================
int power(int a, int b)
{
    int r = 1;
    for (int i = 1; i <= b; i++)
        r = r * a;
    return r;
}

// Function ProcessCommand()
// =====================================
void ProcessCommand(int& x, int& y)
{
    cout << "Enter two numbers :";
    cin >> x >> y;
}


Sample Output:


------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 1
Enter two numbers :2 3
Sum = 5
Product= 6
Difference =-1

------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 2
Enter two numbers :3 4

Power of 3to 4 is :81

------ Program Menu ------
1: SumProductDifference
2: Power
3: Quit
Enter your selection: 3

Screenshot

------------------------------------------------------
Hope this helps.
Kindly like the solution if it is useful to you.
Let me know if you need more help.
Thanks.


Related Solutions

Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main,...
1G. This program, unlike the previous 6 programs, will have several functions in it: besides main, it willhave the following 7 functions:public static int triangle( int n )public static int multiply( int a, int b )public static void square( int size )public static void hollowSquare( int size )public static int factorial( int n )public static long fibonacci( int n )public static boolean prime( long n )These 7 functions should work as described below.Your main program should call each of these...
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of...
In C Write a main function with a function call to a function called GetLetter which...
In C Write a main function with a function call to a function called GetLetter which has two double arguments/parameters. The function returns a character. Declare and initialize the necessary data variables and assign values needed to make it executable and to prevent a loss of information
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what...
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what are the parameters? findMax()- what is the return, what are the parameters? findMin()-what is the return, what are the parameters? find()- should return the index of the element or a -1 indicating not found The main function will call those methods and print the results of each. 1 // declare necessary variables 2 // declare array 3 double numbers[SIZE]; 4 // Function prototypes 5...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT