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...
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...
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   ...
Part B) Write a C++ program which prompts user to choose from the two alternative funtions...
Part B) Write a C++ program which prompts user to choose from the two alternative funtions specified below, each of which passes a random number between 1 and 4 and returns a random message. The two functions are: Function messageByValue that passes a random value between 1 and 4 and returns the message by value and Function messageByReference that passes a random value between 1 and 4 and returns the message by reference. Each function returns one of four random...
Write a complete C++ program that prompts the user for and takes as input, numbers until...
Write a complete C++ program that prompts the user for and takes as input, numbers until the user types in a negative number. the program should add all of the numbers together. Then if the result is less than 20 the program should multiply the result by 3, otherwise subtract 2 from the result. Finally, the program should printout the result.
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit...
In Java: Write a program called F2C that allows the user to convert from degrees Fahrenheit to degrees Celsius. The program should prompt for a temperature in Fahrenheit and output a temperature in Celsius. All calculations should be done in in ints, so be careful of truncation.
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT