In: Computer Science
write a complete C++ program that allows the user to choose from one of several options. The program will display a menu and each option will be in a separate function. The program will continue looping until the user chooses to quit. The module descriptions are given below.
a. Prompt for and take as input 3 floating point values (l, w, and h). The program will then calculate the volume (volume = l*h*w ). The program should then output to the monitor the volume. b. In the main() function prompt for and read in three characters. Then call the function with the three characters as arguments. The function will print the characters out in alphabetic order. c. Prompt for, input and sum a list of numbers until any one of the following conditions is true: the sum exceeds 100.0, a negative value is input, or a maximum of 25 numbers are input. d. In the main() function prompt for and input an integer. Then call the function with the integer as an argument. The function will then add all integers from 1 to the user’s input.
CODE:
#include <bits/stdc++.h>
using namespace std;
//function to calculate the volume of l,h,w;
double calculatevolume(double l, double h, double w)
{
return l*h*w;//multiply all the 3 inputs and return the
value.
}
//to print the 3 characters in alphabetical order.
void printalphabet(char a, char b,char c)
{
//if a<b and a<c then a is the smallest so we print it and
check between b and c.
if(a<b && a<c)
{
cout<<a<<" ";
//if b<c then b is 2nd and c is 3rd else c is 2nd and b is
3rd.
if(b<c)
cout<<b<<" "<<c<<endl;
else
cout<<c<<" "<<b<<endl;
}
//the same method is followed for all the below options.
else if(b<a && b<c)
{
cout<<b<<" ";
if(a<c)
cout<<a<<" "<<c<<endl;
else
cout<<c<<" "<<a<<endl;
}
else
{
cout<<c<<" ";
if(a<b)
cout<<a<<" "<<b<<endl;
else
cout<<b<<" "<<a<<endl;
}
}
int calculatesum()
{
int i=0, sum=0,n;
cin>>n;
//we keep inputting numbers until 25 numbers are done, or sum is
greater than 100 or n is negative.
while(i<25 && sum<100 && n>=0)
{
sum=sum+n;//calculate the sum.
cin>>n;
}
return sum;//return the sum.
}
int findsum(int n)
{
int sum=0;
for(int i=1;i<=n;i++) //loop until n.
sum=sum+i; //for each value of i we add it.
return sum;// and finally return the sum.
}
int main()
{
while(1)
{
int option;
cout<<"Choose from the following options:"<<endl;
cout<<"1: Calculate the Volume."<<endl;
cout<<"2: Print 3 characters in alphabetical
order."<<endl;
cout<<"3: Calculate the sum of a list of
integers."<<endl;
cout<<"4: Calculate the sum of numbers upto a number
n."<<endl;
cout<<"5: quit."<<endl;
cin>>option;
if(option==5) //if the option is 5 we exit the loop.
break;
switch(option) //else according to options we proceed.
{
case 1:
double l,h,w;
cout<<"Enter the length: ";
cin>>l;
cout<<"Enter the height: ";
cin>>h;
cout<<"Enter the width: ";
cin>>w;
cout<<"The volume is:
"<<calculatevolume(l,w,h)<<endl;
break;
case 2:
char a,b,c;
cout<<"Enter three characters: "<<endl;
cin>>a>>b>>c;
cout<<"Characters in alphabetical order
are:"<<endl;
printalphabet(a,b,c);
break;
case 3:
cout<<"Enter a list of numbers"<<endl;
cout<<"The sum of the numbers are:
"<<calculatesum()<<endl;
break;
case 4:
int n;
cout<<"Enter a number: ";
cin>>n;
cout<<"The sum of numbers from 1 to n is:
"<<findsum(n)<<endl;
break;
default:
cout<<"Enter a valid option"<<endl;
}
}
}
#include <bits/stdc++.h>
using namespace std;
//function to calculate the volume of l,h,w;
double calculatevolume(double l, double h, double w)
{
return l*h*w;//multiply all the 3 inputs and return the value.
}
//to print the 3 characters in alphabetical order.
void printalphabet(char a, char b,char c)
{
//if a<b and a<c then a is the smallest so we print it and check between b and c.
if(a<b && a<c)
{
cout<<a<<" ";
//if b<c then b is 2nd and c is 3rd else c is 2nd and b is 3rd.
if(b<c)
cout<<b<<" "<<c<<endl;
else
cout<<c<<" "<<b<<endl;
}
//the same method is followed for all the below options.
else if(b<a && b<c)
{
cout<<b<<" ";
if(a<c)
cout<<a<<" "<<c<<endl;
else
cout<<c<<" "<<a<<endl;
}
else
{
cout<<c<<" ";
if(a<b)
cout<<a<<" "<<b<<endl;
else
cout<<b<<" "<<a<<endl;
}
}
int calculatesum()
{
int i=0, sum=0,n;
cin>>n;
//we keep inputting numbers until 25 numbers are done, or sum is greater than 100 or n is negative.
while(i<25 && sum<100 && n>=0)
{
sum=sum+n;//calculate the sum.
cin>>n;
}
return sum;//return the sum.
}
int findsum(int n)
{
int sum=0;
for(int i=1;i<=n;i++) //loop until n.
sum=sum+i; //for each value of i we add it.
return sum;// and finally return the sum.
}
int main()
{
while(1)
{
int option;
cout<<"Choose from the following options:"<<endl;
cout<<"1: Calculate the Volume."<<endl;
cout<<"2: Print 3 characters in alphabetical order."<<endl;
cout<<"3: Calculate the sum of a list of integers."<<endl;
cout<<"4: Calculate the sum of numbers upto a number n."<<endl;
cout<<"5: quit."<<endl;
cin>>option;
if(option==5) //if the option is 5 we exit the loop.
break;
switch(option) //else according to options we proceed.
{
case 1:
double l,h,w;
cout<<"Enter the length: ";
cin>>l;
cout<<"Enter the height: ";
cin>>h;
cout<<"Enter the width: ";
cin>>w;
cout<<"The volume is: "<<calculatevolume(l,w,h)<<endl;
break;
case 2:
char a,b,c;
cout<<"Enter three characters: "<<endl;
cin>>a>>b>>c;
cout<<"Characters in alphabetical order are:"<<endl;
printalphabet(a,b,c);
break;
case 3:
cout<<"Enter a list of numbers"<<endl;
cout<<"The sum of the numbers are: "<<calculatesum()<<endl;
break;
case 4:
int n;
cout<<"Enter a number: ";
cin>>n;
cout<<"The sum of numbers from 1 to n is: "<<findsum(n)<<endl;
break;
default:
cout<<"Enter a valid option"<<endl;
}
}
}
OUTPUT:
You can try outputs for yourself and see for boundary conditions.
If you have any doubts or require any help, do let me know in the comments and i will help you out. Thank you :)