《C Primer Plus》第10章复习题与编程练习

本文详细解答了《C Primer Plus》第10章复习题,涉及数组操作、指针理解、内存地址、数组复制、函数调用等内容,通过实例演示和编程练习巩固C语言基础知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

复习题

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

8 8
4 4
0 0
2 2

2. 在复习题1中,ref有多少个元素?

4

3. 在复习题1中,ref的地址是什么?ref + 1是什么意思?++ref指向什么?

该数组的第一个元素的地址。
该数组第二个元素的地址。
++ref不是个有效的表达式,因为ref是常量,不是变量。

4. 在下面的代码中,*ptr 和 *(ptr + 2)的值分别是什么

a.

int *ptr;
int torf[2][2] = {12, 14, 16};
ptr = torf[0];

b.

int * ptr;
int fort[2][2] = { {12}, {14,16} };
ptr = fort[0];

a. 12 16
b. 12 14

5. 在下面的代码中,**ptr 和 **(ptr + 1)的值分别是什么?

a.

int (*ptr)[2];
int torf[2][2] = {12, 14, 16};
ptr = torf;

b.

int (*ptr)[2];
int fort[2][2] = { {12}, {14,16} };
ptr = fort;

a. 12 16
b. 12 14

6. 假设有下面的声明:

int grid[30][100];

a. 用1种写法表示grid[22][56]的地址
b. 用2种写法表示grid[22][0] 的地址
c. 用3种写法表示grid[0][0]的地址

a.
&grid[22][56]
b.
grid[22]
&grid[22][0]
c.
grid
grid[0]
&grid[0][0]

7. 正确声明以下各变量:

a. digits是一个内含10个int类型值的数组
b. rates是一个内含6个float类型值的数组
c. mat是一个内含3个元素的数组,每个元素都是内含5个整数的数组
d. psa是一个内含20个元素的数组,每个元素都是指向int的指针
e. pstr是一个指向数组的指针,该数组内含20个char类型的值

a. int gigits[10];
b. float rates[6];
c. int mat[3][5];
d. int *psa[20];
e. char (*pstr)[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 a[] = {1, 2, 4, 8, 16, 32};
b. a[2]
c. int a[]={[99] = -1};//可以指明或不指明数组大小,不指明默认100,指明比100小编译器报错
d. int a[100] = {[5] = 101, [10] = 101, 101, 101, [3] = 101};//不能写成[5] = [10] = … = 101

9. 内含10个元素的数组下标范围是什么?

0~9

10. 假设有下面的声明:

float rootbeer[10], things[10][5], *pf, value = 2.2;
int i = 3;

判断以下各项是否有效:
a. rootbeer[2] = value;
b. scanf(“%f”, &rootbeer );
c. rootbeer = value;
d. printf(“%f”, rootbeer);
e. things[4][4] = rootbeer[3];
f. things[5] = rootbeer;
g. pf = value;
h.pf = rootbeer;

a. o
b. x
c. x
d. x
e. o
f. x
g. x
h. o

11. 声明一个800×600的int类型数组

int a[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 func(double []);
void func(int, double [*]);
 
/*
题目说的是调用- -自闭
void func(double a[]){}
void func(int a, double b[a]){}
*/
func(trots);
func(20, trots);

b.

void func(short [][30]);
void func(int, int, short [*][*]);
 
/*
void func(short a[][30]){}
void func(int a, int b, short c[a][b]){}
*/
func(clops);
func(10, 30, clops);

c.

void func(long [][10][15]);
void func(int, int, int, long[*][*][*]);
 
/*
void func(long a[][10][15]){}
void func(int a, int b, int c, long d[a][b][c]){}
*/
func(shots);
func(5, 10, 15, shots);

13. 下面有两个函数原型:

void show(const double ar[], int n); // n是数组元素的个数
void show2(const double ar2[][3], int n); // n是二维数组的行数
a. 编写一个函数调用,把一个内含8、3、9和2的复合字面量传递给show()函数。
b. 编写一个函数调用,把一个2行3列的复合字面量(8、3、9作为第1行,5、4、1作为第2行)传递给show2()函数

a.

show((double []){8, 3, 9, 2}, 4);

b.

show((double [][3]){{8, 3, 9},{5, 4, 1}}, 2);//二维里的3不能落下

编程练习

1. rain.c

修改程序清单10.7的rain.c程序,用指针进行计算(仍然要声明并初始化数组)。

代码:

#include <stdio.h>
#include <stdlib.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 %11.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");

    system("pause");
    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 <stdio.h>
#include <stdlib.h>
void copy_arr(double *target1, double *source, int n);
void copy_ptr(double *target2, double *source, int n);
void copy_ptrs(double *target3, double *source1, double *source2);
void print_arr(double *arr, int n);
int main(void)
{

    double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
    double target1[5];
    double target2[5];
    double target3[5];

    print_arr(source, 5);
    copy_arr(target1, source, 5);
    copy_ptr(target2, source, 5);
    copy_ptrs(target3, source, source + 5);
    print_arr(target1, 5);
    print_arr(target2, 5);
    print_arr(target3, 5);

    system("pause");
    return 0;
}
void copy_arr(double *target1, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        target1[i] = source[i];
    }
}
void copy_ptr(double *target2, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        *(target2 + i) = *(source + i);
    }
}
void copy_ptrs(double *target3, double *source1, double *source2)
{
    double *i;
    int j;
    for (i = source1, j = 0; i < source2; i++, j++)
    {
        *(target3 + j) = *i;
    }
}
void print_arr(double *arr, int n)
{
    for (int i = 0; i < n; i++)
        printf("%.2lf ", *(arr + i));
    printf("\n");
}

3. 最大值

编写一个函数,返回储存在int类型数组中的最大值,并在一个简单的程序中测试该函数。

代码:

#include <stdio.h>
#include <stdlib.h>
int arr_max(int arr[], int n);
int main(void)
{
    int a[5] = {1, 3, 5, 7, 9};
    printf("The max element in array is %d.\n", arr_max(a, 5));

    system("pause");
    return 0;
}
int arr_max(int arr[], int n)
{
    int max = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > max)
            max = arr[i];
    }
    return max;
}

4. 最大值的下标

编写一个函数,返回储存在double类型数组中最大值的下标,并在一个简单的程序中测试该函数。

代码:

#include <stdio.h>
#include <stdlib.h>
int arr_max_index(double arr[], int n);
int main(void)
{
    double a[5] = {1.0, 3.0, 5.0, 7.0, 9.0};
    printf("The index of the max element in array is %d.\n", arr_max_index(a, 5));

    system("pause");
    return 0;
}
int arr_max_index(double arr[], int n)
{
    double max = arr[0];
    int max_index = 0;
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
            max_index = i;
        }
    }
    return max_index;
}

5. 最大值和最小值的差值

编写一个函数,返回储存在double类型数组中最大值和最小值的差值,并在一个简单的程序中测试该函数。

代码:

#include <stdio.h>
#include <stdlib.h>
double difference(double arr[], int n);
int main(void)
{
    double a[5] = {1.0, 3.0, 5.0, 7.0, 9.0};
    printf("The index of the max element in array is %.2lf.\n", difference(a, 5));

    system("pause");
    return 0;
}
double difference(double arr[], int n)
{
    double max, min;
    max = min = arr[0];
    for (int i = 1; i < n; i++)
    {
        if (arr[i] > max)
            max = arr[i];
        if (arr[i] < min)
            min = arr[i];
    }
    return max - min;
}

6. 倒序排列

编写一个函数,把double类型数组中的数据倒序排列,并在一个简单的程序中测试该函数。

代码:

#include <stdio.h>
#include <stdlib.h>
void show_arr(double arr[], int n);
void swap(double *a, double *b);
void reverse_sort(double arr[], int n);
int main(void)
{
    double a[5] = {1.0, 3.0, 5.0, 7.0, 9.0};
    printf("Original array: ");
    show_arr(a, 5);
    reverse_sort(a, 5);
    printf("After sorting: ");
    show_arr(a, 5);

    system("pause");
    return 0;
}
void show_arr(double arr[], int n)
{
    for (int i = 0; i < n; i++)
        printf("%.2lf ", arr[i]);
    printf("\n");
}
void swap(double *a, double *b)
{
    double temp = *a;
    *a = *b;
    *b = temp;
}
void reverse_sort(double arr[], int n)
{
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (arr[i] < arr[j])
                swap(&arr[i], &arr[j]);
        }
    }
}

7. 拷贝二维数组

编写一个程序,初始化一个double类型的二维数组,使用编程练习2中的一个拷贝函数把该数组中的数据拷贝至另一个二维数组中(因为二维数组是数组的数组,所以可以使用处理一维数组的拷贝函数来处理数组中的每个子数组)。

代码:

#include <stdio.h>
#include <stdlib.h>
void copy_arr(double *target1, double *source, int n);
void show_arr(double (*arr)[3], int n);
int main(void)
{
    double a[3][3] = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}};
    double copy[3][3];

    printf("Original array: \n");
    show_arr(a, 3);
    for (int i = 0; i < 3; i++)
    {
        copy_arr(copy[i], a[i], 3);
    }
    printf("Copy array: \n");
    show_arr(copy, 3);

    system("pause");
    return 0;
}
void copy_arr(double *target1, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        target1[i] = source[i];
    }
}
void show_arr(double (*arr)[3], int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%.2lf ", arr[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

使用变长数组:

#include <stdio.h>
#include <stdlib.h>
#define COLS 3
void copy_arr(double *target1, double *source, int n);
void show_arr(double arr[][COLS], int n);
int main(void)
{
    int n = 3;
    double a[n][COLS] = {{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}};
    double copy[n][COLS];

    printf("Original array: \n");
    show_arr(a, n);
    for (int i = 0; i < n; i++)
    {
        copy_arr(copy[i], a[i], COLS);
    }
    printf("Copy array: \n");
    show_arr(copy, n);

    system("pause");
    return 0;
}
void copy_arr(double *target1, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        target1[i] = source[i];
    }
}
void show_arr(double arr[][COLS], int n)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%.2lf ", arr[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}

8. 编程练习2的拷贝函数之再利用

使用编程练习2中的拷贝函数,把一个内含7个元素的数组中第3~第5个元素拷贝至内含3个元素的数组中。该函数本身不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组大小,只需要是数组元素的地址和待处理元素的个数)。

代码:

#include <stdio.h>
#include <stdlib.h>
void copy_arr(double *target1, double *source, int n);
void copy_ptr(double *target2, double *source, int n);
void copy_ptrs(double *target3, double *source1, double *source2);
void print_arr(double *arr, int n);
int main(void)
{

    double source[7] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};
    double target1[3];
    double target2[3];
    double target3[3];

    print_arr(source, 7);
    copy_arr(target1, source + 2, 3);
    copy_ptr(target2, &source[2], 3);
    copy_ptrs(target3, source + 2, source + 5);
    print_arr(target1, 3);
    print_arr(target2, 3);
    print_arr(target3, 3);

    system("pause");
    return 0;
}
void copy_arr(double *target1, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        target1[i] = source[i];
    }
}
void copy_ptr(double *target2, double *source, int n)
{
    for (int i = 0; i < n; i++)
    {
        *(target2 + i) = *(source + i);
    }
}
void copy_ptrs(double *target3, double *source1, double *source2)
{
    double *i;
    int j;
    for (i = source1, j = 0; i < source2; i++, j++)
    {
        *(target3 + j) = *i;
    }
}
void print_arr(double *arr, int n)
{
    for (int i = 0; i < n; i++)
        printf("%.2lf ", *(arr + i));
    printf("\n");
}

9. 拷贝可变二维数组

编写一个程序,初始化一个double类型的3×5二维数组,使用一个处理变长数组的函数将其拷贝至另一个二维数组中。还要编写一个以变长数组为形参的函数以显示两个数组的内容。这两个函数应该能处理任意N×M数组(如果编译器不支持变长数组,就使用传统C函数处理N×5的数组)。

代码:

#include <stdio.h>
#include <stdlib.h>
#define COLS 5
void copy_arr(int rows, int cols, double target[][COLS], double source[][COLS]);
void show_arr(int rows, int cols, double arr[][COLS]);
int main(void)
{
    int n = 3;
    int m = 5;
    double a[n][COLS] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
    double copy[n][COLS];

    printf("Original array: \n");
    show_arr(n, m, a);
    copy_arr(n, m, copy, a);
    printf("Copy array: \n");
    show_arr(n, m, copy);

    system("pause");
    return 0;
}
void copy_arr(int rows, int cols, double target[][COLS], double source[][COLS])
{
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            target[i][j] = source[i][j];
}
void show_arr(int rows, int cols, double arr[][COLS])
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%-5.2lf ", arr[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

10. 数组中相对应的元素相加

编写一个函数,把两个数组中相对应的元素相加,然后把结果储存到第 3 个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接受3个数组名和一个数组大小。在一个简单的程序中测试该函数。

代码:

#include <stdio.h>
#include <stdlib.h>
void show_arr(int *arr, int n);
void add_arr(const int *arr1, const int *arr2, int *sum, int n);
int main(void)
{
    int n = 5;
    int a[n] = {1, 3, 5, 7, 9};
    int b[n] = {2, 4, 6, 8, 10};
    int sum[n];

    printf("array a: ");
    show_arr(a, n);
    printf("array b: ");
    show_arr(b, n);
    add_arr(a, b, sum, n);
    printf("array sum: ");
    show_arr(sum, n);

    system("pause");
    return 0;
}
void show_arr(int *arr, int n)
{
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    putchar('\n');
}
void add_arr(const int *arr1, const int *arr2, int *sum, int n)
{
    for (int i = 0; i < n; i++)
        sum[i] = arr1[i] + arr2[i];
}

11. 翻倍机会

编写一个程序,声明一个int类型的3×5二维数组,并用合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显示出各元素的新值。编写一个函数显示数组的内容,再编写一个函数把各元素的值翻倍。这两个函数都以数组名和行数作为参数。

代码:

#include <stdio.h>
#include <stdlib.h>
#define COLS 5
void double_arr(int rows, int cols, int arr[][COLS]);
void show_arr(int rows, int cols, int arr[][COLS]);
int main(void)
{
    int n = 3;
    int m = 5;
    int a[n][COLS] = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};

    printf("Original array: \n");
    show_arr(n, m, a);
    double_arr(n, m, a);
    printf("After doubling: \n");
    show_arr(n, m, a);

    system("pause");
    return 0;
}
void double_arr(int rows, int cols, int arr[][COLS])
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            arr[i][j] *= 2;
        }
    }
}
void show_arr(int rows, int cols, int arr[][COLS])
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%-3d ", arr[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

12. rain.c

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

代码:

#include <stdio.h>
#include <stdlib.h>
#define MONTHS 12 // 一年的月份数
#define YEARS 5   // 年数
void avg_year(const float rain[YEARS][MONTHS]);
void avg_month(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}};
    avg_year(rain);
    avg_month(rain);

    system("pause");
    return 0;
}
void avg_year(const float rain[YEARS][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 += rain[year][month];
        printf("%5d %11.1f\n", 2010 + year, subtot);
        total += subtot; // 5年的总降水量
    }
    printf("\nThe yearly average is %.1f inches.\n\n", total / YEARS);
}
void avg_month(const float rain[YEARS][MONTHS])
{
    int year, month;
    float subtot, total;

    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");
}

13. 数组统计

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

代码:

#include <stdio.h>
#include <stdlib.h>
#define COLS 5
void store_arr(double arr[][COLS]);
double line_avg(double *arr, int n);
void average_arr(double arr[][COLS], int rows, int cols);
void max_arr(double arr[][COLS], int rows, int cols);
void show_arr(double arr[][COLS], int rows, int cols);
int main(void)
{
    int n = 3;
    int m = 5;
    double a[n][COLS];

    store_arr(a);
    show_arr(a, n, m);
    for (int i = 0; i < n; i++)
        printf("The average of line %d is %.2lf.\n", i, line_avg(a[i], m));
    average_arr(a, n, m);
    max_arr(a, n, m);

    system("pause");
    return 0;
}
void store_arr(double arr[][COLS])
{
    printf("Enter 5 values of double for 3 lines.\n");
    printf("first line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[0][i]);
    printf("second line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[1][i]);
    printf("third line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[2][i]);
}
double line_avg(double *arr, int n)
{
    double avg = 0;
    for (int i = 0; i < n; i++)
        avg += arr[i];
    avg /= n;
    return avg;
}
void average_arr(double arr[][COLS], int rows, int cols)
{
    double avg = 0;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            avg += arr[i][j];
    avg /= (rows * cols);
    printf("The average of this array is %.2lf.\n", avg);
}
void max_arr(double arr[][COLS], int rows, int cols)
{
    double max = **arr;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            if (arr[i][j] > max)
                max = arr[i][j];
    printf("The max of this array is %.2lf.\n", max);
}
void show_arr(double arr[][COLS], int rows, int cols)
{
    printf("Your input is already stored in an array:\n");
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%-5.2lf ", arr[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}

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

同编程练习13。

代码:

#include <stdio.h>
#include <stdlib.h>
#define COLS 5
void store_arr(double arr[][COLS]);
double line_avg(double *arr, int n);
void average_arr(double arr[][COLS], int rows, int cols);
void max_arr(double arr[][COLS], int rows, int cols);
void show_arr(double arr[][COLS], int rows, int cols);
int main(void)
{
    int n = 3;
    int m = 5;
    double a[n][COLS];

    store_arr(a);
    show_arr(a, n, m);
    for (int i = 0; i < n; i++)
        printf("The average of line %d is %.2lf.\n", i, line_avg(a[i], m));
    average_arr(a, n, m);
    max_arr(a, n, m);

    system("pause");
    return 0;
}
void store_arr(double arr[][COLS])
{
    printf("Enter 5 values of double for 3 lines.\n");
    printf("first line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[0][i]);
    printf("second line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[1][i]);
    printf("third line: ");
    for (int i = 0; i < COLS; i++)
        scanf("%lf", &arr[2][i]);
}
double line_avg(double *arr, int n)
{
    double avg = 0;
    for (int i = 0; i < n; i++)
        avg += arr[i];
    avg /= n;
    return avg;
}
void average_arr(double arr[][COLS], int rows, int cols)
{
    double avg = 0;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            avg += arr[i][j];
    avg /= (rows * cols);
    printf("The average of this array is %.2lf.\n", avg);
}
void max_arr(double arr[][COLS], int rows, int cols)
{
    double max = **arr;
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < cols; j++)
            if (arr[i][j] > max)
                max = arr[i][j];
    printf("The max of this array is %.2lf.\n", max);
}
void show_arr(double arr[][COLS], int rows, int cols)
{
    printf("Your input is already stored in an array:\n");
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            printf("%-5.2lf ", arr[i][j]);
        }
        putchar('\n');
    }
    putchar('\n');
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

UestcXiye

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值