Home > Mobile >  Why is this happening with the matrix in C?
Why is this happening with the matrix in C?

Time:10-19

I have this function:

void changes_equation_order(double** system, unsigned int qty) {

    double aux[100];
     
     //first print
    for(int i = 0; i < qty; i  ) {
        for(int j = 0; j < qty; j  ) {
            printf("%.1lf ", system[i][j]);
            if(i == 0)
                aux[j] = system[i][j];
        }

        printf("\n");
    }

    remove_line(system, 0, qty);

    printf("\nReordering the matrix: \n");
    for(int i = 0; i < qty; i  ) {

        for(int j = 0; j < qty; j  ) {

            if(i == qty- 1) {
                system[i][j] = aux[j];

            }

            printf("%.1lf ", system[i][j]);
        }

        printf("\n");
    }

    printf("\Second print: \n");

    for(int i = 0; i < qty; i  ) {

        for(int j = 0; j < qty; j  ) {

            printf("%.1lf ", system[i][j]);
        }
        printf("\n");
    }

}

It receives a matrix with rows and columns with qty value, so, in the example, system is:

0   3   2
4   0   2
2   3   0

and qty equals to 3.

When I print the first time, the matrix is with the right value, so it gives me

0   3   2
4   0   2
2   3   0

When I print the second one (Reordering print), it reorders the matrix values and prints to me:

4.0 0.0 2.0
2.0 3.0 0.0
0.0 3.0 2.0

Which is correct, but when I print the matrix right after, it produces this:

4.0 0.0 2.0
0.0 3.0 2.0
0.0 3.0 2.0

Why is this happening?

Also, this is the remove_line function:

void remove_line(double** system, int row, unsigned int qty){
    qty--;

    free(system[row]);
    while(row < qty) {
        system[row] = system[row   1];
        row  ;
    }

}

Thanks so much for your help!

CodePudding user response:

It happen because system[1] and system[2] are pointing to the same memory after execution of remove_line. In other words, when you change, e.g. system[2][0] it will also change system[1][0]. They are the same memory.

To explain it with an example assume that the pointers system[n] has these values when you call changes_equation_order

system[0] = 0x1000;
system[1] = 0x2000;
system[2] = 0x3000;

then after executing remove_line(system, 0, qty); the values will be

system[0] = 0x2000;
system[1] = 0x3000;
system[2] = 0x3000;

As you can see system[1] and system[2] are now pointing to the same memory.

The first print seemed fine because you print the system[1] row before executing

        if(i == qty- 1) {
            system[i][j] = aux[j];

        }

where you are changing the system[2] row (but also the system[1] row as they are the same).

I'm not sure what you are trying to do in the remove_line function so I can't advice you. However, one advice....

Use separate loops for printing and modifying the matrix! I know that will give you some extra loops but your code will be much easier to understand.

For instance instead of:

printf("\nReordering the matrix: \n");
for(int i = 0; i < qty; i  ) {

    for(int j = 0; j < qty; j  ) {

        if(i == qty- 1) {
            system[i][j] = aux[j];

        }

        printf("%.1lf ", system[i][j]);
    }

    printf("\n");
}

use multiple loops - like

printf("\nPrint before reestablish of last row: \n");
for(int i = 0; i < qty; i  ) {

    for(int j = 0; j < qty; j  ) {
        printf("%.1lf ", system[i][j]);
    }
    printf("\n");
}

// Reestablish last row
for(int i = 0; i < qty; i  ) {
    for(int j = 0; j < qty; j  ) {
        if(i == qty- 1) {
            system[i][j] = aux[j];
        }
    }
}

printf("\nPrint after reestablish of last row: \n");
for(int i = 0; i < qty; i  ) {

    for(int j = 0; j < qty; j  ) {
        printf("%.1lf ", system[i][j]);
    }
    printf("\n");
}
  • Related