第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;
}