Ruggero Latella - 1 year ago 48
C Question

The code below is giving the wrong answer if, for example, I enter:

``````1st Matrix
1 2 3
4 5 6
7 8 9

2nd Matrix
2 2 2
2 2 2
2 2 2
``````

It gives me this matrix sum output:

``````9 10 11
9 10 11
9 10 11
``````

which is obviously wrong! I can't seem to find why?

``````#include <stdlib.h>
#include <stdio.h>

int main(void)
{

int r,c;
int ir,ic;
int matrix1[r][c];
int matrix2[r][c];
int finalmatrix[r][c];

printf("Insert number of rows of the matrixes (max 10):  ");
scanf("%d", &r);
printf("Insert number of columns of the matrixes (max 10):  ");
scanf("%d", &c);

while(r!=c)
{
printf("The number of rows and columns are not equal, retry:\n");
printf("Insert number of rows of the Matrix (max 10):  ");
scanf("%d", &r);
printf("Insert number of columns of the Matrix (max 10):  ");
scanf("%d", &c);
}

for(ir=1; ir<=r; ir++)
{
for(ic=1; ic<=c; ic++)
{
printf("Insert element row %d and column %d of the first matrix: ", ir, ic);
scanf("%d", &matrix1[ir][ic]);
}
}

for(ir=1; ir<=r; ir++)
{
for(ic=1; ic<=c; ic++)
{
printf("Insert element row %d and column %d of the second matrix: ", ir, ic);
scanf("%d", &matrix2[ir][ic]);
}
}

for(ir=1; ir<=r; ir++)
{
for(ic=1; ic<=c; ic++)
{
finalmatrix[ir][ic]=matrix1[ir][ic]+matrix2[ir][ic];
}
}

printf("The sum Matrix is:\n");

for(ir=1; ir<=r; ir++)
{
for(ic=1; ic<=c; ic++)
{
printf("%d ", finalmatrix[ir][ic]);
}
printf("\n");
}

return 0;
}
``````

Your code fails to initialize the variables `r` and `c` before declaring the VLAs with them. You have no way of knowing what size these arrays are, or if `r` and `c` are even positive values! I am surprised that this code will even run. I ran the code that you posted, and got this runtime error:

runtime error: variable length array bound evaluates to non-positive value -1208010352

I made the following change, and the code works:

``````int r,c;
int ir,ic;

printf("Insert number of rows of the matrixes (max 10):  ");
scanf("%d", &r);
printf("Insert number of columns of the matrixes (max 10):  ");
scanf("%d", &c);

int matrix1[r][c];
int matrix2[r][c];
int finalmatrix[r][c];
``````

``````The sum Matrix is:
3 4 5
6 7 8
9 10 11
``````

According to section of the standard about array declarators:

the [ and ] may delimit an expression.... If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. (ISO/IEC 9899:1999 6.7.5.2/1)

If the size is an expression that is not an integer constant expression... each time it is evaluated it shall have a value greater than zero. (ISO/IEC 9899:1999 6.7.5.2/5)

So a negative value for `r` or `c`, which is very likely, results in undefined behavior, meaning that anything could happen! When I ran the code, I got a runtime error. You ran the same code, and got a different result. The behavior is undefined, so there is no telling what might happen.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download