自学C第17天

C primer plus

第10章

第11题:

#include <stdio.h>

#define COLS 5

void p_arr(double ar[][5],double cr[][5],int x);

void d_arr(double ar[][5],double cr[][5],int y);

int main(void) {

       double target1[3][5];

       double target2[3][5];

       double source[3][5] = { {3.1,5.5,6.6,8.8,4.1},

                                              {4.7,8.5,9.6,6.6,5.1},

                                              {3.1,1.2,2.5,6.5,7.7} };

       p_arr(target1, source, 3);

       putchar('\n');

       d_arr(target2, source, 3);

       return 0;

}

void p_arr(double ar[][5], double cr[][5],int x) {

       int i, j;

       for (i = 0; i < x; i++) {

              putchar('\n');

              for (j = 0; j < COLS; j++) {

                     ar[i][j] = cr[i][j]; #注:可以不需要定义两个数组形参,这里直接该为打印ar[i][j].

                     printf("%g ", ar[i][j]);

              }

       }

}

void d_arr(double ar[][5], double cr[][5], int y) {

       int i, j;

       for (i = 0; i < y; i++) {

              putchar('\n');

              for (j = 0; j < COLS; j++) {

                     ar[i][j] = 2*cr[i][j];#注:可以不需要定义两个数组形参,这里直接该为打印2*ar[i][j].

                     printf("%g ", ar[i][j]);

              }

       }

}

第12题:

#include<stdio.h>

#define MONTHS 12

#define YEARS 5

void ave_year(double ar[][MONTHS]);

void ave_month(double ar[][MONTHS]);

int main(void)

{

       const double rain[YEARS][MONTHS] = {

              {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}

       };

       ave_year(rain);

       ave_month(rain);

       return 0;

}

void ave_year(double ar[][MONTHS]) { #注:在后面可以加上形参int n,当调用时需要带入YEARS,这样就可以在define里面修改YEARS来计算比如一年,或者两年的数据了。

       int year, month;

       double subtot, total;

       printf("YEAR    RAINFALL  (inches)\n");

       for (year = 0, total = 0; year < YEARS; year++)#注:如果加上形参int n, for循环中的YEARS需改改成n,下面的函数同理。

       {

              for (month = 0, subtot = 0; month < MONTHS; month++)

              {

                     subtot += ar[year][month];

              }

              printf("%5d %15.1f\n", 2010 + year, subtot);

              total += subtot;

       }

       printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);

       printf("MONTHLY AVERAGES:\n\n");

       printf("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT");

       printf("NOV DEC\n");

}

void ave_month(double ar[][MONTHS]) {

       int year, month;

       double subtot;

       for (month = 0; month < MONTHS; month++)

       {

              for (year = 0, subtot = 0; year < YEARS; year++)

              {

                     subtot += ar[year][month];

              }

              printf("%4.1f", subtot / YEARS);

       }

       printf("\n");

加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值