学完C语言之后,我就去阅读《C Primer Plus》这本经典的C语言书籍,对每一章的编程练习题都做了相关的解答,仅仅代表着我个人的解答思路,如有错误,请各位大佬帮忙点出!
1.修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始 化数组)。
#include
#define MONTHS 12
#define YEARS 5
int main(void)
{
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;
}
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++)
{
for (year = 0, subtot = 0; year < YEARS; year++)
{
subtot += *(*(rain + year) + month);
}
printf("%4.1f ", subtot / YEARS);
}
printf("\n");
return 0;
}
2.编写一个程序,初始化一个double类型的数组,然后把该数组的内容 拷贝至3个其他数组中(在main()中声明这4个数组)。使用带数组表示法的 函数进行第1份拷贝。使用带指针表示法和指针递增的函数进行第2份拷贝。 把目标数组名、源数组名和待拷贝的元素个数作为前两个函数的参数。第3 个函数以目标数组名、源数组名和指向源数组最后一个元素后面的元素的指 针。也就是说,给定以下声明,则函数调用如下所示:
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5];
double target2[5];
double target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source + 5);
#include
#define LEN 5
void show_arr(const double x[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-5g", x[i]);
}
putchar('\n');
}
void copy_arr(double x[], const double source[], int n)
{
for (int i = 0; i < n; i++)
{
x[i] = source[i];
}
}
void copy_ptr(double* x, const double* source, int n)
{
for (int i = 0; i < n; i++)
{
*(x + i) = *(source + i);
}
}
void copy_ptrs(double* x, const double* source, const double* end)
{
for (int i = 0; i < end - source; i++)
{
*(x + i) = *(source + i);
}
}
int main(void)
{
double source[LEN] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double target1[LEN];
double target2[LEN];
double target3[LEN];
printf("Source array:\n");
show_arr(source, LEN);
copy_arr(target1, source, LEN);
printf("Target1:\n");
show_arr(target1, LEN);
copy_ptr(target2, source, LEN);
printf("Target2:\n");
show_arr(target2, LEN);
copy_ptrs(target3, source, source + LEN);
printf("Target3:\n");
show_arr(target3, LEN);
printf("Done.\n");
return 0;
}
3.编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的 程序中测试该函数。
#include
int find_max(const int a[], int n)
{
int max = a[0];
for (int i = 1; i < n; i++)
{
max = max < a[i] ? a[i] : max;
}
return max;
}
void show_array(const int a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-3d", a[i]);
}
printf("\n");
}
int main(void)
{
int max;
int array[5] = { 1, 4, 3, 2, 5 };
printf("数组元素为:");
show_array(array, 5);
max = find_max(array, 5);
printf("最大值为%d.\n", max);
return 0;
}
4.编写一个函数,返回储存在double类型数组中最大值的下标,并在一 个简单的程序中测试该函数。
#include
int find_max_index(const double a[], int n)
{
int index = 0;
double maxv = a[0];
for (int i = 1; i < n; i++)
{
if (maxv < a[i])
{
maxv = a[i], index = i;
}
}
return index;
}
void show_array(const double a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-5f ", a[i]);
}
printf("\n");
}
int main(void)
{
int max_index;
double array[5] = { 1.0, 4.0, 3.0, 2.0, 5.0 };
printf("数组元素为:");
show_array(array, 5);
max_index = find_max_index(array, 5);
printf("数组最大值的下标为%d.\n", max_index);
return 0;
}
5.编写一个函数,返回储存在double类型数组中最大值和最小值的差 值,并在一个简单的程序中测试该函数。
#include
double d_value(const double a[], int n)
{
double max = a[0];
double min = a[0];
for (int i = 1; i < n; i++)
{
max = max < a[i] ? a[i] : max;
min = min > a[i] ? a[i] : min;
}
return max - min;
}
void show_array(const double a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-5g", a[i]);
}
printf("\n");
}
int main(void)
{
double array[5] = { 1.0, 4.0, 3.0, 2.0, 5.0 };
printf("数组元素为:");
show_array(array, 5);
printf("数组最大值和最小值的差值为%.2f.\n", d_value(array, 5));
return 0;
}
6.编写一个函数,把double类型数组中的数据倒序排列,并在一个简单 的程序中测试该函数。
#include
void reverse(double a[], int n)
{
for (int i = 0; i < n / 2; i++)
{
double t = a[i];
a[i] = a[n - 1 - i];
a[n - 1 - i] = t;
}
return;
}
void show_array(const double a[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-5g", a[i]);
}
printf("\n");
}
int main(void)
{
double array[5] = { 1.0, 4.0, 3.0, 2.0, 5.0 };
printf("数组元素为:");
show_array(array, 5);
reverse(array, 5);
printf("倒序后的数组为:");
show_array(array, 5);
return 0;
}
7.编写一个程序,初始化一个double类型的二维数组,使用编程练习2中 的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组 是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个 子数组)。
#include
void copy_arr(const double a[], double b[], int n)
{
for (int i = 0; i < n; i++)
{
b[i] = a[i];
}
}
void show_array(double(*x)[3], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%-5g", x[i][j]);
}
printf("\n");
}
}
int main(void)
{
double a[2][3] ={{1.0, 2.0, 3.0},{4.0, 5.0, 6.0}};
double b[2][3] = { 0.0 };
printf("数组a元素为:\n");
show_array(a, 2);
printf("数组b元素为:\n");
show_array(b, 2);
for (int i = 0; i < 2; i++)
{
copy_arr(a[i], b[i], 3);
}
printf("拷贝后的b数组元素为:\n");
show_array(b, 2);
return 0;
}
8.使用编程练习2中的拷贝函数,把一个内含7个元素的数组中第3~第5 个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择 合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素 的地址和待处理元素的个数)。
#include
void copy_arr(double ar1[], const double ar2[], int n)
{
for (int i = 0; i < n; i++)
{
ar1[i] = ar2[i];
}
}
void show_arr(const double ar[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-5g", ar[i]);
}
printf("\n");
}
int main(void)
{
double orig[7] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0 };
double copy[3];
printf("数组元素为:");
show_arr(orig, 7);
printf("拷贝数组中的第3个到第5个元素到新数组里面:");
copy_arr(copy, orig + 2, 3);
show_arr(copy, 3);
return 0;
}
9.编写一个程序,初始化一个double类型的3×5二维数组,使用一个处理 变长数组的函数将其拷贝至另一个二维数组中。还要编写一个以变长数组为 形参的函数以显示两个数组的内容。这两个函数应该能处理任意N×M数组 (如果编译器不支持变长数组,就使用传统C函数处理N×5的数组)。
#include
void show_array(int n, int m, double x[][5])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf("%-5g", x[i][j]);
}
printf("\n");
}
}
void copy_array(int n, int m, double a[][5], double b[][5])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
b[i][j] = a[i][j];
}
}
}
int main(void)
{
double a[3][5] ={{1.0,2.0,3.0,4.0,5.0},{6.0,7.0,8.0,9.0,10.0},{11.0,12.0,13.0,14.0,15.0}};
double b[3][5] = { 0.0 };
printf("数组a的元素:\n");
show_array(3, 5, a);
printf("数组b的元素:\n");
show_array(3, 5, b);
copy_array(3, 5, a, b);
printf("拷贝之后的数组b元素:\n");
show_array(3, 5, b);
return 0;
}
10.编写一个函数,把两个数组中相对应的元素相加,然后把结果储存 到第 3 个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中 包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接 受3个数组名和一个数组大小。在一个简单的程序中测试该函数。
#include
void add_array(const int a[], const int b[], int c[], int n)
{
for (int i = 0; i < n; i++)
{
c[i] = a[i] + b[i];
}
}
void show_array(const int x[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%-3d", x[i]);
}
printf("\n");
}
int main(void)
{
int a[4] = { 2, 4, 5, 8 }, b[4] = { 1, 0, 4, 6 }, c[4] = { 0 };
printf("数组a的元素:");
show_array(a, 4);
printf("数组b的元素:");
show_array(b, 4);
printf("数组c的元素:");
show_array(c, 4);
add_array(a, b, c, 4);
printf("执行相加之后的数组c的元素为:");
show_array(c, 4);
return 0;
}
11.编写一个程序,声明一个int类型的3×5二维数组,并用合适的值初始 化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示 出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素 的值翻倍。这两个函数都以函数名和行数作为参数。
#include
void show(int(*a)[5], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%-5d", a[i][j]);
}
printf("\n");
}
}
void two_times(int(*a)[5], int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 5; j++)
{
a[i][j] *= 2;
}
}
}
int main(void)
{
int a[3][5] = { {1, 2, 3, 4, 5},{6, 7, 8, 9, 10},{11, 12, 13, 14, 15} };
printf("数组a的元素为:\n");
show(a, 3);
two_times(a, 3);
printf("翻倍后的数组元素为:\n");
show(a, 3);
return 0;
}
12.重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数 来完成。
#include
#define MONTHS 12
#define YEARS 5
void rainfall_total(const float(*rain)[MONTHS], int years)
{
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.1lf\n", 2010 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / years);
}
void rainfall_aver(const float(*rain)[MONTHS], int years)
{
float subtot;
int month, year;
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++)
{
for (year = 0, subtot = 0; year < years; year++)
{
subtot += rain[year][month];
}
printf("%4.1f ", subtot / years);
}
putchar('\n');
}
int main(void)
{
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},
};
rainfall_total(rain, YEARS);
rainfall_aver(rain, YEARS);
return 0;
}
13.编写一个程序,提示用户输入3组数,每组数包含5个double类型的数 (假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务。
a.把用户输入的数据储存在3×5的数组中
b.计算每组(5个)数据的平均值
c.计算所有数据的平均值
d.找出这15个数据中的最大值
e.打印结果
每个任务都要用单独的函数来完成(使用传统C处理数组的方式)。完 成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该 函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c 和d的函数应把结果返回主调函数。
#include
#define ROWS 3
#define COLS 5
void store(double ar[], int n)
{
for (int i = 0; i < n; i++)
{
printf("Please enter a number for position %d: ", i + 1);
scanf("%lf", &ar[i]);
}
}
double average2d(double ar[][COLS], int rows)
{
double sum = 0.0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < COLS; j++)
{
sum += ar[i][j];
}
}
return rows * COLS > 0 ? sum / (rows * COLS) : 0.0;
}
double max2d(double ar[][COLS], int rows)
{
double maxv = ar[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < COLS; j++)
{
maxv = maxv < ar[i][j] ? ar[i][j] : maxv;
}
}
return maxv;
}
void showarr2(double ar[][COLS], int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("%-5g", ar[i][j]);
}
printf("\n");
}
}
double average(const double ar[], int n)
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += ar[i];
}
return n > 0 ? sum / n : 0.0;
}
int main(void)
{
double stuff[ROWS][COLS];
for (int row = 0; row < ROWS; row++)
{
printf("Please enter %d numbers for %d row\n", COLS, row + 1);
store(stuff[row], COLS);
}
printf("Array:\n");
showarr2(stuff, ROWS);
for (int row = 0; row < ROWS; row++)
{
printf("Average for row %d is %g.\n", row + 1, average(stuff[row], COLS));
}
printf("Average is %g.\n", average2d(stuff, ROWS));
printf("Maximum is %g.\n", max2d(stuff, ROWS));
printf("Done.\n");
return 0;
}
14.以变长数组作为函数形参,完成编程练习13。
#include
#define ROWS 3
#define COLS 5
void store(int n, double ar[n])
{
for (int i = 0; i < n; i++)
{
printf("Please enter a number for position %d: ", i + 1);
scanf("%lf", &ar[i]);
}
}
double average2d(int rows, int cols, double ar[rows][cols])
{
double sum = 0.0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
sum += ar[i][j];
}
}
return rows * cols > 0 ? sum / (rows * cols) : 0.0;
}
double max2d(int rows, int cols, double ar[rows][cols])
{
double maxv = ar[0][0];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
maxv = maxv < ar[i][j] ? ar[i][j] : maxv;
}
}
return maxv;
}
void showarr2(int rows, int cols, double ar[rows][cols])
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
printf("%-5g", ar[i][j]);
}
printf("\n");
}
}
double average(int n, const double ar[n])
{
double sum = 0.0;
for (int i = 0; i < n; i++)
{
sum += ar[i];
}
return n > 0 ? sum / n : 0.0;
}
int main(void)
{
double stuff[ROWS][COLS];
for (int row = 0; row < ROWS; row++)
{
printf("Please enter %d numbers for %d row\n", COLS, row + 1);
store(COLS, stuff[row]);
}
printf("Array:\n");
showarr2(ROWS, COLS, stuff);
for (int row = 0; row < ROWS; row++)
{
printf("Average for row %d is %g.\n", row + 1, average(COLS, stuff[row]));
}
printf("Average is %g.\n", average2d(ROWS, COLS, stuff));
printf("Maximum is %g.\n", max2d(ROWS, COLS, stuff));
printf("Done.\n");
return 0;
}