C primer plus (第六版)第十章 编程练习第7,8,9,10,11题
因为第7题,第8题,第9题,第10和11题的程序类似,所以合并这几个题目的程序为一篇;
题目7.编写⼀个程序,初始化⼀个double类型的⼆维数组,使⽤编程练习2中的⼀个拷⻉函数把该数组中的数据拷⻉⾄另⼀个⼆维数组中(因为⼆维数组是数组的数组,所以可以使⽤处理⼀维数组的拷⻉函数来处理数组中的每个⼦数组)。
#include <stdio.h>
#define ROWS 5
#define COLS 4
void copy_arr(int row, double target[][COLS], double source[][COLS]); // 函数声明
int main()
{int rows, cols;double source[ROWS][COLS] = {{1.1, 1.2, 1.3, 1.4},{2.1, 2.2, 2.3, 2.4},{3.1, 3.2, 3.3, 3.4},{4.1, 4.2, 4.3, 4.4},{5.1, 5.2, 5.3, 5.4},};double target[ROWS][COLS];for ( rows = 0; rows < ROWS; rows++) // 复制每一行copy_arr(rows, target, source);printf("The target array now as following:\n"); // 打印目标数组for ( rows = 0; rows < ROWS; rows++){for (cols = 0; cols < COLS; cols++){printf("target[%d][%d] = %4.3lf ",rows, cols, target[rows][cols]);}putchar('\n');}return 0;
}void copy_arr(int row, double target[][COLS], double source[][COLS])
{int col;for ( col = 0; col < COLS; col++){target[row][col] = source[row][col];//printf("%lf ; %lf \n", target1[i], source[i]);}
}
题目8.使⽤编程练习2中的拷⻉函数,把⼀个内含7个元素的数组中第3〜第5个元素拷⻉⾄内含3个元素的数组中。该函数本⾝不需要修改,只需要选择合适的实际参数(实际参数不需要是数组名和数组⼤⼩,只需要是数组元素的地址和待处理元素的个数)。
#include <stdio.h>
#define SIZE 7void copy_ptr(double *target2, double *source, int n);int main()
{int num = 3;double source[SIZE] = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7};double target2[3];copy_ptr(target2, source, num);return 0;
}
void copy_ptr(double *target2, double *source, int n)
{int i;for ( i = 0; i < n; i++){*(target2 + i) = source[i + 2];printf("%lf ; %lf \n", target2[i], source[i+2]);}
}
题目9.编写⼀个程序,初始化⼀个double类型的3×5⼆维数组,使⽤⼀个处理变⻓数组的函数将其拷⻉⾄另⼀个⼆维数组中。还要编写⼀个以变⻓数组为形参的函数以显⽰两个数组的内容。这两个函数应该能处理任意N×M数组(如果编译器不⽀持变⻓数组,就使⽤传统C函数处理N×5的数组)。
思路:
需要一个输入变长二维数组row和col变量值的步骤,同时需要检查row与col的值至少不小于源数组的ROWS和COLS的值,根据C99标准,声明未赋值VLA的数组的值为0,运行程序可以看到这个效果。
#include <stdio.h>
#define ROWS 3
#define COLS 5
// 复制数组函数(将source内容复制到target,超出部分置自动填充0.0)
void copy_arr(int rows, int cols, double target[rows][cols], double source[][COLS]);
// 打印数组函数
void print_arr(int rows, int cols, double target[rows][cols]);int main()
{int rows, cols;// 源数组:3行5列double source[3][5] = {{1.1, 1.2, 1.3, 1.4, 1.5},{2.1, 2.2, 2.3, 2.4, 2.5},{3.1, 3.2, 3.3, 3.4, 3.5}};// 提示用户输入目标数组的行数和列数(需大于等于源数组)printf("Please input the rows and cols for target array:\n");while( scanf("%d %d",&rows, &cols) == 2){if ((rows >= ROWS) && (cols >= COLS)){ 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;}} // 声明目标数组(在rows和cols赋值后声明,符合VLA要求)double target[rows][cols];copy_arr(rows, cols, target, source); // 复制数组print_arr(rows, cols, target);return 0;
}
// 复制数组:source范围内的元素复制,超出部分置0
void copy_arr(int rows, int cols, double target[rows][cols], double source[][COLS])
{int i, j; for ( i = 0; i < ROWS; i++){for ( j = 0; j < COLS; j++){target[i][j] = source[i][j];}}
}
// 打印数组,使用正确的double格式符%lf
void print_arr(int rows, int cols, double target[rows][cols])
{int i, j; for ( i = 0; i < rows; i++){for ( j = 0; j < cols; j++){printf("target[%d][%d] = %4.3Lf; ",i, j, target[i][j]);}putchar('\n');}
}
题目10.编写⼀个函数,把两个数组中相对应的元素相加,然后把结果存储到第3个数组中。也就是说,如果数组1中包含的值是2、4、5、8,数组2中包含的值是1、0、4、6,那么该函数把3、4、9、14赋给第3个数组。函数接受3个数组名和⼀个数组⼤⼩。在⼀个简单的程序中测试该函数。
#include <stdio.h>
#define SIZE 5
int sum_arr(int source1[], int source2[], int target[], int size);
int main()
{int source1[SIZE] = {1, 2, 3, 4, 5};int source2[SIZE] = {11, 12, 13, 14, 15};int target[SIZE];int i;int sum;sum = sum_arr(source1, source2, target, SIZE);for ( i = 0; i < SIZE; i++){printf("source1[%d]%d + source2[%d]%d = target[%d] = %d\n", i,source1[i],i,source2[i],i,target[i]);}return 0;
}int sum_arr(int source1[], int source2[], int target[], int size)
{int i;for ( i = 0; i < size; i++){target[i] = source1[i] + source2[i];}
}
题目11.编写⼀个程序,声明⼀个int类型的3×5⼆维数组,并⽤合适的值初始化它。该程序打印数组中的值,然后各值翻倍(即是原值的2倍),并显⽰出各元素的新值。编写⼀个函数显⽰数组的内容,再编写⼀个函数把各元素的值翻倍。这两个函数都以函数名和⾏数作为参数。
思路:
程序针对题目修改了一下,程序首先需要手动输入rows和cols的值给VAL变长数组,源数组source就是变长数组,可以自定义数组大小并按照提示输入数据(独立的函数),然后数组打印和数组值翻倍都是基于独立的函数;
#include <stdio.h>
void initial_array(int rows, int cols, int source[rows][cols]);
void double_array(int rows, int cols, int source[rows][cols]);
void show_array(int rows, int cols, int source[rows][cols]);int main()
{int rows, cols;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;}}int source[rows][cols];initial_array(rows, cols, source);show_array(rows, cols, source); double_array(rows, cols, source);show_array(rows, cols, source);
}void initial_array(int rows, int cols, int source[rows][cols])
{int i, j;for ( i = 0; i < rows; i++){for ( j = 0; j < cols; j++){printf("Please enter array source[%d][%d] data.\n",i,j);scanf("%d", &source[i][j]);}}
}void double_array(int rows, int cols, int source[rows][cols])
{int i, j;for ( i = 0; i < rows; i++){for ( j = 0; j < cols; j++){source[i][j] = 2 * source[i][j];}}
}void show_array(int rows, int cols, int source[rows][cols])
{int i, j;for ( i = 0; i < rows; i++){for ( j = 0; j < cols; j++){printf("source[%d][%d] = %5d ;",i, j, source[i][j]);}putchar('\n');}
}
然后把程序喂给豆包AI如下网站它居然还能帮忙优化╰(*°▽°*)╯,(豆包AI网址:豆包),优化后的程序如下;
#include <stdio.h>
#include <stdlib.h> // 用于处理输入错误// 函数声明
void initial_array(int rows, int cols, int source[rows][cols]);
void double_array(int rows, int cols, int source[rows][cols]);
void show_array(int rows, int cols, int source[rows][cols]);int main()
{int rows, cols;int input_result;printf("=== 数组处理程序 ===\n");printf("请输入数组的行数和列数(均需大于1,用空格分隔):\n");// 循环直到获得有效的输入while(1){input_result = scanf("%d %d", &rows, &cols);// 检查输入是否为有效数字if (input_result != 2){printf("输入错误,请输入两个整数!\n");// 清除输入缓冲区while (getchar() != '\n');continue;}// 检查行列数是否符合要求if ((rows > 1) && (cols > 1)){ printf("已接受数组大小:%d行 %d列\n", rows, cols);break;}else{printf("行数和列数必须都大于1,请重新输入:\n");}}// 声明变长数组int source[rows][cols];// 初始化数组printf("\n----- 开始初始化数组 -----\n");initial_array(rows, cols, source);// 显示原始数组printf("\n----- 原始数组内容 -----\n");show_array(rows, cols, source);// 将数组元素翻倍double_array(rows, cols, source);// 显示翻倍后的数组printf("\n----- 元素翻倍后的数组 -----\n");show_array(rows, cols, source);return 0;
}// 初始化数组,让用户输入每个元素的值
void initial_array(int rows, int cols, int source[rows][cols])
{int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){printf("请输入数组元素 source[%d][%d] 的值:", i, j);// 确保输入的是整数while (scanf("%d", &source[i][j]) != 1){printf("输入错误,请输入一个整数:");// 清除输入缓冲区while (getchar() != '\n');}}}
}// 将数组中的每个元素翻倍
void double_array(int rows, int cols, int source[rows][cols])
{int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){source[i][j] *= 2; // 等价于 source[i][j] = 2 * source[i][j]}}
}// 显示数组内容
void show_array(int rows, int cols, int source[rows][cols])
{int i, j;for (i = 0; i < rows; i++){for (j = 0; j < cols; j++){printf("source[%d][%d] = %5d ;", i, j, source[i][j]);}putchar('\n');}
}