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

c++ Rewrite Chapter 5 Programming Assignment(provided at bottom of the page) so

ID: 3705031 • Letter: C

Question

c++

Rewrite Chapter 5 Programming Assignment(provided at bottom of the page) so that it utilizes functions. You will need at least 4 functions that are called by main: menu, summation, factorial, and exponential.

The functions operate as follows:

int menu() displays the menu and prompts for the user's choice and returns that choice.

void summation() prompts the user for their integer and then does the summation and the displays the result. It is passed nothing and returns nothing.

void factorial() is same as summation

void exponential is same as summation - make sure to use for loops to do the calculations and not pow or exp.

Each function should have it's own header block. It should state the name of the function, the purpose of the function and what it is passed or what it returns. These header blocks are created using comment lines within your code.

Rule #1: Absolutely no global variables. Variables used only inside of a function should be declared locally to that function.   For example, if you have a counter variable down in Summation, then declare it locally to Summation. OR, if main() needs to know what choice the user made from the menu, then the function menu() returns that value. No global variables under any circumstances.

Same input validation requirements as when you submitted for Chapter 5.

Pseudocode for the main function might look like this: (I underlined a few side comments for you - but they are not part of the p-code.)

main()

int choice;   

do

choice = displayMenu (this function will return the user's choice)

switch on choice

case 1

call function to handle summation

case 2

call function to handle factorial

case 3

call function to handle exponential

case 4

output goodbye statement

default

handle incorrect choice (ie, choice not 1-4)

end switch

while (choice not equal to 4)

end main

--------pseudocode for displaymenu function

int function displayMenu()

int userChoice //this userChoice is local to displayMenu function

output the menu

input the userChoice

return userChoice   //here userChoice is being returned to main()

-------pseudocode for function summation

void function summation() //this function is passed nothing and returns nothing

declare total and maxNumber

initialize total

prompt for maxNumber

input maxNumber

while (maxNumber <0)

output error message

input maxNumber

end while

for (counter = 1; counter <=maxNumber; counter++)

total = total + counter

end for loop

ouptut total, maxNumber...

return

------------

You need to supply the rest of the pseudocode when you submit your assignment.

#include "stdafx.h" #include

Explanation / Answer

#include <iostream>

using namespace std;

void showChoices();

float add(float, float);

float exp(float);

int main()

{

int x, y;

int choice;

do

{

showChoices();

cin >> choice;

switch (choice)

{

case 1:

{

cout << "Enter two numbers: ";

cin >> x >> y;

cout << "Summation " << add(x,y) <<endl;

}

break;

case 2:

{

cout << "Enter a number: ";

unsigned long long factorial = 1;

cin >> x;

for(int i = 1; i <=x; ++i)

{

factorial *= i;

}

cout << "Factorial " << factorial <<endl;

}

break;

case 3:

{

cout << "Enter a number: ";

cin >> x;

cout << "Exponential " << exp(x) <<endl;

}

break;

case 4:

{

cout << "Good bye: ";

}

break;

case 5:

break;

default:

cout << "Invalid input" << endl;

}

}while (choice != 5);

return 0;

}

void showChoices()

{

cout << "MENU" << endl;

cout << "1: Summation " << endl;

cout << "2: Factorial" << endl;

cout << "3: Exponential " << endl;

cout << "4: Good bye " << endl;

cout << "5: Exit " << endl;

cout << "Enter your choice :";

}

float add(float a, float b)

{

return a + b;

}

float exp(float a)

{

float base, result = 1;

while(a != 0) {

result *= base;

--a;

}

return result;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote