# Thread: Why does my two-dimensional array print out incorrect values?

1. ## Why does my two-dimensional array print out incorrect values?

I'm trying to print the sum of columns 1, 4 and 6. When I input 30 values into the 2d array, only the fist column is has the correct output.

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

int main()
{
int i, j, sum1=0, sum2=0, sum3=0;
int arr[5][6];

for(i=0; i < 5; i++){
for(j=0; j<6; j++){
printf("Enter a value: ");
scanf("%d",&arr[i][j]);
;
}
}

printf("Elements of 2D array:- \n");
for(i=0; i<5; i++) {
for(j=0; j<6; j++) {
printf("%d ", arr[i][j]);
if(j==5){
printf("\n");
}
}
}

//this functions calculate the sum of the 1st column//
for(i=0; i<1; i++){
for(j=0; j<6; j++){
sum1 += arr[j][i];
}
printf("Sum of 1st column is: %d\n", sum1);
sum1=0;
}

//this functions calculate the sum of the 4th column//
for(i=0; i<1; i++){
for(j=3; j<6; j++){
sum2 = arr[j][i] + sum2;
}
printf("Sum of 4th column is: %d\n", sum2);
sum2=0;
}

//this functions calculate the sum of the 6th column//
for(i=0; i<1; i++){
for(j=5; j<6; j++){
sum3 = arr[j][i] + sum3;
}
printf("Sum of 6th column is: %d\n", sum3);
sum3=0;
}

return 0;
}```

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

#define ROWS 5
#define COLS 6

int main()
{
int arr[ROWS][COLS];
for (int r = 0; r < ROWS; r++)
for (int c = 0; c < COLS; c++) {
printf("Enter a value: ");
scanf("%d", &arr[r][c]);
}

printf("Elements of 2D array:-\n");
for (int r = 0; r < ROWS; r++) {
for (int c = 0; c < COLS; c++)
printf("%3d ", arr[r][c]);
printf("\n");
}

int sum = 0;
for (int r = 0; r < ROWS; r++)
sum += arr[r][0];
printf("Sum of 1st column is: %d\n", sum);

sum = 0;
for (int r = 0; r < ROWS; r++)
sum += arr[r][3];
printf("Sum of 4th column is: %d\n", sum);

sum = 0;
for (int r = 0; r < ROWS; r++)
sum += arr[r][5];
printf("Sum of 6th column is: %d\n", sum);

return 0;
}```

3. Thank you bro, I appreciate your help!
It finally work!