I am trying to pass a 2D array of variable size to a function to print it.but the code doesn't show the exact result of sum.
this is the code:
#include <stdio.h>
#define ROW 5
#define COLL 5
void print_arr(int a[][COLL],int m,int n){
int i,j,sum;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("a[%d][%d]=%d\n",i,j,a[i][j]);
}
}
}
int sum_arr(int a[][COLL],int m,int n){
int i,j,sum;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
sum+=a[i][j];
}
}
return sum;
}
int main (void){
int a[ROW][COLL];
int i,j,m,n;
int sum;
printf("enter rows:");scanf("%d",&m);
printf("enter coll:");scanf("%d",&n);
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("a[%d][%d]=",i,j);scanf("%d",&a[i][j]);
}
}
print_arr(a,m,n);
printf("\n");
sum=sum_arr(a,m,n);
printf("sum=%d\n",sum);
return 0;
}
this is the result of the code
enter rows:2
enter coll:3
a[0][0]=5
a[0][1]=8
a[0][2]=4
a[1][0]=7
a[1][1]=9
a[1][2]=6
a[0][0]=5
a[0][1]=8
a[0][2]=4
a[1][0]=7
a[1][1]=9
a[1][2]=6
sum=-1217388517
please tell me what's wrong with the code....
sum=0