This is for C++ Visual Studio Community. Could someone help me Please use as a r
ID: 3875312 • Letter: T
Question
This is for C++ Visual Studio Community. Could someone help me
Please use as a reference Programming Challenge #1 "Sum Of Numbers" of your textbook on page 297. 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
Answer:
code:
#include<iostream.h>
#include<conio.h>
//The displayMenu() function here outputs the Program's Menu to the user and asks for the user's choice. It then returns the user's choice back to main function.
int displayMenu()
{
int ch;
cout<<"Input your choice :"<<endl<<"1.Summation."<<endl<<" 2.Factorial"<<endl<<"3.Exponentiation"<<endl<<"4.Thank you "<<endl;
cin>>ch;
return(ch);
}
/*The Sum() function accepts a number 'maxno' from main(). This number is the number upto which summation is to be carried out. By deploying a simple for() loop,
we accomplish the desired function of summation of the natural numbers upto the max number. It then displays the sum and returns back the output to the menu.*/
void sum(int maxno)
{
int sum=0;
if(maxno!=0)
{
for(int i=1; i<=maxno; i++)
sum+=i;
cout<<" The sum of natural numbers upto the given max number "<<maxno<<" is: "<<sum;
}
else
{
cout<<" Enter max number >0";
}
}
/*The function fact() calculates the factorial of a number 'num' passed from main() by deploying recursive function calling.
It returns a 'long' type result to main()*/
long fact(int num)
{
if(num==1 || num==0)
return(1);
else
return(num*fact(num-1));
}
/* The function expo() raises a number to the desired exponentiation and returns the exponentiated number.
The exponentiation is done by recursively calling the function*/
long expo(int num, int ex)
{
if(ex==0)
return(1);
else
return(num*expo(num,ex-1));
}
/* Start of main() function */
void main()
{
clrscr();
int choice,num, lim;
do
{
choice=displayMenu();
switch(choice)
{
case 1:
{
cout<<" Enter the maximum number upto which summation should be carried:(<20000): "<<endl;
cin>>num;
sum(num);
getch();
clrscr();
break;
}
case 2:
{
cout<<" Enter the number whose factorial is to be calculated:"<<endl;
cin>>num;
cout<<" The factorial of "<<num<<" is: "<<fact(num);
getch();
clrscr();
break;
}
case 3:
{
cout<<" Enter the number whose exponentiation is to be calculated:"<<endl;
cin>>num;
cout<<" Enter the power to which "<<num<<" has to be raised:"<<endl;
cin>>lim;
cout<<" The number "<<num<<" raised to "<<lim<<" results in:"<<expo(num,lim);
getch();
clrscr();
break;
}
case 4:
{
cout<<"Thank you";
break;
}
default:
{
cout<<"Wrong choice";
break;
}
}
}while(choice!=4);
}
Please provide your valuable feedback.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.