标题:关于C语言二维数组的很简单的代码,为何计算结果全是0?
只看楼主
prisoner_an
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-6-27
结帖率:0
已结贴  问题点数:20 回复次数:5 
关于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
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:7 
#include <stdio.h>

void total( float ar[][12], size_t n )
{
    float total = 0;
    for( size_t y=0; y!=n; ++y )
        for( size_t m=0; m!=12; ++m )
            total += ar[y][m];
    printf( "the total rainfall of the %zu years is %2.2f\n", n, total );
}

void aveyear( float ar[][12], size_t n )
{
    printf( "average rainfall of year:" );
    for( size_t y=0; y!=n; ++y )
    {
        float subtotal = 0;
        for( size_t m=0; m!=12; ++m )
            subtotal += ar[y][m];
        printf( " %2.2f", subtotal/12 );
    }
    putchar( '\n' );
}

void avemonth( float ar[][12], size_t n )
{
    printf( "average rainfall of month:" );
    for( size_t m=0; m!=12; ++m )
    {
        float subtotal = 0;
        for( size_t y=0; y!=n; ++y )
            subtotal += ar[y][m];
        printf(" %2.2f",subtotal/n );
    }
    putchar( '\n' );
}

int main( void )
{
    float rain[5][12] = { { 4.3f, 4.3f, 4.3f, 3.0f, 2.0f, 1.2f, 0.2f, 0.2f, 0.4f, 2.4f, 3.5f, 6.6f }
                        , { 8.5f, 8.2f, 1.2f, 1.6f, 2.4f, 0.0f, 5.2f, 0.9f, 0.3f, 0.9f, 1.4f, 7.3f }
                        , { 9.1f, 8.5f, 6.7f, 4.3f, 2.1f, 0.8f, 0.2f, 0.2f, 1.1f, 2.3f, 6.1f, 8.4f }
                        , { 7.2f, 9.9f, 8.4f, 3.3f, 1.2f, 0.8f, 0.4f, 0.0f, 0.6f, 1.7f, 4.3f, 6.2f }
                        , { 7.6f, 5.6f, 3.8f, 2.8f, 3.8f, 0.2f, 0.0f, 0.0f, 0.0f, 1.3f, 2.6f, 5.2f } };

    total( rain, sizeof(rain)/sizeof(*rain) );
    aveyear( rain, sizeof(rain)/sizeof(*rain) );
    avemonth( rain, sizeof(rain)/sizeof(*rain) );

    return 0;
}
2019-06-27 12:33
幻紫灵心
Rank: 11Rank: 11Rank: 11Rank: 11
来 自:山咔咔里面
等 级:贵宾
威 望:53
帖 子:395
专家分:2640
注 册:2018-3-30
得分:7 
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");
}
int j=0,subtotal=0; 这句,又声明了一次subtotal变量。
所以说你的subtotal声明了两次,for循环里面的subtotal作用范围只在那个for里面,后面输出的是最上边声明的那个。


saber,别哭.
2019-06-27 12:44
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
huhaile
Rank: 1
等 级:新手上路
帖 子:1
专家分:7
注 册:2019-7-3
得分:7 
这个可以哈,学习了学习了
2019-07-03 16:46



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




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

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