C primer plus (第六版)第十章 编程练习第12,13,14题

第12题题目:

12.重写程序清单10.7的rain.c程序,把main()中的主要任务都改成⽤函数来完成。

// 程序清单10.7 rain.c程序
/* rain.c  -- 计算每年的总降⽔量、年平均降⽔量和5年中每⽉的平均降⽔量 */
// #include <stdio.h>
// #define MONTHS 12        // ⼀年的⽉份数
// #define YEARS   5        // 年数
// int main(void)
// {
//      // ⽤2010〜2014年的降⽔量数据初始化数组
//      const float 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 }
//      };
//      int year, month;
//      float subtot, total;
//      printf(" YEAR    RAINFALL  (inches)\n");

//      for (year = 0, total = 0; year < YEARS; year++)
//      {                    // 每⼀年,各⽉的降⽔量总和
//           for (month = 0, subtot = 0; month < MONTHS; month++)
//                subtot += rain[year][month];
//           printf("%5d %15.1f\n", 2010 + year, subtot);
//           total += subtot;    // 5年的总降⽔量
//      }
//      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");
//      for (month = 0; month < MONTHS; month++)
//      {                    // 每个⽉,5年的总降⽔量
//           for (year = 0, subtot = 0; year < YEARS; year++)
//                subtot += rain[year][month];
//           printf("%4.1f ", subtot / YEARS);
//      }
//      printf("\n");
//      return 0;
// }

//12. 重写程序清单10.7的rain.c程序,把main()中的主要任务都改成⽤函数来完成。
#include <stdio.h>
#define MONTHS 12        // ⼀年的⽉份数
#define YEARS   5        // 年数
#define Y_AVG_HEAD " YEAR    RAINFALL  (inches)"
#define M_AVG_HEAD " Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec "
double yearly_avg(int m, int y, const float rain[YEARS][MONTHS]);
double monthly_avg(int m, int y, const float rain[YEARS][MONTHS]);
int main(void)
{
     // ⽤2010〜2014年的降⽔量数据初始化数组
     const float 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 }
     };
    
    yearly_avg(MONTHS, YEARS, rain);

    monthly_avg(MONTHS, YEARS, rain);
    return 0;
}
   
double yearly_avg(int m, int y, const float rain[YEARS][MONTHS])
{
    int year, month;
    float subtot, total;
    printf("%s\n",Y_AVG_HEAD);
    for (year = 0, total = 0; year < YEARS; year++)
    {                    // 每⼀年,各⽉的降⽔量总和
          for (month = 0, subtot = 0; month < MONTHS; month++)
               subtot += rain[year][month];
          printf("%5d %15.1f\n", 2010 + year, subtot);
          total += subtot;    // 5年的总降⽔量
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
    return total / YEARS;
}

double monthly_avg(int m, int y, const float rain[YEARS][MONTHS])
{
    int year, month;
    float subtot;
    printf("MONTHLY AVERAGES:\n\n");
    printf("%s\n",M_AVG_HEAD);
    for (month = 0; month < MONTHS; month++)
    {                    // 每个⽉,5年的总降⽔量
        for (year = 0, subtot = 0; year < YEARS; year++)
            subtot += rain[year][month];
        printf("%4.1f ", subtot / YEARS);
    }
    putchar('\n');
    return subtot / YEARS;
}

第13题题目

13.编写⼀个程序,提⽰⽤⼾输⼊3组数,每组数包含5个double类型的数(假设⽤⼾都正确地响应,不会输⼊⾮数值数据)。该程序应完成下列任务。
  a.把⽤⼾输⼊的数据存储在3×5的数组中
  b.计算每组(5个)数据的平均值
  c.计算所有数据的平均值
  d.找出这15个数据中的最⼤值
  e.打印结果
每个任务都要⽤单独的函数来完成(使⽤传统C处理数组的⽅式)。完成任务b,要编写⼀个计算并返回⼀维数组平均值的函数,利⽤循环调⽤该函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c和d的函数应把结果返回主调函数。

#include <stdio.h>
#define ROWS 3
#define COLS 5
void ar_initial(int r, int c, double source[ROWS][COLS]);
double ar_avg(int r, int c, double source[ROWS][COLS]);
double ar_tot_avg(int r, int c, double source[ROWS][COLS]);
double ar_max(int r, int c, double source[ROWS][COLS]);

int main()
{
    int i;
    double r_ar_avg;
    double source[ROWS][COLS];
    printf("Please entry data for array[%d][%d].\n",ROWS, COLS);
    ar_initial(ROWS, COLS, source);
    for ( i = 0; i < ROWS; i++)
    {
        r_ar_avg = ar_avg(i, COLS, source);
        printf("Row number %d average: %4.4Lf\n", i+1, r_ar_avg);
    }
    printf("Total datas' average is %4.4Lf\n", 
    ar_tot_avg(ROWS, COLS, source));
    printf("The maximum number is: %4.4Lf\n",
    ar_max(ROWS, COLS, source));
    
    return 0;
}

void ar_initial(int r, int c, double source[ROWS][COLS])
{
    int i,j;
    for ( i = 0; i < ROWS; i++)
    {
        printf("Please entry array row %d datas:\n", i);
        for ( j = 0; j < COLS; j++)
        {
            scanf("%lf",&source[i][j]);
        }
    }   
}

double ar_avg(int r, int c, double source[ROWS][COLS])
{
    int j;
    double sub_tot;
    for ( j = 0, sub_tot = 0; j < COLS; j++)
    {
        sub_tot += source[r][j];
    }
    return sub_tot / COLS;
}

double ar_tot_avg(int r, int c, double source[ROWS][COLS])
{
    int i, j;
    double tot, sub_tot;
    for ( i = 0, tot = 0; i < ROWS; i++)
    {
        for ( j = 0, sub_tot = 0; j < COLS; j++)
        {
            sub_tot += source[i][j];
        }
        tot += sub_tot;
    }
    return tot / (ROWS * COLS);
}

double ar_max(int r, int c, double source[ROWS][COLS])
{
    int i,j;
    double max = source[0][0];
    for ( i = 0; i < ROWS; i++)
    {
        for ( j = 0; j < COLS; j++)
        {
            if (max < source[i][j])
                max = source[i][j];
            else
                continue;
        }
    }
    return max;    
}

我把代码直接复制到豆包AI的编程对话框中,豆包直接分析并帮我分析修改了程序并加上了注释,修改后的程序如下;

#include <stdio.h>
#define ROWS 3
#define COLS 5

// 函数声明
void ar_initial(int r, int c, double source[ROWS][COLS]);
double ar_avg(int r, int c, double source[ROWS][COLS]);
double ar_tot_avg(int r, int c, double source[ROWS][COLS]);
double ar_max(int r, int c, double source[ROWS][COLS]);

int main()
{
    int i;
    double r_ar_avg;
    double source[ROWS][COLS];
    
    printf("Please enter data for array[%d][%d].\n", ROWS, COLS);
    ar_initial(ROWS, COLS, source);
    
    // 计算并打印每行的平均值
    for (i = 0; i < ROWS; i++)
    {
        r_ar_avg = ar_avg(i, COLS, source);
        printf("Row number %d average: %.4f\n", i + 1, r_ar_avg);
    }
    
    // 计算并打印总平均值
    printf("Total data average is %.4f\n", ar_tot_avg(ROWS, COLS, source));
    
    // 查找并打印最大值
    printf("The maximum number is: %.4f\n", ar_max(ROWS, COLS, source));
    
    return 0;
}

// 初始化数组,从用户输入获取数据
void ar_initial(int r, int c, double source[ROWS][COLS])
{
    int i, j;
    for (i = 0; i < ROWS; i++)
    {
        printf("Please enter array row %d data:\n", i + 1);  // 调整为从1开始显示行号
        for (j = 0; j < COLS; j++)
        {
            scanf("%lf", &source[i][j]);
        }
    }   
}

// 计算指定行的平均值
double ar_avg(int r, int c, double source[ROWS][COLS])
{
    int j;
    double sub_tot = 0;  // 初始化放在声明处更清晰
    for (j = 0; j < COLS; j++)
    {
        sub_tot += source[r][j];
    }
    return sub_tot / COLS;
}

// 计算所有元素的总平均值
double ar_tot_avg(int r, int c, double source[ROWS][COLS])
{
    int i, j;
    double tot = 0;  // 简化变量,无需sub_tot
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            tot += source[i][j];
        }
    }
    return tot / (ROWS * COLS);
}

// 查找数组中的最大值(已修复越界问题)
double ar_max(int r, int c, double source[ROWS][COLS])
{
    int i, j;
    double max_val = source[0][0];  // 直接用一个变量记录全局最大值
    
    for (i = 0; i < ROWS; i++)
    {
        for (j = 0; j < COLS; j++)
        {
            if (source[i][j] > max_val)  // 直接比较当前元素和全局最大值
            {
                max_val = source[i][j];
            }
        }
    }
    return max_val;    
}

第14题题目:

14.以变⻓数组作为函数形参,完成编程练习13。

思路:

应该用类似题目11中的变长数组作为函数形参的格式,先提示输入数组的行数和列数,然后定义二维变长数组后后面的程序基本上和题目13类似了,用相同的二维数组初始化,计算平均和找最大值函数,唯一区别就是将原本宏定义的ROWS和COLS改为从输入的rows和cols,并且函数形参中一定要有int rows, int cols 在数组前面。

#include <stdio.h>
void ar_initial(int rows, int cols, double source[rows][cols]);
double ar_avg(int rows, int cols, double source[rows][cols]);
double ar_tot_avg(int rows, int cols, double source[rows][cols]);
double ar_max(int rows, int cols, double source[rows][cols]);

int main()
{
    int rows, cols;
    int i;
    double r_ar_avg;
    printf("Please input the rows and cols for source array:\n");
    while( scanf("%d %d",&rows, &cols) == 2)
    {
        if ((rows > 1) && (cols > 1))
        {    
            printf("Accept the rows and cols for array target[%d][%d]\n", rows, cols);
            break;
        }
        else
        {
            printf("Wrong number for rows and cols.\n");
            printf("Re enter the numbers.\n");
            continue;
        }
    }
    double source[rows][cols];
    ar_initial(rows, cols, source);
    printf("Please entry data for array[%d][%d].\n",rows, cols);
    for ( i = 0; i < rows; i++)
    {
        r_ar_avg = ar_avg(i, cols, source);
        printf("Row number %d average: %4.4Lf\n", i+1, r_ar_avg);
    }

    printf("Total datas' average is %4.4Lf\n", 
    ar_tot_avg(rows, cols, source));
    printf("The maximum number is: %4.4Lf\n",
    ar_max(rows, cols, source));

    return 0;
}

void ar_initial(int rows, int cols, double source[rows][cols])
{
    int i,j;
    for ( i = 0; i < rows; i++)
    {
        printf("Please entry array row %d datas:\n", i);
        for ( j = 0; j < cols; j++)
        {
            scanf("%lf",&source[i][j]);
        }
    }   
}

double ar_avg(int rows, int cols, double source[rows][cols])
{
    int j;
    double sub_tot;
    for ( j = 0, sub_tot = 0; j < cols; j++)
    {
        sub_tot += source[rows][j];
    }
    return sub_tot / cols;
}

double ar_tot_avg(int rows, int cols, double source[rows][cols])
{
    int i, j;
    double tot, sub_tot;
    for ( i = 0, tot = 0; i < rows; i++)
    {
        for ( j = 0, sub_tot = 0; j < cols; j++)
        {
            sub_tot += source[i][j];
        }
        tot += sub_tot;
    }
    return tot / (rows * cols);
}

double ar_max(int rows, int cols, double source[rows][cols])
{
    int i,j;
    double max = source[0][0];
    for ( i = 0; i < rows; i++)
    {
        for ( j = 0; j < cols; j++)
        {
            if (max < source[i][j])
                max = source[i][j];
            else
                continue;
        }
    }
    return max;    
}

豆包AI注释并修改后的程序:

#include <stdio.h>
// 修复函数声明,确保参数名和类型匹配
void ar_initial(int rows, int cols, double source[rows][cols]);
double ar_avg(int row, int cols, int rows, double source[rows][cols]);  // 添加rows参数
double ar_tot_avg(int rows, int cols, double source[rows][cols]);
double ar_max(int rows, int cols, double source[rows][cols]);

int main()
{
    int rows, cols;
    int i;
    double r_ar_avg;
    printf("Please input the rows and cols for source array:\n");
    while( scanf("%d %d",&rows, &cols) == 2)
    {
        if ((rows > 1) && (cols > 1))
        {    
            printf("Accept the rows and cols for array target[%d][%d]\n", rows, cols);
            break;
        }
        else
        {
            printf("Wrong number for rows and cols.\n");
            printf("Re enter the numbers.\n");
            continue;
        }
    }
    double source[rows][cols];
    ar_initial(rows, cols, source);
    printf("Please entry data for array[%d][%d].\n",rows, cols);
    for ( i = 0; i < rows; i++)
    {
        // 调用时添加rows参数
        r_ar_avg = ar_avg(i, cols, rows, source);
        printf("Row number %d average: %4.4f\n", i+1, r_ar_avg);
    }

    printf("Total datas' average is %4.4f\n", 
    ar_tot_avg(rows, cols, source));
    printf("The maximum number is: %4.4f\n",
    ar_max(rows, cols, source));

    return 0;
}

void ar_initial(int rows, int cols, double source[rows][cols])
{
    int i,j;
    for ( i = 0; i < rows; i++)
    {
        printf("Please entry array row %d datas:\n", i);
        for ( j = 0; j < cols; j++)
        {
            scanf("%lf",&source[i][j]);
        }
    }   
}

// 修复函数定义,添加rows参数并正确使用
double ar_avg(int row, int cols, int rows, double source[rows][cols])
{
    int j;
    double sub_tot;
    for ( j = 0, sub_tot = 0; j < cols; j++)
    {
        sub_tot += source[row][j];
    }
    return sub_tot / cols;
}

double ar_tot_avg(int rows, int cols, double source[rows][cols])
{
    int i, j;
    double tot = 0;
    for ( i = 0; i < rows; i++)
    {
        for ( j = 0; j < cols; j++)
        {
            tot += source[i][j];
        }
    }
    return tot / (rows * cols);
}

double ar_max(int rows, int cols, double source[rows][cols])
{
    int i,j;
    double max = source[0][0];
    for ( i = 0; i < rows; i++)
    {
        for ( j = 0; j < cols; j++)
        {
            if (max < source[i][j])
                max = source[i][j];
        }
    }
    return max;    
}
    

dnSpy是目前业界广泛使用的一款.NET程序的反编译工具,支持32位和64位系统环境。它允许用户查看和编辑.NET汇编和反编译代码,以及调试.NET程序。该工具通常用于程序开发者在维护和调试过程中分析程序代码,尤其在源代码丢失或者无法获取的情况下,dnSpy能提供很大的帮助。 V6.1.8版本的dnSpy是在此系列软件更新迭代中的一个具体版本号,代表着该软件所具备的功能与性能已经达到了一个相对稳定的水平,对于处理.NET程序具有较高的可用性和稳定性。两个版本,即32位的dnSpy-net-win32和64位的dnSpy-net-win64,确保了不同操作系统架构的用户都能使用dnSpy进行软件分析。 32位的系统架构相较于64位,由于其地址空间的限制,只能支持最多4GB的内存空间使用,这在处理大型项目时可能会出现不足。而64位的系统能够支持更大的内存空间,使得在处理大型项目时更为方便。随着计算机硬件的发展,64位系统已经成为了主流,因此64位的dnSpy也更加受开发者欢迎。 压缩包文件名“dnSpy-net-win64.7z”和“dnSpy-net-win32.7z”中的“.7z”表示该压缩包采用了7-Zip压缩格式,它是一种开源的文件压缩软件,以其高压缩比著称。在实际使用dnSpy时,用户需要下载对应架构的压缩包进行解压安装,以确保软件能够正确运行在用户的操作系统上。 dnSpy工具V6.1.8版本的发布,对于.NET程序员而言,无论是32位系统还是64位系统用户,都是一个提升工作效率的好工具。用户可以根据自己计算机的操作系统架构,选择合适的版本进行下载使用。而对于希望进行深度分析.NET程序的开发者来说,这个工具更是不可或缺的利器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值