关于C语言二维数组的很简单的代码,为何计算结果全是0?
#include<stdio.h>void total(float ar[][12],int n)
{
float total=0;
for(int i=0;i<n;i++)
for(int j=0;j<12;j++)
total+=ar[i][j];
printf("the total rainfall of the %d years is %2.2f",n,total);
printf("\n");
}
void aveyear(float ar[][12],int n)
{
float subtotal;
printf("average rainfall of year :");
for(int i=0;i<n;i++){
for(int j=0,subtotal=0;j<12;j++)
subtotal+=ar[i][j];
printf("%2.2f ",subtotal/12);
}
printf("\n");
}
void avemonth(float ar[][12],int n)
{
float subtotal;
printf("average rainfall of month :");
for(int j=0;j<12;j++){
for(int i=0,subtotal=0;i<n;i++)
subtotal+=ar[i][j];
printf("%2.2f ",subtotal/n);
}
}
void main()
{
float rain[5][12]={
{4.3,4.3,4.3,3.0,2.0,1.2,0.2,0.2,0.4,2.4,3.5,6.6},
{8.5,8.2,1.2,1.6,2.4,0.0,5.2,0.9,0.3,0.9,1.4,7.3},
{9.1,8.5,6.7,4.3,2.1,0.8,0.2,0.2,1.1,2.3,6.1,8.4},
{7.2,9.9,8.4,3.3,1.2,0.8,0.4,0.0,0.6,1.7,4.3,6.2},
{7.6,5.6,3.8,2.8,3.8,0.2,0.0,0.0,0.0,1.3,2.6,5.2}};
total(rain,5);
aveyear(rain,5);
avemonth(rain,5);
}