0

I'm trying to do a modified Gauss-Jordan Elimination for computing inverses. With this I take a matrix and find the inverse. Although I can get the matrix using malloc When I try to double the size of a new matrix it causes an exception

int main (int argc, char *argv[]) {
    FILE *train, *test;

    train = fopen(argv[1], "r");
    test = fopen(argv[2], "r");

    int abutes,examples;
    fscanf(train," %d %d",&abutes,&examples);  /*scan in number of examples and attributes*/

    int i,j, abuteswp;
    abuteswp=abutes+1;
    double (*trainA)[10000] = malloc(sizeof(double[10000][10000]));
    for (j=0;j<examples;j++){
        for ( i=1; i <abuteswp+1; i++){
            fscanf(train," %lf %[^,]",&trainA[j][i]);}}

            int p;
            /*add in 1s to matrix*/
            for (p=0;p<examples;p++)    {
                trainA[p][0]=1;
            }

            /*take out price*/
            double trainatt[examples][abuteswp];
            for ( i=0; i <examples; i++){
                for ( j=0;j<abuteswp;j++){
                    trainatt[i][j]=trainA[i][j];
                }
            }

After doing some computations with the matrix I need to augment it and double the columns. After I try testing this causes an exception

    double ** trainaug = NULL;
    int aug;
    aug=examples*2;
    trainaug = malloc (examples * sizeof(double *));

    for(i = 0; i < examples; i++)
    {
        trainaug[i] = malloc (aug * sizeof(double));
    }
    for(i = 0; i <examples; i++)
    {
        trainaug[i] = malloc (examples * sizeof(double));
    }
    for ( i=0; i <examples; i++){
        for ( j=0;j<examples;j++){
            trainaug[i][j]=trainmult[i][j];}
        }
        for ( i=0; i <examples; i++){
            for ( j=examples;j<aug;j++){
                trainaug[i][j]=0;}
            }
            for ( j=0;j<examples;j++){
                i=examples;
                trainaug[j][i]=1;
                i++;
            }

Even without the for loops this causes an exception. Am I doing malloc the wrong way or is my code not right? I am freeing my pointers as I go along

Steve
  • 1,747
  • 10
  • 18
Devs1575
  • 9
  • 2

0 Answers0