-2

I am currently working on a program for c++ class that will process and generate a student transcript for a semester assuming that the student is taking five classes.(Output is printed to the screen in a chart format). The student will enter three grades for each course. There should be one array for the course names, first exam grades, second exam grades, and third exam grades.

Alright, fair enough. Well I have managed to initialize the above arrays and output them correctly in the below code. Here is where I am a bit confused. Next, I have to initialize an array that will calculate the average of the three exam grades for each course and initialize an array for the letter grades for the averages of each course.(and print them to the screen in the same fashion as the exam grades and course names.) Perhaps if somebody could shed light upon how to create an array that will calculate the average of the three grades, I could try and figure out how to make an array for the letter grades. Thank you.

# include iostream

using namespace std;
const int SIZE =5;

void getData(string courseName[], float examOne[], float examTwo[], float examThree[]);


int main ()
{
    // local declaration
    string courseName[SIZE];
    float examOne[SIZE];
    float examTwo[SIZE];
    float examThree[SIZE];

    getData(courseName,examOne,examTwo,examThree);


    cout<<"\n\n\n";

    for (int i = 0; i <SIZE; i++){
        cout<< courseName[i]<<"    "<<examOne[i]<<"    "<<examTwo[i]<<"     "<<examThree[i]<<"   "<<total[i]<< endl;

    }  
    return 0;
}



void getData(string courseName[],float examOne[],float examTwo[],float examThree[]){

    for (int i = 0; i <SIZE; i++){
    cout<<"Enter Course Name: "; 
    cin >> courseName[i];
    cout<<"enter First Exam Grade: ";
    cin>>examOne[i];
    cout<<"enter Second Exam Grade: ";
    cin>>examTwo[i];
    cout<<"enter Third Exam Grade: ";
    cin>>examThree[i];

    }   
}
red
  • 17
  • 2
  • 8
  • Why don't you just use the arrays you store the data in, create a sum variable and then divide the sum by the size of the array? In this case it would be 3 –  Dec 01 '16 at 01:38
  • 3
    Arrays don't calculate. Code does. Arrays store data. You cannot create *an array that will calculate the average*, because arrays can't do math. – Ken White Dec 01 '16 at 01:40
  • This is true, Thank you – red Dec 01 '16 at 02:30

1 Answers1

0

EDIT:

I add a function to display students’ average grades as letters.

using namespace std; is considered bad practice and can be dangerous sometimes. Check this. Declaring SIZE as globale variable is also considered bad practice.

#include <iostream>
#include <string>

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[]);
std::string letterGrade(int grade);
void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam);

int main()
{
    // local declaration
    const int SIZE = 5;
    std::string courseName[SIZE];
    float examOne[SIZE];
    float examTwo[SIZE];
    float examThree[SIZE];
    float average[SIZE];

    getData(SIZE, courseName, examOne, examTwo, examThree, average);

    std::cout << "\n\n\n";

    for (int i = 0; i <SIZE; i++)
    {
        std::cout << courseName[i] << "    " << examOne[i] << "    " << examTwo[i] << "     "
            << examThree[i] << "   " << average[i] << "   " << letterGrade(average[i]) << std::endl;
    }
    std::cout << "\n";

    float highestGradeExam = 0;
    float lowestGradeExam = 0;
    float averageGradeExam = 0;

    highestLowestAverageByExam(examOne, SIZE, highestGradeExam, lowestGradeExam, averageGradeExam);
    std::cout << "Exam number 1:  Highest grade = " << highestGradeExam
        << "  Lowest grade = " << lowestGradeExam
        << "  Average grade = " << averageGradeExam << std::endl;


    return 0;
}

void getData(int sizeArray, std::string courseName[], float examOne[], float examTwo[], float examThree[], float average[])
{
    for (int i = 0; i < sizeArray; i++)
    {
        int count = 0;
        std::cout << "Enter Course Name: ";
        std::cin >> courseName[i];

        std::cout << "enter First Exam Grade: ";
        std::cin >> examOne[i];
        count++;

        std::cout << "enter Second Exam Grade: ";
        std::cin >> examTwo[i];
        count++;

        std::cout << "enter Third Exam Grade: ";
        std::cin >> examThree[i];
        count++;

        average[i] = (examOne[i] + examTwo[i] + examThree[i]) / count;
    }
}


std::string letterGrade(int averageGrade)
{
    if (averageGrade >= 0 && averageGrade <= 100)
    {
        const int NUMBER_CATEGORIES = 11;
        const std::string GRADE_LETTER[] = { "F", "D", "D+", "C-", "C", "C+", "B-", "B", "B+", "A-", "A" };
        const int LOWEST_GRADE_SCORE[] = { 0, 60, 67, 70, 73, 77, 80, 83, 87, 90, 93 };
        int category = 0;
        while (category < NUMBER_CATEGORIES && LOWEST_GRADE_SCORE[category] <= averageGrade)
        {
            category++;
        }
        return GRADE_LETTER[category - 1];
    }
    else
    {
        return "Average grade is not valid.";
    }
}

void highestLowestAverageByExam(float tExamArray[], int tSize, float & tHighestGradeExam, float & tLowestGradeExam, float & tAverageGradeExam)
{
    tHighestGradeExam = tExamArray[0];
    tLowestGradeExam = tExamArray[0];
    for (int i = 1; i < tSize; i++)
    {
        if (tExamArray[i] > tHighestGradeExam)
        {
            tHighestGradeExam = tExamArray[i];
        }
        if (tExamArray[i] < tLowestGradeExam)
        {
            tLowestGradeExam = tExamArray[i];
        }
    }
    tAverageGradeExam = (tHighestGradeExam + tLowestGradeExam) / 2;
}
Community
  • 1
  • 1
yahiheb
  • 147
  • 9
  • 1
    Thank you, This really helps clarify my code. I see how you initialized a count before the input of course names to divide the total count by the courses. – red Dec 01 '16 at 03:55
  • You are welcome. I edited my answer and I added a function to display students’ average grades as letters. – yahiheb Dec 01 '16 at 13:23
  • Hello again, I ran the code you had edited and it runs flawlessly as expected. Although, I am still trying to figure out how it exactly works. Bear with me,I am rather new to c++ and had just started arrays in the textbook/class. Why do you place category within the array LOWEST_GRADE_SCORE and put that <= grade...... is int grade set to 0 as well because I do not see int grade any where else in the code besides the function, or does it not need to be?.....why is one subtracted from category in the return type;--->return GRADE_LETTER[category - 1]; If this does not make sense I am sorry. – red Dec 02 '16 at 07:50
  • The letterGrade() function is used to display students’ average grades as letters, and int grade is a parameter that you pass to this function, in your case it represents any average of the courses.(I changed int grade to int averageGrade for clarity) for example let's say you have the average of the math course (int averageMath = 82) you should pass this variable like this: letterGrade(averageMath); to get the the letter grade. (this returns (B-) because 82 exceeds 80 and it is less than 83) – yahiheb Dec 02 '16 at 17:29
  • With the arrays GRADE_LETTER[] and LOWEST_GRADE_SCORE[] in place, we initialize category to 0 and search through the LOWEST_GRADE_SCORE array, stopping when the lowest grade score exceeds the averageGrade (that we passed) or when we run out of categories. In either case, when the loop is done, category will be in range 1–11 based on the averageGrade, and this is why we subtract 1 from category because the index of our array is in range 0–10. – yahiheb Dec 02 '16 at 17:30
  • Let's say (averageGrade = 5) this should have ofcourse the letter F, when the while loop stops category will be equal to 1, so if we return GRADE_LETTER[category] without subtracting 1 the function would return the letter D wich is not correct. PS: I add an if statement to handle the case if the user enters an averageGrade less than 0. And if you find this answer helpful you can accept it and up vote it. Thanks – yahiheb Dec 02 '16 at 17:30