[英]I am trying to find sum of diagonal from 2d array. But getting its address instead of its value .Can anyone explain this to me?
I was trying to sum the diagonal value in 2d array but getting different Output ( Total:4194438 ) can any one help me through this.我试图对二维数组中的对角线值求和,但得到不同的 Output ( Total:4194438 )任何人都可以帮助我解决这个问题。
#include <stdio.h>
int main(){
int i,j,a,sum,total;
int diagonal_arr[3][3];
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a);
diagonal_arr[i][j]=a;
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
if(i==j){
sum= printf("%d\n",diagonal_arr[i][j]);
total+=sum;
}
}
printf("\n");
}
printf("Total= %d ",total);
}
OUPUT:输出:
Total= 4194438
You want this:你要这个:
...
total = 0; // initialize total to 0
for (i = 0; i < 3; i++){
for (j = 0; j < 3; j++){
if (i == j){
printf("%d\n", diagonal_arr[i][j]); // you can remove this line
total += diagonal_arr[i][j]; // add diagonal value
}
}
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.