标题:关于C语言二维数组的很简单的代码,为何计算结果全是0?
取消只看楼主
prisoner_an
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-6-27
结帖率:0
已结贴  问题点数:20 回复次数:2 
关于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);
}

搜索更多相关主题的帖子: void float int for printf 
2019-06-27 12:12
prisoner_an
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-6-27
得分:0 
回复 3楼 幻紫灵心
这么明显的错误居然没发现
2019-06-27 16:58
prisoner_an
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-6-27
得分:0 
回复 2楼 rjsp
还有这操作!学习了
2019-06-27 17:01



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-495532-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061662 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved