Answer to Question #349995 in C++ for Babu

Question #349995

Write a program to calculate students’ average test scores and their grades. You may assume the following input data:


Nondo 85 83 77 91 76


Djiara 80 90 95 93 48


Anna 78 81 11 90 73


Salum 92 83 30 69 87


Moloko 23 45 96 38 59


Job 60 85 45 39 67


Feisal 77 31 52 74 83


Kibwana 93 94 89 77 97


Shomari 79 85 28 93 82


Mayele 85 72 49 75 63


Use three arrays: a one-dimensional array to store the students’ names, a (parallel) two-dimensional array to store the test scores, and a parallel one-dimensional array to store grades. Your program must contain at least the following features: read and store data into two arrays, calculate the average test score and grade, and output the results. Have your program also output the class average.

1
Expert's answer
2022-06-13T08:27:02-0400
#include <iostream>
#include <string>
#include <vector>
using namespace std;

constexpr auto numOfTESTS = 5;

int addStudents(string* names, int (&scores)[100][numOfTESTS], int& numOfstudents);

int main(int argc, char* argv[]) {
    string names[100];
    int scores[100][numOfTESTS]{};
    int averages[100]{};
    char grades[100];
    int class_average = 0, numOfstudents = 0;

    if (argc > 1) {
        for (int i = 0; i < (argc-1)/6; i++) {
            names[i] = argv[i * 6 + 1];
            for (int j = 0; j < 5; j++) {
                scores[i][j] = stoi(argv[i * 6 + j + 2]);
            }
            numOfstudents++;
        }
    } else {
        addStudents(names, scores, numOfstudents);
    }

    for (int i = 0; i < numOfstudents; i++) {
        averages[i] = (scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3] + scores[i][4]) / 5;
        class_average += averages[i];

        if (averages[i] > 89) {
            grades[i] = 'A';
        } else if (averages[i] > 79) {
            grades[i] = 'B';
        } else if (averages[i] > 69) {
            grades[i] = 'C';
        } else if (averages[i] > 59) {
            grades[i] = 'D';
        } else {
            grades[i] = 'F';
        }

        cout << "Student name: " << names[i] << "\n";
        cout << "Scores: " << scores[i][0] << " " << scores[i][1] << " " << scores[i][2] << " " << scores[i][3] << " " << scores[i][4] << "\n";
        cout << "Average score: " << averages[i] << "\n";
        cout << "Grade: " << grades[i] << "\n\n";
    }
    class_average /= numOfstudents;
    cout << "Class average score: " << class_average << endl;

    return 0;
}

int addStudents(string* names, int(&scores)[100][numOfTESTS], int& numOfstudents) {
    vector<string> students;
    string data, more = "No";
    do {
        cout << "Enter sudent name and scores: \n";
        getline(cin, data);
        students.push_back(data);
        cout << "Do you want add one more student? (Type \"Yes\" for add one more) ";
        getline(cin, more);
        numOfstudents++;
    } while (more == "Yes" || more == "yes" || more == "YES");
    cout << endl;
    for (int i = 0; i < students.size(); i++) {
        size_t start = 0, end = students[i].find(" ");
        names[i] = students[i].substr(start, end);
        for (int j = 0; j < numOfTESTS; j++) {
            scores[i][j] = 0;
        }
        for (int j = 0; j < numOfTESTS; j++) {
            start = end + 1;
            end = students[i].find(" ", end + 1);
            scores[i][j] = stoi(students[i].substr(start, end));
        }
    }
    return numOfstudents;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS