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