Hi I think this is my first post, so here it goes:
Overview:

This program declares global bidimensional array of 30 rows and 5 columns (not counting 0 in the rows) of floating type to save information of students, table is as follows:

ID Partial1 Partial2 Partial3 Partial4 Average

ID: em..I
Partial N: Students grade of a evaluation.
Average: well students final grade.

the grade can't be higher than 10.

Problem:

Ok so my problem is that when I start inputing data everything goes well until we input more than 1 student, when I enter the second ID it is saved but the second student's ID overwrites the last student's average, having the ID duplicated and the las average erased, and i can't figure out why is this happening, I've looked at the code for hours, so any help is really appreciated, by the way I'm relatively new to c++.

Notes:

//Start of data input
    for (row= 1;row<=30;row++){
         Sum=0;//reset acumulator.
         
         printf("\nStudent %d.", row);
         printf("\n\tAverage: ");scanf("%f",&alumno[row][0]);
         
         for (col=1;col<=4;col++){
              printf("\n\tPartial %d: ",col);scanf("%f",&alumno[row][col]);
              Sum=Sum+alumno[row][col];//acumulating values for average
         }
         
         alumno[row][5]=Sum/4; //this is the average
         printf("\nAverage: %0.2f", alumno[row][5]);
    }//end of input

Output

ID  P1 P2 P3 P4 Av
10 10 10 10 10 9
9   9    9  9   9   6
6   6    6  6   6   6

Data of 3 students
assuming you use 10 for all of the first student's data, 9 for the second and 6 for the third.

see how the last value of the first and second student gets replaced by the ID of the next one.

Is your array declared as 5 columns or as 6? It makes a difference in the indexing you use.
Are you using the row index as the student ID? I don't see input for IDs.

Remember that the maximum index you can (safely) use is one less than the size of the array dimension. So, if your array is [30][5], your maximum index is [29][4]. By putting the average in column index [5], you are probably overwriting the first position of the next row.

Typical loop usage:

int arr[10][5];
int r, c;
for( r = 0; i < 10; r++ )
   for( c = 0; c < 6; c++ )
       //do something with arr[r][c]

By not using index 0, you waste a row and a column, and your looping takes a bit more care to understand and do correctly.

het thanks for ypur answer, redimensioning tn columns to 6 solved the issue, Just on last question, why does it overwrite?, I mean why is it safe to use cols-1 as the last index.

Thread Solved.

because arrays will alwayas allocate the the first index of the array to 0,

Typical loop usage:

int arr[10][5];
int r, c;
for( r = 0; i < 10; r++ )
   for( c = 0; c < 6; c++ ) // <--- typo ?
       //do something with arr[r][c]

I'm assuming that was a typo?

@Triztian:

Array elements follow each other contiguously in memory.

int arr[2][5] will look like this in memory

0,0 | 0,1 | 0,2 | 0,3 | 0,4 | 1,0 | 1,1 | 1,2 | 1,3 | 1,4

So if you write to 0,5 it goes into the slot for 1,0.

Always use array indexing from 0 to n-1.

YES, that was a typo
Should read
for( c = 0; c < 5; c++ )

(walks away with embarrassed look)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.