im trying to use a dynamic array to calculate average with credits but my program crashes when i run with the values:
- 2 1 1 1 1 3 1 1
it doesn't crash if i do:
- 2 1 1 1 1 4 1 1 1 1
if i remove the for loop with the free(); inside
for (i=0 ; i<classes ; i++) 
{   //free each individual 2unit array first
    free(grades[i]);    //This line doesnt work
}
it runs fine but i don't want to do that because im told not to do that.
here's the code, i tried to remove the unnecessary parts as best as i can
#include<stdio.h>
#include<stdlib.h>
void fillArray(int **grades,int start,int finish)
{   
    int i;
    for(i=start;i<finish;i++)
    {
        printf("Enter grade for Class %d: ",i+1);
        scanf("%d",&grades[i][0]);
        printf("Enter Credit for Class %d: ",i+1);
        scanf("%d",&grades[i][1]);
    }
}
void expandArray(int **grades,int oldSize,int newSize)
{
    *grades = (int *)realloc(*grades,newSize*sizeof(int*));    //expanding the pointer array
    int i;
    for(i=oldSize;i<newSize;i++)   //filling it with 2 unit arrays per class
    {
        grades[i] = (int *)malloc(2*sizeof(int));   
    }
    fillArray(grades,oldSize,newSize);  
}
int main()
{
    int classes,oldClasses;
    printf("Enter number of classes: ");
    scanf("%d",&classes);
    int **grades = (int **)malloc(classes*sizeof(int*));   //creating an array to store 2unit arrays(pointer array)
    int i;
    for(i=0;i<classes;i++)   //filling the pointer array with 2 unit arrays per class
    {
        grades[i] = (int *)malloc(2*sizeof(int));
    }
    printf("Enter grades for each classes: \n");
    fillArray(grades,0,classes);    // this 0 here means we start at the index 0, that parameter is later used to start at the lastIndex+1
    oldClasses = classes;   // copied the value of classes to oldClasses instead of taking the new one as newClasses to avoid confusion.
    printf("Enter new number of classes: ");
    scanf("%d",&classes);
    expandArray(grades,oldClasses,classes);
    printf("This line works!");
    for (i=0 ; i<classes ; i++) 
    {   //free each individual 2unit array first
        free(grades[i]);    //This line doesnt work
    }
    printf("This won't get printed with the value 3...");
    free(grades);   //free the pointer array (This one also works)
    return 0;
}
 
     
    