# Thread: What is wrong with the code? How the element values of array changes?

1. ## What is wrong with the code? How the element values of array changes?

Code:
```#include <stdio.h>

int main()
{
int i,j,row=1,col=3,sum=0;
int a[row][col];
printf("Enter the elements on by one:");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("The A matrix \n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
int colsum[col];	//To calculate column sum
for(j=1;j<=col;j++)
{
colsum[j]=0;
for(i=1;i<=row;i++)
{
colsum[j] = colsum[j] + a[i][j];
}
}
printf("The A matrix \n");
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
printf("\n The Column Sum is =");
for(i=1;i<=col;i++)
{
printf("%5d",colsum[i]);
}
return(0);
}```

2. > for(i=1;i<=row;i++)
> for(j=1;j<=col;j++)
Arrays start at subscript 0, not 1.

for ( i = 0 ; i < N ; i++ )
is the idiomatic loop for iterating over an array of length N.

3. Agreed, subscript start at 0, but starting at 1 is not invalid.

4. Agreed, subscript start at 0, but starting at 1 is not invalid.
True, if you don't ever want to use the element at index 0. On the other hand, accessing a[row][col] when you declared a as int a[row][col] is invalid as you're accessing the array out of bounds.

So you either want to start from 0, or you want to declare a as int a[row + 1][col + 1].