10.12 复习题
1. 下面的程序将打印什么内容?
#include <stdio.h>
int main(void)
{
int ref[] = { 8, 4, 0, 2 };
int* ptr;
int index;
for (index = 0, ptr = ref; index < 4; index++, ptr++)
printf("%d %d\n", ref[index], *ptr);
return 0;
}
答案:
2. 在复习题1中,ref有多少个元素?
答案:
数组ref有4个元素,因为初始化列表中的值是4个。
3. 在复习题1中,ref的地址是什么?ref + 1是什么意思?++ref指向什么?
答案:
数组名ref指向该数组的首元素(整数8)。
ref + 1指向数组的第二个元素(整数4)。
++ref不是有效的表达式,因为ref是一个常量,不是变量。
#include <stdio.h>
int main(void)
{
int ref[] = { 8, 4, 0, 2 };
printf("%d\n", *ref);
printf("%d\n", *(ref + 1));
//++ref;
return 0;
}
4. 在下面的代码中,*ptr和*(ptr + 2)的值分别是什么?
#include <stdio.h>
void printA();
void printB();
int main(void)
{
printA();
printB();
return 0;
}
void printA()
{
int* ptr;
int torf[2][2] = { 12, 14, 16 };
ptr = torf[0];
printf("a:\n");
printf("*ptr = %d, *(ptr + 2) = %d\n", *ptr, *(ptr + 2));
printf("\n");
}
void printB()
{
int* ptr;
int fort[2][2] = { { 12 },{ 14, 16 } };
ptr = fort[0];
printf("b:\n");
printf("*ptr = %d, *(ptr + 2) = %d\n", *ptr, *(ptr + 2));
printf("\n");
}
答案:
5. 在下面的代码中,**ptr和**(ptr + 1)的值分别是什么?
#include <stdio.h>
void printA();
void printB();
int main(void)
{
printA();
printB();
return 0;
}
void printA()
{
int (*ptr)[2];
int torf[2][2] = { 12, 14, 16 };
ptr = torf;
printf("a:\n");
printf("**ptr = %d, **(ptr + 1) = %d\n", **ptr, **(ptr + 1));
printf("\n");
}
void printB()
{
int (*ptr)[2];
int fort[2][2] = { { 12 },{ 14, 16 } };
ptr = fort;
printf("b:\n");
printf("**ptr = %d, **(ptr + 1) = %d\n", **ptr, **(ptr + 1));
printf("\n");
}
答案:
6. 假设有下面的声明:
int grid[30][100];
a. 用一种写法表示grid[22][56]的地址
b. 用两种写法表示grid[22][0]的地址
c. 用三种写法表示grid[0][0]的地址
答案:
a. &grid[22][56]
b. &grid[22][0] 或 grid[22]
c. &grid[0][0] 或 grid[0] 或 (int *) grid
7. 正确声明以下变量:
a. digits 是一个内含10个int类型值的数组
b. rates是一个内含6个float类型值得数组
c. mat是一个内含3个元素的数组,每个元素都是内含5个整数的数组
d. psa是一个内含20个元素的数组,每个元素都是指向int的指针
e. pstr是一个指向数组的指针,该数组内含20个char类型的值
答案:
a. int digits[10];
b. float rates[6];
c. int mat[3][5];
d. int* psa[20];注意:[]比*的优先级高,所以在没有圆括号的情况下,psa先与[20]结合,然后再与*结合。因此该声明与char *(psa[20])相同;
e. char (*pstr)[20];注意:对第e小题而言,char *pstr[20];不正确这会让pstr成为一个指针数组,而不是一个指向数组的指针。具体地说,如果使用该声明,pstr就指向一个char类型的值(即数组的第1个成员),而pstr + 1则指向下一个字节。使用正确的声明,pstr是一个变量,而不是一个数组名。而且pstr + 1指向起始字节后面的第20个字节。
8. 正确声明以下变量:
a. 声明一个内含6个int类型值的数组,并初始化各元素1、2、4、8、16、32
b. 用数组表示法表示a声明的数组的第3个元素(其值为4)
c. 假设编译器支持C99/C11标准,声明一个内含100个int类型值的数组,并初始化最后一个元素为-1,其他元素不考虑。
d. 假设编译器支持C99/C11标准,声明一个内含100个int类型值的数组,并初始化下标为5、10、11、12、3的元素为101,其他元素不考虑。
答案:
a. int array[6] = { 1, 2, 4, 8, 16, 32 };
b. array[2];
c. int array[100] = { [99] = -1 };
d. int array[100] = { [5] = 101, [10] = 101, [11] = 101, [12] = 101, [3] = 101 };
9. 内含10个元素的数组下标范围是什么?
答案: 0~9
10. 假设有下面的声明:
float rootbear[10], things[10][5], *pf, value = 2.2;
int i = 3;
判断以下各项是否有效:
a. rootbear[2] = value;
b. scanf("%f", &rootbear);
c. rootbear = value;
d. printf("%f", rootbear);
e. things[4][4] = rootbear[3];
f. things[5] = rootbear;
g. pf = value;
h. pf = rootbear;
答案:
a. 有效
b. 无效,rootbear不是float类型。
c. 无效,rootbear不是float类型。
d. 无效,rootbear不是float类型。
e. 有效
f. 无效,不能用数组赋值。
g. 无效,value不是地址。
h. 有效
11. 声明一个800X600的int类型数组。
答案: int array[800][600];
12. 下面声明了3个数组:
double trots[20];
short clops[10][30];
long shots[5][10][15];
a. 分别以传统方式和以变长数组为参数的方式编写处理trots数组的void函数原型和函数调用
b. 分别以传统方式和以变长数组为参数的方式编写处理clops数组的void函数原型和函数调用
c. 分别以传统方式和以变长数组为参数的方式编写处理shots数组的void函数原型和函数调用
答案:
a.
void process(double ar[], int n);
void processVla(int n, double ar[n]);
process(trots, 20);
processVla(20, trots);
b.
void processTwo(short arTwo[30], int n);
void processTwoVla(int n, int m, short arTwo[n][m]);
processTwo(clops, 10);
processTwoVla(10, 30, clops);
c.
void processThree(long arThree[10][15], int n);
void processThreeVla(int n, int m, int k, long arThree[n][m][k]);
processThree(shots, 5);
processThreeVla(5, 10, 15, shots);
13. 下面有两个函数原型:
void show(const double ar[], int n); //n是数组元素的个数
void show2(const double ar2[], int n); //n是二维数组的行数
a. 编写一个函数调用,把一个内含8、3、9和2的复合字面量传递给show()函数。
b. 编写一个函数调用,把一个2行3列的复合字面量(8、3、9作为第1行,5、4、1作为第二行)传递给show2()函数。
答案:
a. show((int [4]) { 8, 3, 9, 2 }, 4);
b. show2((int [][3]){{ 8, 3, 9 },{ 5, 4, 1 }},2);
10.13 编程练习
1. 修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。
#include <stdio.h>
#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;
const float (*ptr)[MONTHS] = rain;
printf(" YEAR RAINFALL (inches)\n");
for (year = 0, total = 0; year < YEARS; year++)
{
for (month = 0, subtot = 0; month < MONTHS; month++)
{
subtot += *(*(ptr + 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 AVERAGE:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{
for (year = 0, subtot = 0; year < YEARS; year++)
{
subtot += *(*(ptr+year) + month);
}
printf("%4.1f ", subtot / MONTHS);
}
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 targetOne[5];
double targetTwo[5];
double targetThree[5];
copy_arr(targetOne, source, 5);
copy_ptr(targetTwo, source, 5);
copy_ptrs(targetThree, source, source + 5);
#include <stdio.h>
void copy_arr(double targetArray[], double source[], int n);
void copy_ptr(double * targetArray, double * source, int n);
void copy_ptrs(double * targetArray, double * sourceFirst, double * sourceLast);
int main(void)
{
double source[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
double targetOne[5];
double targetTwo[5];
double targetThree[5];
int i;
copy_arr(targetOne, source, 5);
copy_ptr(targetTwo, source, 5);
copy_ptrs(targetThree, source, source + 5);
for (i = 0; i < 5; i++)
printf("%lf ", targetOne[i]);
printf("\n");
for (i = 0; i < 5; i++)
printf("%lf ", targetTwo[i]);
printf("\n");
for (i = 0; i < 5; i++)
printf("%lf ", targetThree[i]);
printf("\n");
return 0;
}
void copy_arr(double targetArray[], double source[], int n)
{
int i;
for (i = 0; i < n; i++)
targetArray[i] = source[i];
}
void copy_ptr(double targetArray[], double source[], int n)
{
int i = 0;
for (i = 0; i < n; i++)
*(targetArray + i) = * (source + i);
}
void copy_ptrs(double* targetArray, double* sourceFirst, double* sourceLast)
{
int i;
for (i = 0; sourceFirst + i < sourceLast; i++)
*(targetArray + i) = * (sourceFirst + i);
}
3. 编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的程序中测试该函数。
#include <stdio.h>
int maxValueInArray(int array[], int n);
int main()
{
int array[5] = { 1, 2, 3, 4, 5 };
printf("The largest number in array is: %d\n", maxValueInArray(array, 5));
return 0;
}
int maxValueInArray(int array[], int n)
{
int i, max = array[0];
for (i = 1; i < n; i++)
{
if (max < array[i])
max = array[i];
}
return max;
}
4. 编写一个函数,返回储存在double类型数组中最大值的下标,并在一个简单的程序中测试该函数。
#include <stdio.h>
int maxIndexInArray(double array[], int n);
int main()
{
double array[5] = { 1, 2, 3, 4, 5 };
printf("The largest number in array is: %d\n", maxIndexInArray(array, 5));
return 0;
}
int maxIndexInArray(double array[], int n)
{
int i, maxIndex = 0;
double maxValue = array[0];
for (i = 1; i < n; i++)
{
if (maxValue < array[i])
{
maxValue = array[i];
maxIndex = i;
}
}
return maxIndex;
}
5. 编写一个函数,返回储存在double类型数组中最大值和最小值的差值,并在一个简单的程序中测试该函数。
#include <stdio.h>
double maxRangenArray(double array[], int n);
int main()
{
double array[5] = { 1, 2, 3, 4, 5 };
printf("The largest number in array is: %.1lf\n", maxRangenArray(array, 5));
return 0;
}
double maxRangenArray(double array[], int n)
{
int i;
double maxValue = array[0], minValue = array[0];
for (i = 1; i < n; i++)
{
if (maxValue < array[i])
{
maxValue = array[i];
}
if (minValue > array[i])
{
minValue = array[i];
}
}
return maxValue - minValue;
}
6. 编写一个函数,把double类型数组中的数据倒叙排序,并在一个简单的程序中测试该函数。
#include <stdio.h>
#define SIZE 5
void reserveArray(double array[], int n);
int main()
{
int i;
double array[SIZE] = { 1, 2, 3, 4, 5 };
for (i = 0; i < SIZE; i++)
{
printf("%g ", array[i]);
}
printf("\n");
reserveArray(array, SIZE);
for (i = 0; i < SIZE; i++)
{
printf("%g ", array[i]);
}
printf("\n");
return 0;
}
void reserveArray(double array[], int n)
{
int i, j;
double temp;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n -1 -i; j++)
{
if (array[j] < array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
}
7. 编写一个程序,初始化一个double类型的二维数组,使用编程练习2中的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个子数组)。
#include <stdio.h>
#define ROWS 2
#define COLS 4
void copy_arr(double targetArray[], double source[], int n);
void copy_ptr(double* targetArray, double* source, int n);
void copy_ptrs(double* targetArray, double* sourceFirst, double* sourceLast);
void copy_2d_array(double target[][COLS], double source[][COLS], int n);
void copy_2d_ptr(double (*target)[COLS], double (*source)[COLS], int n);
int main(void)
{
int i, j;
double targetOne[ROWS][COLS], targetTwo[ROWS][COLS], source[ROWS][COLS] = {{1.1, 2.2, 3.3, 4.4}, {5.5, 6.6, 7.7, 8.8}};
copy_2d_array(targetOne, source, 2);
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%g ", targetOne[i][j]);
}
printf("\n");
}
printf("\n\n\n");
copy_2d_ptr(targetTwo, source, 2);
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%g ", targetTwo[i][j]);
}
printf("\n");
}
return 0;
}
void copy_2d_array(double target[][COLS],double source[][COLS], int n)
{
int i;
for (i = 0; i < n; i++)
copy_arr(target[i], source[i], COLS);
}
void copy_2d_ptr(double (*target)[COLS], double (*source)[COLS], int n)
{
int i;
for (i = 0; i < n; i++)
copy_ptr(*(target + i), *(source + i), COLS);
}
void copy_arr(double targetArray[], double source[], int n)
{
int i;
for (i = 0; i < n; i++)
targetArray[i] = source[i];
}
void copy_ptr(double targetArray[], double source[], int n)
{
int i = 0;
for (i = 0; i < n; i++)
*(targetArray + i) = *(source + i);
}
void copy_ptrs(double* targetArray, double* sourceFirst, double* sourceLast)
{
int i;
for (i = 0; sourceFirst + i < sourceLast; i++)
*(targetArray + i) = *(sourceFirst + i);
}
8. 使用编程练习2中的拷贝函数,把一个内含7个元素的数组中第3~第5个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素的地址,和待处理元素的个数)。
#include <stdio.h>
#define SIZE 3
void copy_ptr(double* targetArray, double* source, int n);
int main(void)
{
double source[7] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
double targetOne[SIZE];
int i;
copy_ptr(targetOne, source + 2, SIZE);
for (i = 0; i < SIZE; i++)
printf("%g ", targetOne[i]);
printf("\n");
return 0;
}
void copy_ptr(double targetArray[], double source[], int n)
{
int i;
for (i = 0; i < n; i++)
*(targetArray + i) = *(source + i);
}
9. 编写一个程序,初始化一个double类型的3X5二维数组,使用一个处理变长数组的函数将其拷贝至另一个二维数组中。还要编写一个以变长数组为形参的函数以显示两个数组的内容。这两个函数应该能处理任意NXM数组(如果编译器不支持变长数组,就使用传统C函数处理NX5的数组)。
#include <stdio.h>
#define N 3
#define M 5
void copy_array(int n, int m, double target[][M], const double source[][M]);
void show_array(int n, int m, const double array[][M]);
int main(void)
{
int n = 3;
int m = 5;
double target[N][M], source[][5] = {
{ 0.2, 0.4, 2.4, 3.5, 6.6 },
{ 0.5, 8.2, 1.2, 1.6, 2.4 },
{ 9.1, 8.5, 2.3, 6.1, 8.4 }
};
copy_array(n,m,target,source);
show_array(n,m,target);
return 0;
}
void copy_array(int n, int m, double target[][M], const double source[][M])
{
int i, j;
for (i = 0; i < n; i++)
for (j = 0; j < m; j++)
target[i][j] = source[i][j];
}
void show_array(int n, int m, const double array[][M])
{
int i, j;
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
printf("%g ", array[i][j]);
}
printf("\n");
}
}
10. 编写一个函数,把两个数组中相对应的元素相加,然后把结果储存到第3个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接受3个数组名和一个数组大小。在一个简单的程序中测试该函数。
#include <stdio.h>
#define SIZE 4
void copyArray(const int arrayOne[],const int arrayTwo[], int copyArray[], int n);
int main(void)
{
int arrayOne[SIZE] = {2, 4, 5, 8};
int arrayTwo[SIZE] = {1, 0, 4, 6};
int targetArray[SIZE];
int i;
copyArray(arrayOne, arrayTwo, targetArray, SIZE);
for (i = 0; i < SIZE; i++)
printf("%d ", targetArray[i]);
return 0;
}
void copyArray(const int arrayOne[], const int arrayTwo[], int copyArray[], int n)
{
int i;
for (i = 0; i < n; i++)
{
copyArray[i] = arrayOne[i] + arrayTwo[i];
}
}
11. 编写一个程序,声明一个int类型的3X5二维数组,并用合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原来的2倍),并显示出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素翻倍。这两个函数都以函数名和行数作为参数。
#include <stdio.h>
#define ROWS 3
#define COLS 5
void show_element(int rows, int cols, const int (*array)[COLS]);
void double_element(int rows, int cols, int (*array)[COLS]);
int main(void)
{
int array[ROWS][COLS] = {
{ 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 10},
{ 11, 12, 13, 14, 15 }
};
show_element(ROWS, COLS, array);
double_element(ROWS, COLS, array);
printf("\n");
show_element(ROWS, COLS, array);
return 0;
}
void show_element(int rows, int cols, const int (*array)[COLS])
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%d ", *(*(array + i) + j));
}
printf("\n");
}
}
void double_element(int rows, int cols, int (*array)[COLS])
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
*(*(array + i) + j) *= 2;
}
}
}
12. 重写程序清单10.7的rain.c程序,把main()中的主要任务都改成用函数来完成。
#include <stdio.h>
#define MONTHS 12
#define YEARS 5
void year_average(int years, int months, const float array[][MONTHS]);
void month_average(int years, int months, const float array[][MONTHS]);
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}
};
year_average(YEARS, MONTHS, rain);
month_average(YEARS, MONTHS, rain);
printf("\n");
return 0;
}
void year_average(int years, int months, const float array[][MONTHS])
{
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 += array[year][month];
}
printf("%5d %15.1f\n", 2010 + year, subtot);
total += subtot;
}
printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
}
void month_average(int years, int months, const float array[][MONTHS])
{
int year, month;
float subtot, total;
printf("MONTHLY AVERAGE:\n\n");
printf(" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec\n");
for (month = 0; month < MONTHS; month++)
{
for (year = 0, subtot = 0; year < YEARS; year++)
{
subtot += array[year][month];
}
printf("%4.1f ", subtot / MONTHS);
}
}
13. 编写一个程序,提示用户输入3组数,每组数包含5个double类型的数(假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务:
a. 把用户输入的数据储存在3X5的数组中
b. 计算每组(5个)数据的平均值
c. 计算所有数据的平均值
d. 找出这15个数据中的最大值
e. 打印结果
每个任务都要使用单独的函数来完成(使用传统C处理数组的方式)。完成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c和d的函数应把结果返回主调函数。
#include <stdio.h>
#define ROWS 3
#define COLS 5
void input_array(int rows, double ar[][COLS]);
double col_average(int rows, const double ar[]);
double array_average(int rows, const double ar[][COLS]);
double array_max_number(int rows, const double ar[][COLS]);
void show_result(int rows, const double ar[][COLS]);
int main()
{
double array[ROWS][COLS];
input_array(ROWS, array);
show_result(ROWS, array);
printf("\n");
return 0;
}
void input_array(int rows, double ar[][COLS])
{
int i, j;
printf("Enter the array number.\n");
for (i = 0; i < rows; i++)
{
printf("Enter five double number:\n");
for (j = 0; j < COLS; j++)
{
scanf_s("%lf", &ar[i][j]);
}
}
}
double col_average(int cols, const double ar[])
{
int i;
double sum = 0;
for (i = 0; i < cols; i++)
sum += ar[i];
return sum / cols;
}
double array_average(int rows, const double ar[][COLS])
{
int i = 0;
double sum = 0;
for (i = 0; i < rows; i++)
sum += col_average(COLS, ar[i]);
return sum / rows;
}
double array_max_number(int rows, const double ar[][COLS])
{
int i, j;
double max = ar[0][0];
for (i = 0; i < rows; i++)
{
for (j = 0; j < COLS; j++)
{
if (max < ar[i][j])
max = ar[i][j];
}
}
return max;
}
void show_result(int rows, const double ar[][COLS])
{
int i, j;
printf("The array you input is:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < COLS; j++)
{
printf("%g ", ar[i][j]);
}
printf("\n");
}
printf("The average of every column is:\n");
for (i = 0; i < rows; i++)
{
printf("The %d column's average is %g.\n", i, col_average(COLS, ar[i]));
}
printf("The array's data average is %g.\n", array_average(ROWS, ar));
printf("The max datum in the array is %g.\n", array_max_number(ROWS, ar));
}
13. 以变长数组作为函数形参,完成编程练习13。
#include <stdio.h>
#define ROWS 3
#define COLS 5
void input_array(int rows, int cols, double ar[][COLS]);
double col_average(int rows, const double ar[]);
double array_average(int rows, int cols, const double ar[][COLS]);
double array_max_number(int rows, int cols, const double ar[][COLS]);
void show_result(int rows, int cols, const double ar[][COLS]);
int main()
{
double array[ROWS][COLS];
input_array(ROWS, COLS, array);
show_result(ROWS, COLS, array);
printf("\n");
return 0;
}
void input_array(int rows, int cols, double ar[][COLS])
{
int i, j;
printf("Enter the array number.\n");
for (i = 0; i < rows; i++)
{
printf("Enter five double number:\n");
for (j = 0; j < cols; j++)
{
scanf_s("%lf", &ar[i][j]);
}
}
}
double col_average(int cols, const double ar[])
{
int i;
double sum = 0;
for (i = 0; i < cols; i++)
sum += ar[i];
return sum / cols;
}
double array_average(int rows, int cols, const double ar[][COLS])
{
int i = 0;
double sum = 0;
for (i = 0; i < rows; i++)
sum += col_average(COLS, ar[i]);
return sum / rows;
}
double array_max_number(int rows,int cols, const double ar[][COLS])
{
int i, j;
double max = ar[0][0];
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
if (max < ar[i][j])
max = ar[i][j];
}
}
return max;
}
void show_result(int rows, int cols, const double ar[][COLS])
{
int i, j;
printf("The array you input is:\n");
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
printf("%g ", ar[i][j]);
}
printf("\n");
}
printf("The average of every column is:\n");
for (i = 0; i < rows; i++)
{
printf("The %d column's average is %g.\n", i, col_average(COLS, ar[i]));
}
printf("The array's data average is %g.\n", array_average(ROWS,COLS, ar));
printf("The max datum in the array is %g.\n", array_max_number(ROWS,COLS, ar));
}