Grade array trouble

I'm having some trouble with my sum not adding up properly, I'm not exactly sure what's going on here. I'm new to C++ and still trying to get this stuff down.

A user tells the program how many students there are (up to 250). Each student has 8 grades. My problem is when I try to average a project grade for the entire "class" the sum of the grades does something that I really don't understand.

Here's where I input the grades:
int students;
int projects=7;
int grades[250][8];
int number;
//these are all outside of int main()


cout << "Please give number of students: ";
cin >> students;

for (int i=0; i<=projects; i++) {
for (int j=0; j<=students-1; j++) {
cout << "Please give grade of student " << j+1 << " in project " << i+1 << ":";
cin >> grades[i][j];


And here's where the problem starts:

int projectavg;
int sum=0;

cout << "Give the number of the project: ";
cin >> number;

for (int i=0; i<=students; i++) {
sum=sum+grades[i][number];
}
projectavg=sum/students;
cout << sum << endl;


I chose to print the sum to make sure it was spitting out the correct value, which it isn't.

So if you could give me some tips, that'd be awesome! Thanks!

In your first for loop, you go from 0 to students-1 inclusive, and in your second, you go from 0 to students inclusive. Your ranges don't match.
Thanks I figured it out!

I had grades[i][j] instead of grades[j][i]..
Topic archived. No new replies allowed.