2- Dimensional Arrays for grading

Good Afternoon,

I'm having trouble with a assignment that deals with 2-Dimensional Arrays that should output the letter grade for every students from an input file. So far I have this code, the program runs but it only outputting a letter grade of B to every student. I think the problem is in the function grading. I really appreciate any help that I can get.

CPP File:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

#include "lab_20_head.h" 

using namespace std; 

int main( )
{
	//define a const array size
	const int SIZE = 35;		//class size				
	const int COLUMN = 11;		//column size

	//declare five parallel arrays for firstName, lastName, and ID
	string firstNames[SIZE],	         //firstnames for students
		   lastNames[SIZE],	//last names for students
		   IDs[SIZE];		//IDs for students
	double scores[SIZE];		//scores for students
	char   grades[SIZE];		//letter grades

	//declare two dim array for class info
	double csci1370[SIZE][COLUMN]; //for att, quiz, hw, tests and score

	//file var
	ifstream inFile;			//input file

	int row, col;			//loop var

	//open file
	inFile.open("lab_20_data.txt"); 
	if(!inFile)
	{
		cout<<"Fail to open lab_20_data.txt, and stop ."<<endl;
		return 1; 
	}

	//initialize array letterStata
	loadData(inFile, firstNames, lastNames, IDs, csci1370, SIZE, COLUMN);

	//close inFile
	inFile.close(); 

	//compute score and grades
	grading(csci1370, scores, grades, SIZE, COLUMN); 

	//show grades
	showGrades(firstNames, lastNames, IDs, csci1370, scores, grades, SIZE, COLUMN); 
	
	//well done and exit
	return 0; 
}


HEADER FILE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

#include <iostream>
#include <cstring>
#include <string>

using namespace std; 

#ifndef		LAB_20_HEAD_H		
#define		LAB_20_HEAD_H



/********************************************************************
 This function gets class information from the input file and store
 the info to three parallel array and one two dim array.
 inFile: input file
 firstNames, lastNames, IDs: three output parallel arrays
 csci1370: two dim output array
 SIZE, COLUMN: two input parameters
 *********************************************************************/
void loadData(ifstream & inFile, 
		string firstNames[], string lastNames[ ], string IDs[ ], 
		double csci1370[][11], const int SIZE, const int COLUMN)
{
	
	char  str[1024]; 
	//skip the first three lines
	inFile.getline(str, 1080, '\n'); 
	cout<<str<<endl; 
	inFile.getline(str, 1080, '\n'); 
	cout<<str <<endl;
	inFile.getline(str, 1080, '\n'); 
	cout<< str<<endl; 

	for (int row = 0; row < SIZE; row++)
	{
		 inFile>> firstNames[row]		//get first name
				>> lastNames[row]	//get last name
				>> IDs[row];	//get ID

		for (int col = 0; col <COLUMN; col++)
		{
			inFile >> csci1370[row][col];//get a row for csci1370
		}
	}
}

/*********************************************************************
 This function computes scores for each student and decides
 his/her grade. The formula to calculate the scores of attendance 5%, 
 quiz 5%, homework 30%, tests 60%. The grade is decided as follows:
 A if score >= 90, B if 80<= score <90, C if 70<= score < 80, 
 D if 60 <= score <70, and F otherwise.
 *********************************************************************/
void grading(const double csci1370[][11],			//input array
			 double scores[],		//output array for scores
			 char grades[],		//output array for grades
			 const int SIZE, const int COLUMN)//input parameters
{
	
	int average,total;
	for (int row =0; row < SIZE; row++)
	{
		total = 0;
		for (int COLUMN = 0; COLUMN < SIZE; COLUMN++)
		{
			double attendance, quiz, homework, tests;
			double hw1, hw2, hw3, hw4, hw5, hw6, T1, T2, T3;
			attendance = attendance * .05;
			quiz = quiz * .05;
			homework = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6) / 6;
			homework = homework * .30;
			tests = (T1 + T2 + T3) / 3;
			tests = tests * .60;
			average = (attendance + quiz + homework + tests) / 4;
			average += scores[row][COLUMN];
			
			
			if (scores[row] >= 90)
			{
				grades[row] = 'A';
			}
			else if (scores[row] <= 80)
			{
				grades[row] = 'B';
			}
			else if (scores[row] <= 70)
			{
				grades[row] = 'C';
			}
			else if (scores[row] <= 60)
			{
				grades[row] = 'D';
			}
			else 
				grades[row] = 'F';
		}
	}

}

/****************************************************************
 This function displays class information for CSCI 1370 
 in some nice format.
 ****************************************************************/
void showGrades(const string firstNames[], 
				const string lastNames[], 
				const string IDs[], 
				const double csci1370[][11], 
				const double scores[], 
				const char grades[], 
				const int SIZE, const int COLUMN)
{
	
	for (int i = 0; i < SIZE; i++)
	{
		cout<<firstNames[i]
			<<lastNames[i]
			<<IDs[i]
			<<grades<<endl;
}


#endif 


DATA FILE:

-------------------------------------------------------------------
firt last ID att hw1 hw2 hw3 hw4 hw5 hw6 T1 T2 T3 Quiz
-------------------------------------------------------------------
cat Dog 111111 80 90 80 90 90 100 99 90 89 99 100
Bat Hog 111122 81 97 92 78 89 98 80 99 88 97 100
Tag Tug 111133 67 92 90 50 70 100 79 90 99 94 87
Fat What 111122 84 57 72 68 99 78 60 89 96 90 68
Hat Where 111111 85 60 90 80 70 100 99 90 99 92 87
Tat Who 111122 86 96 92 88 89 78 90 79 96 90 68
Jose Jones 111111 90 93 50 70 90 100 99 90 99 93 90
Jane Smith 111122 71 87 72 68 89 98 90 89 96 80 68
John Hill 111111 70 60 90 90 90 100 99 60 86 89 87
Ann Tor 111122 89 57 72 68 89 98 90 89 87 80 89
Anna Why 111111 88 99 80 50 80 100 79 80 79 89 96
Ben Good 111122 86 99 82 68 89 98 70 99 66 97 98
Benny C++ 111111 70 99 80 90 80 100 69 80 99 93 87
Will PC 111122 91 99 92 68 89 78 80 99 96 96 99
Willy Guess 111111 50 94 80 50 70 100 69 60 80 79 87
Cath Fun 111122 41 94 92 98 89 98 80 89 87 60 68
Cathy Now 111111 86 90 80 50 80 100 69 80 89 89 87
Dash Road 111122 71 95 88 68 89 78 90 89 96 90 68
David Texas 111111 60 95 80 90 70 100 99 90 79 99 89
Eric McAllen 111122 89 87 92 68 89 98 90 89 86 90 88
Eerika Edwin 111111 87 80 96 50 70 100 79 90 89 89 87
Frank Wired 111122 71 87 82 88 99 78 90 69 86 66 68
Ford Joker 111111 90 80 90 50 90 100 89 90 89 99 87
Greg Wind 111122 21 77 87 68 99 98 60 99 89 90 68
George Pang 111111 40 70 90 50 90 100 99 90 99 99 45
Hush Ting 111122 87 97 82 98 99 88 60 99 96 90 68
Hate Pada 111111 60 90 80 50 90 100 79 77 99 99 55
Isee Radar 111122 87 97 82 68 89 78 60 90 96 70 68
Irish Web 111111 83 70 90 90 90 100 99 70 89 89 87
Jeb Food 111122 86 98 88 68 89 68 80 99 86 50 97
Joke Webster 111111 89 91 90 50 70 100 89 70 89 89 84
Kurt Lane 111122 91 90 92 98 99 78 80 89 80 98 88
Kevin City 111122 71 96 92 68 89 98 90 90 96 95 98
Larry Games 111111 84 80 92 90 70 100 99 70 89 99 87
Lucy Park 111122 61 100 99 98 99 98 90 99 86 99 98
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
			if (scores[row] >= 90) // If score is greater than or equal to 90
			{
				grades[row] = 'A';
			}
			else if (scores[row] <= 80) // If score is less than or equal to 80
			{
				grades[row] = 'B';
			}
			else if (scores[row] <= 70) // If score is less than or equal to 70
			{
				grades[row] = 'C';
			}
			else if (scores[row] <= 60)// If score is less than or equal to 60
			{
				grades[row] = 'D';
			}
			else 
				grades[row] = 'F';

Ayou suspected, in the if else you check if the score is below 80 it will be a B, which means that if the score is 69 it will still be a B, you probably meant to use of greater or equal to (>=) rather than less or equal to (<=)
Warnis,

Now, the program is outputting a letter grade of F to every student.
Why is COLUMN = 11? From what I can tell, you only have 10 columns; 6 homeworks, 3 tests, 1 quiz. You'll end up with something weird for scores[row][10] on all the rows. It could even be messing up the input of every student after the 1st one.

Sorry, missed the attendance. Your problem is this stuff:

1
2
3
4
5
6
7
8
9
10
double attendance, quiz, homework, tests;
double hw1, hw2, hw3, hw4, hw5, hw6, T1, T2, T3;
attendance = attendance * .05;
quiz = quiz * .05;
homework = (hw1 + hw2 + hw3 + hw4 + hw5 + hw6) / 6;
homework = homework * .30;
tests = (T1 + T2 + T3) / 3;
tests = tests * .60;
average = (attendance + quiz + homework + tests) / 4;
average += scores[row][COLUMN];


None of these variables are initialized. Besides, they should belong in the outer loop. And I thought scores was a 1 dimensional array?
Last edited on
fg109,

It doesn't run, the error is at average += scores[row][COLUMN];
it error says subscript requires array or pointer type
Did you check what I said? You have scores declared as a one dimensional array:
double scores[SIZE];

You're trying to use it as a two dimensional array:
average += scores[row][COLUMN];

Also, you should check the other stuff I pointed out. All those variables need to be initialized, and it's probably a better idea to have them in the outer loop. Might want to check your math too.
Topic archived. No new replies allowed.