Inputting Data into a 2-D Array

This array is designed to store grade data on 8 projects for n students.
Okay, so I have the array declared as
1
2
3
int Students;
int Projects=8;
int Grades[Students][Projects];

And the input set to perform like this
1
2
3
4
5
6
cin >> Students;
cout << "Please give the number of students: ";
for(int i=1; i<=Projects; i++) {
for(int j=1; j<=Students; j++) {
cout << "Please give grade of student " << j << " in project " << i << ":";
cin >> Grades[j][i];

When I print the values of this array or try to do anything with the data in it, everything screws up, so I'm led to believe that the input is wrong somehow.
Is there a better way to do this?
Last edited on
The problem is, when you declare an array, and you use variables inside the brackets[] they must be consts at initializing.

do

1
2
3
4
5
6
7
8
9
10
const int ROWS = 10;
const int COLS = 10;
int Grades[ROWS][COLS];

int students = 0;
int projects = 8;

cout << "\n\nEnter number of students: "
cin >> students;// I know its prolly just a type but cin after your cout not before :P
..................




or just use vectors :)

even if this seems stupid just try it, it will run if you do.
Last edited on
DrolArumil is right, in order to just create an standard array, the variables that are being used to determine the size must be constant... whether you use constant variables like DrolArumil demonstrated or you input regular integers:


int Grades[10][10];


I'm guessing this is an assignment for you so I'm not sure if your professor has talked about dynamic arrays with the use of pointers yet, but that would be the method to use in order for a program to take in a variable and create an array with that. If thats what you need to do, look up dynamic arrays and try to find out the solution yourself. If you cant figure it out, come back and we can show you.
closed account (D80DSL3A)
You are also exceeding the array bounds. Array indexes start at 0 not 1 so for(int i=1; i<=Projects; i++) ought to be for(int i=0; i<Projects; i++). Likewise for the j loop. Starting at 1 also causes elements Grades[0][0] through Grades[0][Projects-1] to be skipped.
Well, the goal here is to allow the user to decide the size of one of the array's dimensions, and then input data to fill the array.
I also have to do some basic math to that array, but I'm pretty sure I've got that down. My problem is getting the array set up.

I tried using dynamic arrays but I cannot find any example of making one part of a 2D array dynamic while the other remains constant.
I suppose I could just have eight arrays, but I kind of want it to be more efficient than that.
closed account (D80DSL3A)
Try this to create a 2D dynamic array of integers of dimensions Students x 8
1
2
3
4
int (*Grades)[8];// pointer to array of 8 integers
int Students = 0;
cin >> Students;// user inputs the # of students
Grades = new int[Students][8];


Don't forget to delete this memory when you are done with the array:
delete [] Grades;

EDIT:Yet another worthless post. I should give up and just focus on my own studies.
Last edited on
I kind of abandoned the entire dynamic array path by setting the array to the hypothetical max of [250][8].
Using dynamic arrays opened up a whole host of problems that I am just not smart enough to deal with.

However, I'm having a problem with another part of the same program, if you guys can help me out with this.
The point is to take the "class average," that is, adding up the averages of each individual 'Project' dimension in the array and then dividing it by 8, or the size of the 'Project' dimension.

The code I've got:
1
2
3
4
5
6
7
8
9
10
11
12
13
int Avg2; //Avg2, Sum2, etc., is just to differentiate between the other Avg variables in the program
int Sum2=0;
float AvgT2;
float SumT2=0;
for(int j=0; j<=Projects; j++){
     for(int i=0; i<=Students; i++) {
          Sum2 = Sum2 + Grades[i][j];
     }
     Avg2 = Sum2/Students;
}
SumT2 = SumT2 + Avg2;
AvgT2 = SumT2/Projects;
cout << AvgT2 << endl;

comes out with values approximately one half of one percent higher than they should be (eg., 47.75 instead of 47.5), which is so close that I am left absolutely baffled, and the weirdest part is that it follows the format of a previous Averaging function, which comes out with the correct result and looks like this:
1
2
3
4
5
6
7
8
9
10
int Select;
float Avg1;
float Sum1=0;
cout << "Give the number of the project: ";
cin >> Select;
for(int i=0; i<=Students; i++) {
     Sum1 = Sum1 + Grades[i][Select];
}
Avg1 = Sum1/Students;
cout << Avg1 << endl;

So one of these things is obviously wrong. Maybe the second is performing correctly in spite of something, or maybe I overlooked something in the first, but, either way I'm stuck now. Any help?
Nevermind, I figured out the problem.
In the stacked 'for' loops, the loop involving Projects needed to be 'Projects-1'
1
2
3
4
5
6
7
8
 for(int j=0; j<=Projects-1; j++){
      for(int i=0; i<=Students; i++) {
         Sum2 = Sum2 + Grades[i][j];
      }
      Avg2 = Sum2/Students;
}
SumT2 = SumT2 + Avg2;
AvgT2 = SumT2/Projects;

I forgot that the array starts at zero. It works perfectly now.
Topic archived. No new replies allowed.