Home > Net >  Using function to create a grading system
Using function to create a grading system

Time:10-08

Question: I honestly have no idea how to answer test #1, it only needs one input and will output one when the question is asking the user will input ten (10) consecutive numeric grades that may or may not have a decimal.

#include <iostream>
#include <array>
#include <iomanip>
char getLetterGrade(double);
using namespace std;
int main(){
    const int students_number = 10;
    array<double, students_number> grades;
    array<char, students_number> letters;
    for(double & grade : grades)
        cin >> grade;
    for(size_t i = 0; i < students_number;   i)
        letters[i] = getLetterGrade(grades[i]);
    for(size_t i = 0; i < students_number;   i)
        cout << fixed << setprecision(2) << grades[i] << "=" << letters[i] << endl;
    return 0;
}
char getLetterGrade(double grade){
    if(grade >= 95 && grade <=100) return 'A';
    if(grade > 89 && grade< 95) return 'B';
    if(grade > 84 && grade< 90) return 'C';
    if(grade > 79 && grade< 85) return 'D';
    if(grade > 75 && grade< 80) return 'E';
    if(grade > 40 && grade< 76) return 'F';
    return 'X';
}

Test #1

--- Input ---

-100

--- Program output ---

-100.00=X

0.00=X

0.00=X

0.00=X

0.00=X

0.00=X

0.00=X

0.00=X

0.00=X

0.00=X

--- Expected output (text)---

-100.00=X

CodePudding user response:

"Using function to create a program that validate and evaluate 10 numeric grades to its respective letter grade"

Well as given, the assignment does not require you to compute the average of the grades.

On the other hand, from the code sample given by your teacher, you are expected to define your grade to letter conversion inside a function (here getLetterGrade()) and not directly into the main() function of your program.

For the program itself, as suggested in comments, you better have to use containers (for example std::array) to store the grades and letters.
It would prevent you from having to create 20 variables which is not really convenient and hurts a lot the readability (and maintainability) of your code.

For the method, you may divide your process into 4 steps:

  • Create the containers.
  • Read the grades from user input and store them into the container dedicated to the grades.
  • Iterate over the previous container, call the getLetterGrade() function for each grade and store the result into the container dedicated to the letters.
  • [Optional] Display the letters to validate the program (another iteration)

Possible solution:

#include <iostream>
#include <array>

char getLetterGrade(double);

int main()
{
    const int students_number = 10;

    // Create the arrays (more convenient than creating 20 variables)
    std::array<double, students_number> grades;
    std::array<char, students_number> letters;

    // Read the grades and store them into `grades`
    for(double & grade : grades)
        std::cin >> grade;

    // Deduce the letter for the given grades and store them into `letters`
    for(std::size_t i = 0; i < students_number;   i)
        letters[i] = getLetterGrade(grades[i]);

    // Display the "- [grade]: [letter]"
    for(std::size_t i = 0; i < students_number;   i)
        std::cout << "- " << grades[i] << ": " << letters[i] << '\n';

    return 0;
}

char getLetterGrade(double grade)
{
    if(grade > 94) return 'A';
    if(grade > 89) return 'B';
    if(grade > 84) return 'C';
    if(grade > 79) return 'D';
    if(grade > 74) return 'E';
    if(grade > 40) return 'F';
    return 'G';
}

Note: From this proposal, you just have to change the value of students_number in order to make it work with a different number of students.

Demo

CodePudding user response:

#include <iostream>
#include <array>
#include <iomanip>
char getLetterGrade(double);
using namespace std;
int main(){
    const int students_number = 10;
    array<double, students_number> grades;
    array<char, students_number> letters;
    for(double & grade : grades){
        cin >> grade;
        for(size_t i = 0; i < students_number;   i){
            letters[i] = getLetterGrade(grades[i]);
            cout << fixed << setprecision(2) << grades[i] << "=" << letters[i] << endl;
            if(grade==0){
                break;
            }
        }
    }
    return 0;
}
char getLetterGrade(double grade){
    if(grade >= 95 && grade <=100) return 'A';
    if(grade > 89 && grade< 95) return 'B';
    if(grade > 84 && grade< 90) return 'C';
    if(grade > 79 && grade< 85) return 'D';
    if(grade > 75 && grade< 80) return 'E';
    if(grade > 40 && grade< 76) return 'F';
    return 'X';
    
}

This is the answer to my question, thanks everyone for the help!

  •  Tags:  
  • c
  • Related