Question

Write a C++ program that will display the following menu and work accordingly. A menu that...

Write a C++ program that will display the following menu and work accordingly. A menu that will display the following choices and uses functions to carry out the calculations: 1. Call a function named classInfo() to ask the user the number of students and exams in a course. 2. Call a function getGrade() that will get the information from classInfo() and asks the user to enter the grades for each student. 3. Call a function courseGrade() to display the average and the letter grade for each student. 4. Exit. Note: You are free to choose the format for the functions, i.e. input-output format. A: 90-100; B: 80-80; C: 70-79; D: 60-69; F: 0-59 Make sure: (i) Follow proper documentation procedure. (ii) Options 2 & 3 can be done only if option 1 was selected before. (iii) You are free to choose your function formats. (iv) Result should be displayed with two decimal place accuracy. (v) Include one sample run with error detection for three students in the class and two exams. (vi) Error detection and correction is needed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

C++ Program:

#include <iostream>

using namespace std;


//Function that reads number of students and exams
void classInfo(int &students, int &exams)
{
//Reading number of students
cout << "\n\n Enter number of students: ";
cin >> students;

//Reading number if exams
cout << "\n\n Enter number of exams: ";
cin >> exams;
}

//Function that allocates memory and reads grades from user
int* getGrade(int *grades, int &students, int &exams)
{
int i, j;

//If number of students were nor read
if(students == 0)
{
//Get number of students and exams
classInfo(students, exams);
}

//Allocating memory
grades = new int*[students];

//Allocating memory for each student
for(i=0; i<students; i++)
{
grades[i] = new int[exams];
}

//Iterating over students
for(i=0; i<students; i++)
{
cout << "\n For, Student " << (i+1) << ": Enter " << exams << " scores: ";

//Iterating over exams
for(j=0; j<exams; j++)
{
//Reading and storing grade
cin >> grades[i][j];
}
}

return grades;
}

//Function that finds the grade based on average
char getGrade(double avg)
{
char grade=' ';

//90-100 -> A
if(avg >= 90 && avg <= 100)
{
grade = 'A';
}
//80-89 -> B
else if(avg >= 80 && avg < 90)
{
grade = 'B';
}
//70-79 -> C
else if(avg >= 70 && avg < 80)
{
grade = 'C';
}
//60-69 -> D
else if(avg >= 60 && avg < 70)
{
grade = 'D';
}
//0-59 -> F
else
{
grade = 'F';
}

return grade;
}

//Function that displays grades
void displayGrades(int **grades, int students, int exams)
{
int i, j;
double sum=0, avg;
char grade;

//Iterating over number of students
for(i=0; i<students; i++)
{
//Printing student number
cout << "\n Student " << (i+1) << ": ";

//Set to 0 in each iteration
sum = 0;

//Iterating over grades
for(j=0; j<exams; j++)
{
//Accumulating sum
sum = sum + grades[i][j];
}

//Calculating average
avg = sum / (double)exams;

//Finding grade based on average
grade = getGrade(avg);

//Printing average and its corresponding grade
cout << " Average: " << avg << " \t Grade: " << grade << " \n";
}
}

//Main function
int main()
{
int ch, i;
int students=0, exams=0;
int **grades;

//Iterate till user wants to quit
while(1)
{
//Displaying menu
cout << "\n\n\t 1 - Get Class Info \n\t 2 - Get Grade \n\t 3 - Display Course Grade \n\t 4 - Exit \n\n\t Your Choice: ";
cin >> ch;

//Calling appropriate functions
switch(ch)
{
case 1: classInfo(students, exams); break;

case 2: grades = getGrade(grades, students, exams); break;

case 3: displayGrades(grades, students, exams); break;

case 4: exit(0);

default: cout << "\n\n Error!!! Invalid choice... \n";
}
}

//Deallocating memory
for(i=0; i<students; i++)
{
delete grades[i];
}

delete[] grades;


cout << "\n\n";
return 0;
}

sample output:

Add a comment
Answer #2

Below is a C++ program that implements the menu with the specified functions for calculating course grades based on user input. The program includes error detection and correction for the number of students and exams entered by the user.

cppCopy code#include <iostream>#include <iomanip>using namespace std;// Function prototypesvoid classInfo(int& numStudents, int& numExams);void getGrade(int numStudents, int numExams, double** grades);char getLetterGrade(double average);void courseGrade(int numStudents, int numExams, double** grades);int main() {    int choice;    int numStudents = 0, numExams = 0;    double** grades = nullptr;    do {
        cout << "Menu:" << endl;
        cout << "1. Enter class information" << endl;
        cout << "2. Enter grades" << endl;
        cout << "3. Display course grades" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice: ";
        cin >> choice;        switch (choice) {            case 1:                classInfo(numStudents, numExams);                // Dynamically allocate memory for grades array
                grades = new double*[numStudents];                for (int i = 0; i < numStudents; i++) {
                    grades[i] = new double[numExams];
                }                break;            case 2:                if (numStudents == 0 || numExams == 0) {
                    cout << "Error: Class information has not been entered yet." << endl;
                } else {                    getGrade(numStudents, numExams, grades);
                }                break;            case 3:                if (numStudents == 0 || numExams == 0) {
                    cout << "Error: Class information has not been entered yet." << endl;
                } else {                    courseGrade(numStudents, numExams, grades);
                }                break;            case 4:
                cout << "Exiting the program. Goodbye!" << endl;                // Deallocate memory for grades array
                if (grades != nullptr) {                    for (int i = 0; i < numStudents; i++) {                        delete[] grades[i];
                    }                    delete[] grades;
                }                break;            default:
                cout << "Invalid choice. Please try again." << endl;
        }

    } while (choice != 4);    return 0;
}void classInfo(int& numStudents, int& numExams) {
    cout << "Enter the number of students: ";
    cin >> numStudents;    while (numStudents <= 0) {
        cout << "Invalid number of students. Please enter a positive integer: ";
        cin >> numStudents;
    }

    cout << "Enter the number of exams: ";
    cin >> numExams;    while (numExams <= 0) {
        cout << "Invalid number of exams. Please enter a positive integer: ";
        cin >> numExams;
    }
}void getGrade(int numStudents, int numExams, double** grades) {
    cout << "Enter the grades for each student:" << endl;    for (int i = 0; i < numStudents; i++) {
        cout << "Student " << i + 1 << ": ";        for (int j = 0; j < numExams; j++) {
            cin >> grades[i][j];            // Validate grade (assume grade is between 0 and 100)
            while (grades[i][j] < 0 || grades[i][j] > 100) {
                cout << "Invalid grade. Please enter a grade between 0 and 100: ";
                cin >> grades[i][j];
            }
        }
    }
}char getLetterGrade(double average) {    if (average >= 90) {        return 'A';
    } else if (average >= 80) {        return 'B';
    } else if (average >= 70) {        return 'C';
    } else if (average >= 60) {        return 'D';
    } else {        return 'F';
    }
}void courseGrade(int numStudents, int numExams, double** grades) {
    cout << "Course grades:" << endl;    for (int i = 0; i < numStudents; i++) {        double total = 0;        for (int j = 0; j < numExams; j++) {
            total += grades[i][j];
        }        double average = total / numExams;        char letterGrade = getLetterGrade(average);
        cout << "Student " << i + 1 << ": Average = " << fixed << setprecision(2) << average << ", Letter Grade = " << letterGrade << endl;
    }
}

This C++ program implements the menu with options to enter class information, enter grades, display course grades, and exit the program. The user can select an option and perform the desired actions accordingly. The program uses functions to carry out the calculations, and error detection is included for the number of students and exams entered by the user.

answered by: Hydra Master
Add a comment
Answer #3

Here's a C++ program that implements the menu and functions as described:

cppCopy code#include <iostream>#include <vector>#include <iomanip>using namespace std;// Global variables to store class informationint numStudents = 0;int numExams = 0;
vector<vector<double>> grades;// Function prototypesvoid classInfo();void getGrade();void courseGrade();int main() {    int choice;    do {        // Display menu
        cout << "Menu:" << endl;
        cout << "1. Enter class information" << endl;
        cout << "2. Enter grades" << endl;
        cout << "3. Display average and letter grade" << endl;
        cout << "4. Exit" << endl;
        cout << "Enter your choice (1-4): ";
        cin >> choice;        switch (choice) {            case 1:                classInfo();                break;            case 2:                if (numStudents == 0 || numExams == 0) {
                    cout << "Please enter class information first." << endl;
                } else {                    getGrade();
                }                break;            case 3:                if (numStudents == 0 || numExams == 0) {
                    cout << "Please enter class information first." << endl;
                } else {                    courseGrade();
                }                break;            case 4:
                cout << "Exiting program. Goodbye!" << endl;                break;            default:
                cout << "Invalid choice. Please try again." << endl;
        }

    } while (choice != 4);    return 0;
}void classInfo() {
    cout << "Enter the number of students in the class: ";
    cin >> numStudents;
    cout << "Enter the number of exams: ";
    cin >> numExams;    // Resize the 2D vector to hold the grades for each student and each exam
    grades.resize(numStudents, vector<double>(numExams, 0));
}void getGrade() {
    cout << "Enter the grades for each student:" << endl;    for (int i = 0; i < numStudents; i++) {
        cout << "Student " << i + 1 << ":" << endl;        for (int j = 0; j < numExams; j++) {
            cout << "Exam " << j + 1 << ": ";
            cin >> grades[i][j];
        }
    }
}void courseGrade() {
    cout << "Course grades:" << endl;    for (int i = 0; i < numStudents; i++) {        double sum = 0;        for (int j = 0; j < numExams; j++) {
            sum += grades[i][j];
        }        double average = sum / numExams;        char letterGrade;        if (average >= 90) {
            letterGrade = 'A';
        } else if (average >= 80) {
            letterGrade = 'B';
        } else if (average >= 70) {
            letterGrade = 'C';
        } else if (average >= 60) {
            letterGrade = 'D';
        } else {
            letterGrade = 'F';
        }

        cout << "Student " << i + 1 << " - Average: " << fixed << setprecision(2) << average << ", Grade: " << letterGrade << endl;
    }
}

Sample run with error detection for three students in the class and two exams:

mathematicaCopy codeMenu:1. Enter class information2. Enter grades3. Display average and letter grade4. ExitEnter your choice (1-4): 1Enter the number of students in the class: 3Enter the number of exams: 2Menu:1. Enter class information2. Enter grades3. Display average and letter grade4. ExitEnter your choice (1-4): 2Enter the grades for each student:Student 1:Exam 1: 85Exam 2: 78Student 2:Exam 1: 92Exam 2: 87Student 3:Exam 1: 75Exam 2: 68Menu:1. Enter class information2. Enter grades3. Display average and letter grade4. ExitEnter your choice (1-4): 3Course grades:Student 1 - Average: 81.50, Grade: BStudent 2 - Average: 89.50, Grade: BStudent 3 - Average: 71.50, Grade: CMenu:1. Enter class information2. Enter grades3. Display average and letter grade4. ExitEnter your choice (1-4): 4Exiting program. Goodbye!

The program allows the user to enter class information, then enter grades for each student, and finally display the average and letter grade for each student. Error detection is included to ensure that options 2 and 3 can only be selected after option 1 has been selected to enter class information.


answered by: Mayre Yıldırım
Add a comment
Know the answer?
Add Answer to:
Write a C++ program that will display the following menu and work accordingly. A menu that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using java Program: Please read the complete prompt before going into coding. Write a program that...

    using java Program: Please read the complete prompt before going into coding. Write a program that handles the gradebook for the instructor. You must use a 2D array when storing the gradebook. The student ID as well as all grades should be of type int. To make the test process easy, generate the grade with random numbers. For this purpose, create a method that asks the user to enter how many students there are in the classroom. Then, in each...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

  • Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve...

    Develop a flowchart and then write a menu-driven C++ program that uses several FUNCTIONS to solve the following program. -Use Microsoft Visual C++ .NET 2010 Professional compiler using default compiler settings. -Use Microsoft Visio 2013 for developing your flowchart. -Adherence to the ANSI C++  required -Do not use <stdio.h> and <conio.h>. -Do not use any #define in your program. -No goto statements allowed. Upon execution of the program, the program displays a menu as shown below and the user is prompted to make a selection from the menu....

  • Write a C++ program that asks user number of students in a class and their names....

    Write a C++ program that asks user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’ names and ranking. Follow the Steps Below Save the project as A4_StudentRanking_yourname....

  • Write a program that asks the user to enter five test scores. The program should display...

    Write a program that asks the user to enter five test scores. The program should display a letter grade for each score and the average test score. Design the following functions in the program: calcAverage—This function should accept five test scores as arguments and return the average of the scores. determineGrade—This function should accept a test score as an argument and return a letter grade for the score (as a String), based on the following grading scale: Score Letter Grade...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels...

    ‘C’ programming language question Write a MENU DRIVEN program to A) Count the number of vowels in the string B) Count the number of consonants in the string C) Convert the string to uppercase D) Convert the string to lowercase E) Display the current string X) Exit the program The program should start with a user prompt to enter a string, and let them type it in. Then the menu would be displayed. User may enter option in small case...

  • Using the following parallel array and array of vectors: // may be declared outside the main...

    Using the following parallel array and array of vectors: // may be declared outside the main function const int NUM_STUDENTS =3; // may only be declared within the main function string Students[NUM_STUDENTS] = {"Tom","Jane","Jo"}; vector <int> grades[NUM_STUDENTS] {{78,98,88,99,77},{62,99,94,85,93}, {73,82,88,85,78}}; Write a C++ program to run a menu-driven program with the following choices: 1) Display the grades 2) Add grade 3) Remove grade for all students for a selected assignment 4) Display Average for each student 5) Display the name of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT