Question

In: Computer Science

Build and present a menu to a user like the following and enclose it into a...

  1. Build and present a menu to a user like the following and enclose it into a loop that ends when the Quit option is chosen. Scan the user's selection into an integer variable.
    1.  Enter user name.
    2.  Enter test scores.
    3.  Display average.
    4.  Display summary.
    5.  Quit.
    Selection:
  2. If the user selects Option #1, scan the user's name, and store it to a char array. Assume the user's name is under 20 characters.
  3. If the user selects Option #2, use a for-loop to scan 3 exam scores into a float array. Calculate the average and store the result in a float variable. Assume all scores are out of 100 points.  
  4. If the user selects Option #3, Display the average of the test scores. If the user has not yet entered test scores, display an error message similar to: "Please use the menu to enter test scores first"

    Hint: Use boolean values to keep track of the options that have been selected.
  5. If the user selects Option #4, display the average, the letter grade of the average, and the user's name. If the user has not yet entered their name or test scores, display an error message.

    Example: "Hello Charley, your test scores were 80, 90, and 100. Your average is 90.0 with letter grade: A."
    Example: "Please use the menu to enter your name first."

    Example: "Please use the menu to enter your test scores first."
  6. When the Quit option is chosen, end the primary loop that contains the menu.

c++ please

for while do while


could you write a c program please?

Solutions

Expert Solution

NOTE: If you have any query regarding the solution, please ask in comment section! HAPPY LEARNING!!

There will not be too much difference in the code for language c and c++, but still I have provided the code in both language for better understanding, and the code is done in the same way as in problem statement explained.

CODE (C++):

#include<iostream>
using namespace std;


void display_average(float scores[]){                   // FUNCTION FOR DISPLAY AVERAGE
    float sum = 0.0;
    for(int i=0;i<3;i++)
        sum += scores[i];                               // ADDED ALL THE SCORES AND DIVIDED BY NUMBER OF SCORES TO CALCULATE AVERAGE

    float average = sum/3;
    cout<<"Your average score is "<<average<<endl;      // PRINTED THE AVERAGE

}
void display_summary(char name[],float scores[]){       // FUNCTION FOR DISPLAY SUMMARY

    float sum = 0.0;
    for(int i=0;i<3;i++)
        sum += scores[i];

    float average = sum/3;                              // CALCULATED THE AVERAGE IN THE SAME WAY AS ABOVE
    char grade;
    if (average>=90)                                    // CALCULATED THE GRADE ACCORDING TO THE AVERAGE CALCULATED
        grade = 'A';
    else if (average>=80)
        grade = 'B';
    else if (average>=70)
        grade = 'C';
    else if (average>=60)
        grade = 'D';
    else
        grade = 'F';
                                                        // PRINTED THE SUMMARY AS SPECIFIED IN THE PROBELEM DESCRIPTION

    cout<<"Hello "<<name<<", your test scores were "<<scores[0]<<", "<<scores[1]<<" and "<<scores[2]<<". Your average is "<<average<<" with letter grade: "<<grade<<"."<<endl;


}

int main(){                                             // MAIN FUNCTION
    char name[20];
    float test_scores[3];                               // TEST SCORES ARRAY OF SIZE 3 TO STORE THE SCORES OF 3 SUBJECTS
    bool name_entered =false, scores_entered = false;   // BOOLEAN VARIABLES FOR THE PROGRAM TO KNOW IF USER HAS ENTERED THE NAME AND SCORES OR NOT
    int choice;

    while (1){
        cout<<"\n1. "<<"Enter User Name."<<endl;        // DISPLAY THE MENU
        cout<<"2. "<<"Enter Test Scores."<<endl;
        cout<<"3. "<<"Display Average."<<endl;
        cout<<"4. "<<"Display Summary."<<endl;
        cout<<"5. "<<"Quit."<<endl;
        cout<<"\nEnter your choice: ";
        cin>>choice;

        switch(choice){                                 // GO TO THE CASE BY SWITCH AS THE CHOICE ENTERED
            case 1:
                cin>>name;                              // TAKE THE NAME INPUT FROM USER AND ASSIGN TRUE TO THE BOOLEAN VARIABLE NAME_ENTERED
                name_entered = true;
                break;
            case 2:
                for(int i=0;i<3;i++)
                    cin>>test_scores[i];                // TAKE THE SCORES OF THE THREE SUBJECTS AND ASSIGN TRUE TO THE BOOLEAN VARIABLE SCORES ENTERED
                scores_entered = true;
                break;
            case 3:
                if (!scores_entered)                    // IF SCORES NOT ENTERED DISPLAY ERROR ELSE CALL THE DISPLAY AVERAGE FUNCTION
                    cout<<"Please use the menu to enter test scores first"<<endl;
                else
                    display_average(test_scores);
                break;
            case 4:
                if(!name_entered)
                    cout<<"Please use the menu to enter your name first"<<endl;     // IF NAME OR SCORES OR BOTH NOT ENTERED DISPLAY THE ERROR ELSE CALL THE DISPLAY SUMMARY FUNCTION
                if(!scores_entered)
                    cout<<"Please use the menu to enter test scores first"<<endl;
                if(name_entered && scores_entered)
                    display_summary(name,test_scores);
                break;
            case 5:
                exit(0);
            default:
                cout<<"Invalid choice!!! please try again..."<<endl;           // IF CHOICE IS INVALID DISPLAY ERROR


        }
    }
    return 0;
}

CODE (C):

#include<stdio.h>
#include<stdlib.h>
void display_average(float scores[]){
    float sum = 0.0,average;
    int i;
    for(i=0;i<3;i++)
        sum += scores[i];

    average = sum/3;
    printf("Your average score is %.2f\n",average);

}
void display_summary(char name[],float scores[]){

    float sum = 0.0,average;
    int i;
    char grade;
    for(i=0;i<3;i++)
        sum += scores[i];

    average = sum/3;
    if (average>=90)
        grade = 'A';
    else if (average>=80)
        grade = 'B';
    else if (average>=70)
        grade = 'C';
    else if (average>=60)
        grade = 'D';
    else
        grade = 'F';

    printf("Hello %s, your test scores were %.1f, %.1f and %.1f. Your average is %.2f with letter grade: %c\n",name,scores[0],scores[1],scores[2],average,grade);


}

int main(){
    char name[20];
    float test_scores[3];
    int name_entered = 0, scores_entered = 0;           // IN C LANGUAGE BOOLEAN IS NOT DEFINED SO TOOK INTEGER VALUE 0 FOR FALSE AND 1 FOR TRUE
    int choice,i;

    while (1){
        printf("\n1. Enter User Name.");
        printf("\n2. Enter Test Scores.");
        printf("\n3. Display Average.");
        printf("\n4. Display Summary.");
        printf("\n5. Quit.\n");
        printf("\nEnter your choice: ");
        scanf("%d",&choice);

        switch(choice){
            case 1:
                scanf("%s",name);
                name_entered = 1;
                break;
            case 2:
                for(i=0;i<3;i++)
                    scanf("%f",&test_scores[i]);
                scores_entered = 1;
                break;
            case 3:
                if (!scores_entered)
                    printf("\nPlease use the menu to enter test scores first\n");
                else
                    display_average(test_scores);
                break;
            case 4:
                if(!name_entered)
                    printf("\nPlease use the menu to enter your name first");
                if(!scores_entered)
                    printf("\nPlease use the menu to enter test scores first\n");
                if(name_entered && scores_entered)
                    display_summary(name,test_scores);
                break;
            case 5:
                exit(0);
            default:
                printf("\nInvalid choice!!! please try again...\n");


        }
    }
    return 0;
}

OUTPUT SCREENSHOT( INPUT INCLUDED) :


Related Solutions

print a menu so that the user can then order from that menu. The user will...
print a menu so that the user can then order from that menu. The user will enter each item they want until they are done with the order. At the end you will print the items the user ordered with their price and the total. Note: You will be storing your menu items (pizza, burger, hotdog, salad) and your prices (10, 7, 4, 8) in separate lists. Menu 0) pizza $10 1) burger $7 2) hotdog $4 3) salad $8...
You must prompt the user to enter a menu selection. The menu will have the following...
You must prompt the user to enter a menu selection. The menu will have the following selections (NOTE: all menu selections by the user should not be case sensitive): 1. ‘L’ – Length a. Assume that a file containing a series of names is called names.txt and exists on the computer’s disk. Read that file and display the length of each name in the file to the screen. Each name’s middle character or characters should be displayed on a line...
C++ CODE Change your code from Part A to now present a menu to the user...
C++ CODE Change your code from Part A to now present a menu to the user asking them for an operation to perform on the text that was input. You must include the following functions in your code exactly as provided. Additionally, you must have function prototypes and place them above your main function, and the function definitions should be placed below the main function. Your program should gracefully exit if the user inputs the character q or sends the...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user...
Write a program names EncryptDecrypt.java that has the following menu choices: Print menu and allow user to choose options. The program must have a file dialogue box for text file. Output should be based on user choices. Read in a file Print the file to the console Encrypt the file and write it to the console Write out the encrypted file to a text file Clear the data in memory Read in an encrypted file Decrypt the file Write out...
ABC Daycare wants to build a fence to enclose a rectangular playground. The area of the...
ABC Daycare wants to build a fence to enclose a rectangular playground. The area of the playground is 920 square feet. The fence along three of the sides costs $5 per foot and the fence along the fourth side, which will be made of brick, costs $15 per foot. Find the length of the brick fence that will minimize the cost of enclosing the playground. (Round your answer to one decimal place.)
T5: CASH signed  a contract with a local carpenter to build a fence to enclose the backyard....
T5: CASH signed  a contract with a local carpenter to build a fence to enclose the backyard. The carpenter charges $250 per hour and expects the fence to take 10 hours to complete. He will begin work next month. Explain the impact of transaction 5 on the Fundamental Equation of Accounting. That is what assets, liabilities and/or net assets are affected by the transaction. Be sure to include the dollar amount involved. Your answer can be descriptive and need not be...
In Python, write a calculator that will give the user the following menu options: 1) Add...
In Python, write a calculator that will give the user the following menu options: 1) Add 2) Subtract 3) Multiply 4) Divide 5) Exit Your program should continue asking until the user chooses 5. If user chooses to exit give a goodbye message. After the user selections options 1 - 4, prompt the user for two numbers. Perform the requested mathematical operation on those two numbers. Round the result to 1 decimal place. Example 1: Let's calculate! 1) Add 2)...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a program to prompt the user to display the following menu: Sort             Matrix                   Q
Write a program to prompt the user to display the following menu: Sort             Matrix                   Quit If the user selects ‘S’ or ‘s’, then prompt the user to ask how many numbers you wish to read. Then based on that fill out the elements of one dimensional array with integer numbers. Then sort the numbers and print the original and sorted numbers in ascending order side by side. How many numbers: 6 Original numbers:                     Sorted numbers 34                                                                         2          55                                                      ...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide...
Write a calculator program that prompts the user with the following menu: Add Subtract Multiply Divide Power Root Modulus Upon receiving the user's selection, prompt the user for two numeric values and print the corresponding solution based on the user's menu selection. Ask the user if they would like to use the calculator again. If yes, display the calculator menu. otherwise exit the program. EXAMPLE PROGRAM EXECUTION: Add Subtract Multiply Divide Power Root Modulus Please enter the number of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT