I am required to store a matrix o N students with 4 fields (number, grade1, grade2 and average). This could be achieved with a struct, but that is not the purpose of this exercise. Once the data is collected (read_data
), it must be sorted according to the average (sort_matrix
). However, when more than 4 or more students are inserted, a segmentation fault happens. I have been unable to detect the origin n of the problem. What am I missing?
Relevant code is bellow.
void read_data(float **mtx, int *size){
float num, grade1, grade2;
while( scanf("%f %f %f", &num, &grade1, &grade2)==3 ){
(*size)++;
mtx = (float**)realloc(mtx, (*size)*sizeof(float*) );
mtx[*size-1] = (float*)malloc(4*sizeof(float));
mtx[*size-1][0] = num;
mtx[*size-1][1] = grade1;
mtx[*size-1][2] = grade2;
mtx[*size-1][3] = (grade1+grade2)/2;
}
printf("Done reading\n");
}
void sort_matrix(float **mtx, int size){
int i=0, j=0;
float *aux = NULL;
for(i=0; i<size-1; i++){
for(j = 0; j<size-1-i; j++){
if(mtx[j][3] > mtx[j+1][3]){
aux = mtx[j];
mtx[j] = mtx[j+1];
mtx[j+1]=aux;
}
}
}
printf("Done sorting\n");
}
int main(void){
float **mtx =(float**)malloc(0);
int size=0;
read_data(mtx, &size);
sort_matrix(mtx, size);
print_matrix(mtx, size);
return 0;
}
EDIT: Following the answers given bellow, I have found this topic which proved to be useful. The problem turned out to be changing the size of mtx without passing the pointer address. For the sorting function, there is no need to pass the address because even though the addresses the mtx array points to will change, the size will not.