Need Help in Arrays

Write a program that demonstrates various gradebook utilities. The program should first create a
two-dimensional array initialized with short values, where each value is a grade in the range 0 through 100.
Each row in this array should be treated as all the grades for a particular student, and each column in this array
should be treated as all the grades for a particular assignment. Then the program should demonstrate using
that array with each of the following functions:
● printGradeScores: This function should accept a two-dimensional array as its argument and print the
contents of the array to the screen as numeric grades in a report formatted like this:
○ Student #1: 74 59 91 11 94
○ Student #2: 48 67 4 68 92
○ Student #3: 92 71 83 80 100
● printGradeLetters: This function should accept a two-dimensional array as its argument and print the
contents of the array to the screen as letter grades in a report formatted like this:
○ Student #1: C F A F A
○ Student #2: F D F D A
○ Student #3: A C B B A
where A=90-100, B=80-89, C=70-79, D=60-69, F=0-59
● getStudentTotalScores: This function should accept a two-dimensional array and a student
number(valid values start at 1) as its arguments and return the sum of all this student's scores.
● getStudentAverageScore: This function should accept a two-dimensional array and a student
number(valid values start at 1) as its arguments and return the average assignment score for this
student.
● getStudentAverageLetter: This function should accept a two-dimensional array and a student
number(valid values start at 1) as its arguments and return the average assignment letter grade for this
student, using the letter grade scale above(see printGradeLetters).
● getAssignmentTotalScores: This function should accept a two-dimensional array and an assignment
number(valid values start at 1) as its arguments and return the sum of all scores earned for the
assignment.
● getAssignmentAverageScore: This function should accept a two-dimensional array and an
assignment number(valid values start at 1) as its arguments and return the average of all scores
earned for the assignment.
● getAssignmentMaxScore: This function should accept a two-dimensional array and an assignment
number(valid values start at 1) as its arguments and return the maximum score earned for the
assignment.
● getAssignmentMinScore: This function should accept a two-dimensional array and an assignment
number(valid values start at 1) as its arguments and return the minimum score earned for the
assignment.
Please don't double (or triple) post.

You cannot just give us your whole homework description and expect us to write it for you, but if you have made an attempt and have a specific question we might be able to help you with that.
Last edited on
here is my code so far but function is not working and giving error

#include<iostream>
#include<string>

using namespace std;

int getStudentTotalScore(short [][COLS], int, int);

short ROWS =3;
short COLS =5;

int main()
{
short table[ROWS][COLS] = {{74,59,91,11,94},{48,67,4,68,92},{92,71,83,80,100}};
cout<<" The total is: "<< getStudentTotalScore(table , ROWS, COLS)<<endl;
return 0;
}

// Student Total Score
int getStudentTotalScore(short table[][COLS], int rows, int cols)
{
int total=0;
for(int row=0; row<3; row++)
total+=int table[][5];
return total;
}
Hello rahan2121,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.

You should compile your program before posting here. This way you have a chance to fix any errors and ask about any you do not know how to fix or understand.

Here is your program with some partial fixes and comments about what needs fixed so you can see what is wrong. Be sure to read the comments.

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
#include<iostream>
#include<string>

using namespace std;

constexpr short ROWS = 3;  // <--- Need to be defined before the first use. Also must be a constant.
constexpr short COLS = 5;

int getStudentTotalScore(short[][COLS], int, int);

int main()
{
	short table[ROWS][COLS] =  // <--- The "=" is not really needed just the {}s.
	{
		{74,59,91,11,94},
		{48,67,4,68,92},
		{92,71,83,80,100}
	};

	cout << " The total is: " << getStudentTotalScore(table, ROWS, COLS) << endl;
	return 0;
}

// Student Total Score
int getStudentTotalScore(short table[][COLS], int rows, int cols)  
// <--- Since ROW and COL are global variables they do not need to be sent to the function.
{
	int total = 0;
	for (int row = 0; row < 3; row++)
		total += int table[][5];  // <--- the "int' is not needed here. The first dimension of the subscript needs a value.
	return total;
}

In main when you define the table the size of the dimensions need to be a constant value at compile time so the compiler will know how much space to set aside for the array. This could be either a variable defined as a constant or a number like 3 and 5.

The way I set up the "table" definition is just a better visual representation of what you are working with. There is nothing wrong with what you did. From C++11 on the "=" is not needed just the {}s.

In line 20. You are sending the function "ROWS" and "COLS". Since these are defined as global variables the whole file has access to them. You do not need to send them to the function just use them.

In the "getStudentTotalScore" function it does not work for several reasons:

The "int" infront of "table" is not needed. This was done in the function definition. Also the "table" comes into the function as a "short", but you are trying to redefine it as an "int". Why? All you need to do is use the "table".

Next you have a 2D "table". You need a way to give the subscripts both row and column numbers. Generally this is done with nested for loops where the outer loop controls the row and the inner loop controls the column. You have the right concept for the function just going about it the wrong way.

Give it some thought and post what you come up with.

Hope that helps,

Andy
Topic archived. No new replies allowed.