Question

In: Computer Science

write a complete C++ program that allows the user to choose from one of several options....

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.

Solutions

Expert Solution

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 :)


Related Solutions

Write a C++ program that allows a user choose item to purchase from a list; ask...
Write a C++ program that allows a user choose item to purchase from a list; ask for the amount to calculate the cost of the purchase; offer to add a tip; add a delivery fee based on the subtotal; then calculate the total amount that the user needs to pay. ---------------------------------- ----- GROCERY SHOPPING ITEMS ----- Milk     - $5.99 / gallon Egg      - $6.99 / dozen Cheese   – $10.98 / 8oz Pasta    – $2.75 / packet ---------------------------------- Other Values to...
PYTHON (BEGINNER) program that allows the user to choose any of the three sports options described...
PYTHON (BEGINNER) program that allows the user to choose any of the three sports options described below and computes the relevant statistic in each case: Quidditch Score Total: Determined based on the number of goals and whether or not the snitch was caught. A goal is scored by propelling the quaffle through a hoop and each earns the team 10 points. If a team catches the snitch, that team earns an additional 30 points. The snitch can be caught at...
C++ Vector Write a program that allows the user to enter the last names of the...
C++ Vector Write a program that allows the user to enter the last names of the candidates in a local election and the votes received by each candidate. The program should then output each candidate's name, votes received by that candidate, and the percentage of the total votes received by the candidate. Assume a user enters a candidate's name more than once and assume that two or more candidates receive the same number of votes. Your program should output the...
Write a C++ program to choose one of the following five options for your summer vacation...
Write a C++ program to choose one of the following five options for your summer vacation Hawaii Bahamas Cancun Las Vegas Europe Your program should select one of the above option. Once the option is selected then your program should select one of the following airlines: US Air Delta Southwest Continental American Airline Once the airline is selected then read number of passengers airfare for round trip. Calculate the total charge. make your own charges
3. Write a C++ program to choose one of the following five options for your summer...
3. Write a C++ program to choose one of the following five options for your summer vacation 1. Hawaii 2. Bahamas 3. Cancun 4. Las Vegas 5. Europe Your program should select one of the above option. Once the option is selected then your program should select one of the following airlines: 1. US Air 2. Delta 3. Southwest 4. Continental 5. American Airline Once the airline is selected then read number of passengers airfare for round trip. Calculate the...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
Write an interactive program in c++ that allows the user toenter 2-15 vectors (use the...
Write an interactive program in c++ that allows the user to enter 2-15 vectors (use the if statement). Display the resultant on the screen.
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the...
DATA STRUCTURES USING C++ 2ND EDITION Write a program that allows the user to enter the last names of five candidates in a local election and the votes received by that candidate. The program should then output each candidates name, votes received by that candidate, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. A sample output is as follow Johnson    5000 25.91 miller    4000   ...
Please code C# 10. Write a program that allows a user to input names and corresponding...
Please code C# 10. Write a program that allows a user to input names and corresponding heights (assumed to be in inches). The user can enter an indefinite number of names and heights. After each entry, prompt the user whether they want to continue. If the user enters true, ask for the next name and height. If the user enters false, display the name of the tallest individual and their height. Sample run: “Name?” James “Height?” 50 “Continue?” True “Name?”...
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT