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

《C Primer Plus》习题第十章复习题 复习题 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[ind... 阅读详情

复习题

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');
}
C Primer Plus第4章编程题 printf()和scanf()的*修饰符 C Primer Plus第4章编程题 阅读详情

相关推荐

《C Primer Plus》第 11 章复习题和编程练习

《C Primer Plus》第 11 章复习题和编程练习

潇雪的博客 1132

C Primer Plus 第十章 课后答案

目录   复习题 1.下面的程序将打印什么内容 2.在复习题1中,ref有多少个元素 3.在复习题1中,ref的地址是什么?ref + 1是什么意思?++ref指向什么 4.在下面的代码中,*ptr和*(ptr + 2)的值分别是什么 a. int *ptr; int torf[2][2] = {12, 14, 16}; ptr = torf[0]; b. int * ptr...

asdgyy的博客 1万+

ubuntu24.04+RTX5090D 显卡驱动安装

本文详细介绍了在Ubuntu系统中更新内核、安装NVIDIA驱动的步骤。首先,通过mainline工具安装新版内核,并确保GCC版本兼容。接着,在无桌面环境下配置TTY模式,解决中文显示问题。然后,禁用默认的nouveau驱动,安装NVIDIA驱动,并选择开源驱动以避免黑屏问题。最后,重新启用图形界面,并通过测试软件验证显卡功能。整个过程涵盖了内核更新、驱动安装、TTY配置及图形界面恢复等多个关键步骤,适合需要手动配置NVIDIA驱动的用户参考。

妖米的博客 4684

c语言从文件中读取数据存入数组_C语言在计算数组平均值的函数中如何用变长数组做函数的形参(C Primer Plus 10-14)...

程序内容: 以变长数组作为函数形参,完成编程练习13. 关于编程练习13的参考代码我也写出来了,就在上一篇文章。 程序为C Primer Plus第十章第十四题的答案示例附:程序代码为本人亲自编写,并非书内标准答案,仅供参考。如果程序有任何bug或者有好的建议,可以在评论区留言,或者私信。程序主调函数代码:#include程序deal.h头文件:#includeSource code fil...

weixin_39626927的博客 184

(UE5 5.2)HISM Mobile DrawInstance在渲染层的实现浅分析

在 (UE4 4.27) UHierarchicalInstancedStaticMesh(HISM)原理分析 这篇博客大致介绍HISM组件从游戏线程到渲染线程的重建KD-Tree和剔除并提交DrawCall逻辑,但是没有分析渲染层的大致数据结构和实现. 可以看出FHierarchicalStaticMeshSceneProxy 实际存储Instance数组数据来自于FInstancedStaticMeshSceneProxy FHierarchicalStaticMeshSceneProxy只是以FClu

qq_29523119的博客 2730

保姆级教程:用STM32单片机实现国标交流充电桩的CP信号检测(附完整代码)

本文详细介绍了基于STM32单片机实现国标交流充电桩CP信号检测的全流程,包括硬件设计、软件架构和充电控制实现。通过分压电路设计、ADC配置和状态机逻辑,确保充电过程的安全与可靠性,适用于新能源汽车充电桩开发。附完整代码和调试技巧,助力开发者快速掌握关键技术。

weixin_42641998的博客 258

C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

C Primer Plus (中文版)第10章编程练习 参考答案(仅供参考~)

成为富婆,报效祖国!! 1166

静态时序分析—伪路径(set_false_path)

set_false_path应用场景

m0_61544122的博客 2万+

C Primer Plus第十章学习笔记和复习题

a.digits是一个内含10个int类型值的数组b.rates是一个内含6个float类型值的数组c.mat是一个内含3个元素的数组,每个元素都是内含5个整数的数组d.psa是一个内含20个元素的数组,每个元素都是指向int的指针e.pstr是一个指向数组的指针,该数组内含20个char类型的值a.声明一个内含6个int类型值的数组,并初始化各元素为1、2、4、8、 16、32b.用数组表示法表示a声明的数组的第3个元素(其值为4)

int_coco_xie的博客 478

secureCRT 破解方法

3、输入注册信息打开keygen.exe,点击path,选择SecureCRT的安装目录(例如D:\VanDykeSoftware\Clients)的securecrt.exe打开,继续下一步打开LicenseHelper.exe,如下。1、注册SecureCRT9.1打开keygen.exe,出现以下问题(没有关掉安全防护,一双击打开,Windows会自动识别keygen.exe为bingdu并删除)打开桌面SecureCRT,此界面不输入内容,直接点击下一步。2、存储配置文件夹(可以默认安装)

逆水行舟 1万+

XDMA Scatter-Gather(SG)模式

XDMA Scatter-Gather(SG)模式 是 Xilinx FPGA 中用于高效处理 ​非连续内存数据 的 DMA 传输机制,通过 ​描述符链表(Descriptor List)​ 管理多块分散数据,显著减少 CPU 开销并提升吞吐量。

浩瀚之水的专栏 2153

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

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

潇雪的博客 1521

DeiT详解:知识蒸馏的Transformer

DeiT模型(8600万参数)仅用一台 GPU 服务器在 53 hours train,20 hours finetune,仅使用 ImageNet 就达到了 84.2 top-1 准确性,而无需使用任何外部数据进行训练,性能与最先进的卷积神经网络(CNN)可以抗衡。其核心是提出了针对 ViT 的教师-学生蒸馏训练策略,并提出了方法,使得在视觉领域训练得又快又好。

走在深度学习前沿的小宋 2642

C Primer Plus 第七章编程练习(第10题)

#include <stdio.h> #define SINGLE 17850 /*单身缴纳税金基础*/ #define HEAD_OF_A_HOUSEHOLD 23900 /*户主缴纳税金基础*/ #define MARRIED 29759 /*已婚缴纳税金基础*/ #define DIVORCE 14875 /*离异缴纳税金基础*/ #define BASE 0.15 /*基...

CrookedCr的博客 356

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

C Primer Plus(第六版)第十章 编程练习答案

前言:这次真的隔了好久才更,今日翻了几篇DASCTF文章,发现个个都是六边形战士,自己要得更努力,淦! 仅供参考,新手勿喷。 CH09Code answer 1: /* rain.c -- finds yearly totals, yearly average, and monthly average for several years of rainfall data */ #include <stdio.h> #define MONTHS 12 // number o.

weixin_50166464的博客 589

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

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

ProgramNovice的博客 1641

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

《C Primer Plus》第 8 章复习题和编程练习

《C Primer Plus》第 8 章复习题和编程练习

潇雪的博客 1415

《C Primer Plus》第 2 章复习题和编程练习

《C Primer Plus》第 2 章复习题和编程练习

潇雪的博客 1568

《C Primer Plus》第 9 章复习题和编程练习

《C Primer Plus》第 9 章复习题和编程练习

潇雪的博客 1477

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

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

ProgramNovice的博客 840

C Primer Plus 第二章:C语言概述 ~~~ 复习题与编程练习的答案与详解

第二章:C语言概述 ~~~ 复习题与编程练习的答案与详解 目录 一、复习题(答案参考该书附录,解析为读者编写) 二、编程练习 一、复习题(答案参考该书附录,解析为读者编写) 1、C语言的基本模块是什么? 函数 2、什么是语法错误?写出一个英语例子和C语言例子。 语法错误违反了组成语句或程序的规则。这是一个有语法错误的英文例子: Me speak Engl...

sumjess 1432

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

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

ProgramNovice的博客 1157

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

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

ProgramNovice的博客 704

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

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

ProgramNovice的博客 1555

C Primer Plus 第7章习题

P212~P216

weixin_45641816的博客 1068
上一篇: 《大数据:互联网大规模数据挖掘与分布式处理》(第2版)习题6.1.1-6.1.3解析
下一篇: Windows平台下向本地Hadoop集群提交作业时报错的解决方法
UestcXiye
博客等级 码龄8年 9044粉丝 1306原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

打赏作者

UestcXiye

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

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

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

打赏作者

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

抵扣说明:

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

余额充值
⌂ 换网址