Rewrite homework 5. You will need at least 4 functions: menu, summation, factori
ID: 3857343 • Letter: R
Question
Rewrite homework 5. You will need at least 4 functions: menu, summation, factorial, and exponential.
Make sure that your header block shows that you have functions and how information is passed between them. Each function should have it's own header block. A header block is comprised of several lines of comments at the top of each function that explain what the function does. See program 6-28 in your chapter for examples.
Think about the scope of your variables. Variables used only inside of the function should be declared locally to that function. No global variables, please. Return "choice" from displayMenu function. And, (for example) if you have a counter variable down in Summation, then declare it locally to Summation.
Make the menu display and resultant output look very clean and professional.
Same validation requirements as when you submitted for Chapter 5.
Here is my code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Constants for menu choices
const int SUM = 1,
FACTORIAL = 2,
EXPONENTIAL = 3,
QUIT = 4;
//Variables
int choice, input;
long double num = 1, value = 1, sum = 0;
do
{
//Display menu of arithmetic functions
cout << " Arithmetic Functions "
<< "1. Summation "
<< "2. Factorial "
<< "3. Exponential "
<< "4. Akkkkkkk No more math functions please ;) "
<< "Enter your choice: ";
cin >> choice;
//validate the menu selection
while (choice < SUM || choice > QUIT)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//process the user's choice
if (choice != QUIT)
{
//ask user to input a positive integer
cout << "Please enter any positive integer: ";
cin >> input;
while (input < 1) //input validation of positive integer
{
cout << " " << input << " is not a positive integer. ";
cout << "Please enter a positive integer: ";
cin >> input;
}
//Respond to the user's menu selection
switch (choice)
{
case SUM:
for (int counter = 1; counter <= input; counter++)
{
sum += counter;
}
//Display result
cout << " The sum of numbers 1 - " << input
<< " " << "is: " << sum << endl;
sum = 0; // resets variable
break;
case FACTORIAL:
for (int counter = 1; counter <= input; counter++)
{
value *= counter;
}
//Display result
cout << " The value of " << input << " factorial is: ";
cout << value << endl;
value = 1; //resets variable
break;
case EXPONENTIAL:
for (int counter = num; counter <= input; counter++)
{
num *= 2;
}
//Display result
cout << " The result is base 2 raised to the power of " << input;
cout << ": ";
cout << num << endl;
num = 1; //resets variable
break;
}
}
else
{
cout << " The program has ended. Goodbye. ";
}
} while (choice != QUIT);
return 0;
}
Explanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
void menu()
{
//Display menu of arithmetic functions
cout << " Arithmetic Functions "
<< "1. Summation "
<< "2. Factorial "
<< "3. Exponential "
<< "4. Akkkkkkk No more math functions please ;) "
<< "Enter your choice: ";
}
long double summation(int input)
{
long double sum = 0;
for (int counter = 1; counter <= input; counter++)
{
sum += counter;
}
return sum;
}
long double factorial(int input)
{
long double value = 1;
for (int counter = 1; counter <= input; counter++)
{
value *= counter;
}
return value;
}
long double exponential(int input)
{
long double num = 1;
for (int counter = num; counter <= input; counter++)
{
num *= 2;
}
return num;
}
int main()
{
//Constants for menu choices
const int SUM = 1,
FACTORIAL = 2,
EXPONENTIAL = 3,
QUIT = 4;
//Variables
int choice, input;
long double num = 1, value = 1, sum = 0;
do
{
menu();
cin >> choice;
//validate the menu selection
while (choice < SUM || choice > QUIT)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}
//process the user's choice
if (choice != QUIT)
{
//ask user to input a positive integer
cout << "Please enter any positive integer: ";
cin >> input;
while (input < 1) //input validation of positive integer
{
cout << " " << input << " is not a positive integer. ";
cout << "Please enter a positive integer: ";
cin >> input;
}
//Respond to the user's menu selection
switch (choice)
{
case SUM:
//Display result
cout << " The sum of numbers 1 - " << input
<< " " << "is: " << summation(input) << endl;
sum = 0; // resets variable
break;
case FACTORIAL:
//Display result
cout << " The value of " << input << " factorial is: ";
cout << factorial(input) << endl;
value = 1; //resets variable
break;
case EXPONENTIAL:
//Display result
cout << " The result is base 2 raised to the power of " << input;
cout << ": ";
cout << exponential(input)<< endl;
num = 1; //resets variable
break;
}
}
else
{
cout << " The program has ended. Goodbye. ";
}
} while (choice != QUIT);
return 0;
}
Output:
Arithmetic Functions
1. Summation
2. Factorial
3. Exponential
4. Akkkkkkk No more math functions please ;)
Enter your choice:1 Please enter any positive integer: 6
The sum of numbers 1 - 6 is: 21
Arithmetic Functions
1. Summation
2. Factorial
3. Exponential
4. Akkkkkkk No more math functions please ;)
Enter your choice: 2 Please enter any positive integer: 3
The value of 3 factorial is: 6
Arithmetic Functions
1. Summation
2. Factorial
3. Exponential
4. Akkkkkkk No more math functions please ;)
Enter your choice: 3 Please enter any positive integer: 7
The result is base 2 raised to the power of 7: 128
Arithmetic Functions
1. Summation
2. Factorial
3. Exponential
4. Akkkkkkk No more math functions please ;)
Enter your choice:4
The program has ended. Goodbye.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.