| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2885 人关注过本帖
标题:关于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
快速回复:关于C语言二维数组的很简单的代码,为何计算结果全是0?
数据加载中...
 
   



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

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