Hi guys, I have a homework where I create a file and place grades such as A B C D and F; meant to stand for the students grades. I create a program where I read the values from the file and store them into a 2D array.
So far I have done that part, but then I am suppose to calculate the GPA of the student's grades.
I don't know how to change my char array from holding 'A' to holding the value 4.
#include <iostream>
#include <fstream>
usingnamespace std;
constint ROWS = 5;
constint COLS = 3;
void showGrades(char studentsGrades[][COLS]);
int main()
{
char studentsGrades[ROWS][COLS];
showGrades(studentsGrades);
return 0;
}
void showGrades(char studentsGrades[][COLS])
{
ifstream inFile;
inFile.open("C:\\Users\\Karen\\Documents\\Visual Studio 2013\\Projects\\HW2\\HW2\\grades.dat");
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
inFile >> studentsGrades[i][j];
}
}
cout << "All grades: \n\n";
cout << "Student \t" << "English \t" << "History \t" << "Math \n";
for (int i = 0; i < ROWS; i++)
{
cout <<"#" << i+1 ;
for (int j = 0; j < COLS; j++)
{
cout <<"\t\t"<< studentsGrades[i][j];
}
cout << endl;
}
}
Output:
All grades:
Student English History Math
#1 A A B
#2 C C F
#3 C D B
#4 B A C
#5 B A B
Press any key to continue . . .
A simple way would just be to have variable to accumulate the gpa and use a switch on the extracted letter grade. Case 'A' would add 4 to the accumulator, etc. Then average the accumulation. Do that for each student. I.E. keep an array of floats equal to the amount of students then add to the corresponding element.
I personally would go a totally different route and use a more OO approach, but I'm assuming this is homework so I'll leave it at that. Hope I helped.
float accumulator = 0.0f;
float studentGPAs[*]; //* represents number of students
//...
//do for each student;
for(int i = 0; i < *; i++) //* represents number of students
{
for(int i = 0; i < *; i ++) //* represents however many grades a student can have
{
switch(c) //char extracted from file
{
case'A':
case'a':
accumulator += 4.0f;
break;
//do same for 'B', 'C', 'D', 'F'
}
}
studentGPA[i] = accumulator / *; //* represents number of grades.
accumulator = 0.0f; //set the accumulator back to 0.0f so we start fresh for the new student
}
Note that you can only switch on an integral type. So make sure that whatever you extract is a single character.