For C++ Question: Use as a reference Programming Challenge #1 \"Sum Of Numbers\"
ID: 3875352 • Letter: F
Question
For C++Question: Use as a reference Programming Challenge #1 "Sum Of Numbers" Sum of Numbers: Write a... Use as a reference Programming Challenge #1 "Sum Of Numbers"
Sum of Numbers:
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, … 50. Input Validation: Do not accept a negative starting number.2. Characters for the ASCII Codes Write a program that uses a loop to display the characters for the ASCII codes.
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 its 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 what you submitted for previous assignments such as invalid selection etc.
Pseudocode for the main function might look like this:
main()
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
case 5
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()
output the menu
input the user's choice
return choice
-------pseudocode for function summation
void function summation()
prompt for summation maxNumber
input maxNumber
while (maxNum <0 OR maxNum > TOOBIG)
output error message
input maxNumber
end while
etc.....
For C++
Question: Use as a reference Programming Challenge #1 "Sum Of Numbers" Sum of Numbers: Write a... Use as a reference Programming Challenge #1 "Sum Of Numbers"
Sum of Numbers:
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, … 50. Input Validation: Do not accept a negative starting number.2. Characters for the ASCII Codes Write a program that uses a loop to display the characters for the ASCII codes.
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 its 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 what you submitted for previous assignments such as invalid selection etc.
Pseudocode for the main function might look like this:
main()
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
case 5
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()
output the menu
input the user's choice
return choice
-------pseudocode for function summation
void function summation()
prompt for summation maxNumber
input maxNumber
while (maxNum <0 OR maxNum > TOOBIG)
output error message
input maxNumber
end while
etc.....
For C++
Question: Use as a reference Programming Challenge #1 "Sum Of Numbers" Sum of Numbers: Write a... Use as a reference Programming Challenge #1 "Sum Of Numbers"
Sum of Numbers:
Write a program that asks the user for a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1, 2, 3, 4, … 50. Input Validation: Do not accept a negative starting number.2. Characters for the ASCII Codes Write a program that uses a loop to display the characters for the ASCII codes.
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 its 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 what you submitted for previous assignments such as invalid selection etc.
Pseudocode for the main function might look like this:
main()
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
case 5
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()
output the menu
input the user's choice
return choice
-------pseudocode for function summation
void function summation()
prompt for summation maxNumber
input maxNumber
while (maxNum <0 OR maxNum > TOOBIG)
output error message
input maxNumber
end while
etc.....
Explanation / Answer
C++ code:-
#include<iostream>
#include<math.h>
using namespace std;
//This function reads user's choice and returns that choice.
int displayMenu()
{
int ch;
cout<<"1.Summation 2.factorial 3.exponential 4.exit from program ";
cout<<"enter choice:";
cin>>ch;
return ch;
}
//This function reads user's max number and gives summation
void summation()
{
int maxNum,i,sum=0;
cout<<"enter max number for summation:";
cin>>maxNum;
if(maxNum < 0 || maxNum>32767)
cout<<"number is negative or greater than max value.";
else
{
for(i=1;i<=maxNum;i++)
sum = sum + i;
cout<<"summation of max number is:"<<sum;
}
}
//This function reads user's max number and gives factorial
void factorial()
{
int maxNum,fac=1,i;
cout<<"Enter number to get factorial value:";
cin>>maxNum;
if(maxNum < 0 || maxNum>32767)
cout<<"number is negative or greater than max value.";
else
{
for(i=1;i<=maxNum;i++)
fac = fac * i ;
cout<<"factorial of max number is:"<<fac;
}
}
//This function reads uder's max number and gives exponential
void exponential()
{
int maxNum;
double expo;
cout<<"Enter number to get exponential value:";
cin>>maxNum;
if(maxNum < 0 || maxNum>32767)
cout<<"number is negative or greater than max value.";
else
{
expo = exp(maxNum);
cout<<"exponential value is:"<<expo;
}
}
int main()
{
int ch;
do
{
ch = displayMenu();
if(ch >= 5 || ch<=0)
ch = 5;
switch(ch)
{
case 1: summation();
break;
case 2: factorial();
break;
case 3: exponential ();
break;
case 4: cout<<"Good bye";
exit(0);
break;
case 5: cout<<"entered invalid choice,choose among 1 to 4";
}
}while(ch != 4);
return 0;
}
Expected output:-
1.Summation 2.factorial 3.exponential 4.exit from program
enter choice: 1
enter max number for summation: 5
summation of max number is: 15
1.Summation 2.factorial 3.exponential 4.exit from program
enter choice: 2
Enter number to get factorial value: 5
factorial of max number is: 120
1.Summation 2.factorial 3.exponential 4.exit from program
enter choice: 3
Enter number to get exponential value: 5
exponential value is: 148.413
1.Summation 2.factorial 3.exponential 4.exit from program
enter choice: 6
entered invalid choice, choose among 1 - 4.
1.Summation 2.factorial 3.exponential 4.exit from program
enter choice: 4
Good bye
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.