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;
}