C Primer Plus 第六版---编程练习10

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

//计算每年的总的降水量、年平均降水量、5年中每月的平均降水量
#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 years, months;
	float subtot, total=0;//subtot-每年的降水总量,total-5年降水总量
	printf("YEAR		RAINFALL(inches)\n");
	for (years = 0; years < YEARS; years++)
	{
		for (subtot=0,months = 0; months < MONTHS; months++)
			subtot += *(*(rain + years) + months);
		printf("%4d		%.1f\n", 2010 + years, subtot);
		total += subtot;
	}
	printf("The yearly average is %.1f inches.\n", total / YEARS);
	printf("MONTHLY AVERAGES:\n");
	printf("Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec\n");
	for (months = 0; months < MONTHS; months++)
	{
		subtot = 0;
		for (years = 0; years < YEARS; years++)
			subtot += *(*(rain + years) + months);
		printf("%.1f  ", subtot / YEARS);
	}
	
	printf("\nDone!\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<stdio.h>
void copy_arr(double target[], double source[], int n);
void copy_ptr(double* pa, double* sourcce, int n);
void copy_ptrs(double* pa, double* source, double* end);

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];
	copy_arr(target1, source, 5);
	copy_ptr(target2, source, 5);
	copy_ptrs(target3, source, source + 5);
	return 0;
}
void copy_arr(double target[], double source[], int n)
{
	int index = 0;
	printf("the first copy of the data is:");
	for (index = 0; index < n; index++)
	{
		target[index] = source[index];
		printf("%.1f", target[index]);
	}
	printf("\n");
}
void copy_ptr(double* pa, double* source, int n)
{
	int index;
	printf("the second copy of the data is:");
	for (index = 0; index < n; index++)
	{
		*pa = *(source + index); //pa++;
		printf("%.1f", *pa);
		pa++;
	}
	printf("\n");
}
void copy_ptrs(double* pa, double* source, double* end)
{
	printf("the third copy of the data is:");
	while (source < end)
	{
		*pa = *source;
		printf("%.1f", *pa);
		pa++;
		source++;
	}
	printf("\n");
}

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

#include<stdio.h>
int arr_max(int arr[], int n);

int main(void)
{
	int arr[5];
	int i = 0;
	int max;
	printf("input five integers:");
	for (i = 0; i < 5; i++)
	{
		scanf("%d", &arr[i]);
	}
	max = arr_max(arr, 5);
	printf("the maximum value in an array is: %d\n", max);
	return 0;
}
int arr_max(int arr[], int n)
{
	int result;
	int index;
	result = arr[0];
	for (index = 1; index < n; index++)
	{
		if (result < arr[index])
			result = arr[index];
	}
	return result;
}

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

#include<stdio.h>
#define SIZE 5
int subscript(double * arr, int n);

int main(void)
{
	double arr[SIZE];
	int i;
	int val;
	printf("enter fives characters:");
	for (i = 0; i < SIZE; i++)
		scanf("%lf,", arr + i);
	val = subscript(arr, SIZE);
	printf("the following table for the maximum of the array is:%d\n", val);
	return 0;
}
int subscript(double* arr, int n)
{
	int val = 0;
	int index;
	double max = *arr;
	for (index = 1; index < n; index++)
	{
		if (max < *(arr + index))
		{
			max = *(arr + index);
			val = index;
		}
	}
	return val;
}

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

#include<stdio.h>
#define SIZE 5
double arr_max(double * arr, int n);
double arr_min(double* arr, int n);

int main()
{
	double arr[SIZE];
	double max, min;
	int index;
	printf("enter %d data elements:", SIZE);
	for (index = 0; index < SIZE; index++)
		scanf("%lf", arr + index);
	min = arr_min(arr, SIZE);
	max = arr_max(arr, SIZE);
	printf("the difference between the maximum and the minimum is %3g\n", max - min);
	return 0;
}
double arr_max(double* arr, int n)
{
	double max;
	int index;
	max = *arr;
	for (index = 1; index < n; index++)
		if (max < *(arr + index))
			max = *(arr + index);
	return max;
}
double arr_min(double* arr, int n)
{
	double min;
	int index;
	min = *arr;
	for (index = 1; index < n; index++)
		if (min > *(arr + index))
			min = *(arr + index);
	return min;
}

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

#include<stdio.h>
#define SIZE 8
void arr_sort(double* pa, double* arr, int n);
int main()
{
	int index;
	double arr[SIZE];
	double target[SIZE];
	printf("enter %d number:", SIZE);
	for (index = 0; index < SIZE; index++)
		scanf("%lf,", arr + index);
	arr_sort(target, arr, SIZE);
	printf("inverse array elements are:\n");
	for (index = 0; index < SIZE; index++)
		printf("%3g\t", target[index]);
	putchar('\n');
	return 0;
}
void arr_sort(dounle* pa, double* arr, int n)
{
	int index;
	for (index = n - 1; index >= 0; index--)
	{
		*pa = *(arr + index);
		pa++;
	}
}

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

方法一

#include<stdio.h>
#define COL 4
void copy_arr(double target[][COL], double source[][COL], int n);
int main()
{
	int i, j;
	double arr[3][4] = {
		2.3, 8.5, 0.5, 3.6,
		1.2, 5.2, 9.5, 0.5,
		1.3, 3.5, 6.2, 9
	};
	double target[3][4];
	copy_arr(target, arr, 3);
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
			printf("%3g\t", *(*(target + i) + j));
		printf("\n");
	}
	return 0;
}
void copy_arr(double target[][COL], double source[][COL], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
		for (j = 0; j < COL; j++)
		{
			*(*(target + i) + j) = *(*(source + i) + j);
		}
}

方法二

#include<stdio.h>
#define COL 4
void copy_ptr(double(*target)[COL], double(*source)[COL], int n);
int main()
{
	int i, j;
	double arr[3][4] = {
		2.3, 8.5, 0.5, 3.6,
		1.2, 5.2, 9.5, 0.5,
		1.3, 3.5, 6.2, 9
	};
	double(*target)[4];
	target = arr;
	copy_ptr(target, arr, 3);
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
			printf("%3g\t", *(*(target + i) + j));
		printf("\n");
	}
	return 0;
}
void copy_ptr(double(*target)[COL], double(*source)[COL], int n)
{
	int i, j;
	for (i = 0; i < COL; i++)
		for (j = 0; j < COL; j++)
		{
			*(*(target + i) + j) = *(*(source + i) + j);
		}
}

方法三

#include<stdio.h>
#define COL 4
void copy_ptr(double(*target)[COL], double(*source)[COL], double *end);
int main()
{
	int i, j;
	double arr[3][4] = {
		2.3, 8.5, 0.5, 3.6,
		1.2, 5.2, 9.5, 0.5,
		1.3, 3.5, 6.2, 9
	};
	double target[3][COL];
	copy_ptr(target, arr, *(arr + 3) + 4);
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
			printf("%3g\t", *(*(target + i) + j));
		printf("\n");
	}
	return 0;
}
void copy_ptr(double(*target)[COL], double(*source)[COL], double *end)
{
	int i;
	while(*source < end)
	{
		for (i = 0; i < COL; i++)
			*(*target + i) = *(*source + i);
		*target++;
		*source++;
	}
}

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

#include<stdio.h>
void copy_ptr(double* target, double* source, int n);
int main()
{
	double arr[7];
	double* target;
	target = arr;
	int index;
	printf("enter 7 data elements:\n");
	for (index = 0; index < 7; index++)
		scanf("%lf,", (arr + index));
	copy_ptr(target, arr, 3);
	printf("the third to fifth data elements are:\n");
	for (index = 0; index < 3; inedx++)
		printf("%3g\t", *(target + index));
	printf("\n");
	return 0; 
}
void copy_ptr(double* target, double* source, int n)
{
	int index = 0;
	for (index = 0; index <= n; index++)
		*(target + index) = *(source + index + 2);
}
多维数组:
#include<stdio.h>
#define SIZE 7
#define Size 3
#define ROW 2
void copy_ptr(double (*target)[Size], double (*source)[SIZE], int n);
int main(void)
{
	double arr[ROW][SIZE] = {
		1.5,5.3,6.2,1.8,2.3,5.9,4.5,
		8.4,9.1,5.4,8.2,3.4,8.5,7.5
	};
	double (*target)[Size];
	int index,j;
	target = arr;
	copy_ptr(target, arr, Size);
	printf("The third to fifth data elements are:\n");
	for (index = 0; index < ROW; index++)
	{
		for (j = 0; j < Size; j++)
			printf("%3g\t", *(*(target + index) + j));
		printf("\n");
	}
	return 0;
}
void copy_ptr(double (*target)[Size], double (*source)[SIZE], int n)
{
	int index;
	int count = 0;
	while (count < ROW)
	{
		for (index = 0; index <= n; index++)
			*(*target + index) = *(*source + index + 2);
			*target++;
			*source++;
		count++;
	}
}

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

不支持变长数组,用的是传统C函数处理NX5的数组
#include<stdio.h>
#define ROWS 3
#define COLS 5
void copy_arr(double(*target)[COLS], double(*source)[COLS], int n);
int main(void)
{
	double arr[][COLS] = {
		1.7,4.3,6.2,7.6,5.6,
		3.8,2.8,3.8,0.2,0.8,
		1.9,0.6,1.3,2.6,5.2 };
	double carr[][COLS] = { {0} ,{0} ,{0} };
	copy_arr(carr, arr, ROWS);
	return 0;
}
void copy_arr(double(*target)[COLS], double(*source)[COLS], int n)
{
	int i, j;
	printf("The initial array element is:\n");
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			printf("%3g\t", *(*(source+i)+j));
		printf("\n");
	}
	printf("The replicated array elements are:\n");
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			printf("%3g\t", *(*(target + i) + j)= *(*(source + 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 ROWS 3
#define COLS 5
void show_arr(double(*arr)[COLS], int n);
void sum_arr(double target[][COLS], double arr1[][COLS], double arr2[][COLS], int n);
int main(void)
{
	double arr1[][COLS] = {
		1.7,4.3,6.2,7.6,5.6,
		3.8,2.8,3.8,0.2,0.8,
		1.9,0.6,1.3,2.6,5.2 };
	double arr2 [][COLS] = {
		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 };
	double sum[ROWS][COLS] = { 0 };
	printf("The first set of 3x5 array elements are:\n");
	show_arr(arr1,ROWS);
	printf("The second set of 3x5 array elements are:\n");
	show_arr(arr2, ROWS);
	sum_arr(sum, arr1, arr2, ROWS);
	printf("The array element of 3x5 after summation is:\n");
	show_arr(sum, ROWS);
	return 0;
}
void show_arr(double(*arr)[COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			printf("%3g\t", *(*(arr + i) + j));
		printf("\n");
	}
}
void sum_arr(double target[][COLS], double arr1[][COLS], double arr2[][COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			*(*(target + i) + j) = *(*(arr1 + i) + j) + *(*(arr2 + i) + j);
	}
}

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

#include <stdio.h>
#define ROWS 3
#define COLS 5
void show_arr(double arr[][COLS], int n);
void twofold_arr(double two_arr[][COLS], double arr[][COLS], int n);
int main(void)
{
	double arr[][COLS] = {
		1.7,4.3,6.2,7.6,5.6,
		3.8,2.8,3.8,0.2,0.8,
		1.9,0.6,1.3,2.6,5.2 };
	double two_arr[ROWS][COLS] = { 0 };
	printf("Initial values of array elements:\n");
	show_arr(arr, ROWS);
	twofold_arr(two_arr,arr, ROWS);
	printf("Values of array elements after doubling:\n");
	show_arr(two_arr, ROWS);
	return 0;
}
void show_arr(double arr[][COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			printf("%3g\t", arr[i][j]);
		printf("\n");
	}
}
void twofold_arr(double two_arr[][COLS], double arr[][COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			*(*(two_arr+i)+j) = *(*(arr+i)+j) + *(*(arr + i) + j);
	}
}

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

//计算每年的总的降水量、年平均降水量、5年中每月的平均降水量
#include <stdio.h>
#define MONTHS 12
#define YEARS  5
void total_rain(const float arr[][MONTHS], int m);
void average_rain(const float arr[][MONTHS], int m);
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 };
	total_rain(rain, YEARS);
	average_rain(rain, YEARS);
	return 0;
}
void total_rain(const float arr[][MONTHS], int m)
{
	int i, j;
	double subtot, total=0;
	printf("YEAR		RAINFALL(inches)\n");
	for (i = 0; i < m; i++)
	{
		for (subtot=0,j = 0; j < MONTHS; j++)
			subtot += arr[i][j];
			printf("%d		%3g\n",2010+i, subtot );
		total += subtot;
	}
	printf("The yearly average is %3g inches.\n", total / YEARS);
}
void average_rain(const float arr[][MONTHS], int m)
{
	int i, j;
	double total;
	printf("MONTHLY AVERAGES:\n");
	printf("Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec\n");
	for (i = 0; i < MONTHS; i++)
	{
		for (total=0,j = 0; j < m; j++)
			total += arr[j][i];
		printf("%.1f  ", total/m);
	}
	printf("\n");
}

13.编写一个程序,提示用户输入3组数,每组数包含5个double类型的数(假设用户都正确地响应,不会输入非数值数据)。该程序应完成下列任务。

a.把用户输入的数据储存在3×5的数组中

b.计算每组(5个)数据的平均值

c.计算所有数据的平均值

d.找出这15个数据中的最大值

e.打印结果

每个任务都要用单独的函数来完成(使用传统C处理数组的方式)。完成任务b,要编写一个计算并返回一维数组平均值的函数,利用循环调用该函数3次。对于处理其他任务的函数,应该把整个数组作为参数,完成任务c和d的函数应把结果返回主调函数。

觉得任务b的做法不太对
#include <stdio.h>
#define ROWS 3
#define COLS 5
void input_arr(double arr[][COLS],int n);
void average_arr(double arr[][COLS], int n);
double averall_arr(double arr[][COLS], int n);
double max_arr(double arr[][COLS], int n);
void show_arr(double arr[][COLS], int n);
int main(void)
{
	double aver;
	double max;
	double arr[ROWS][COLS] = { 0 };
	double ave_arr[ROWS] = { 0 };
	printf("输入15个数据:\n");
	input_arr(arr,ROWS);
	show_arr(arr, ROWS);
	average_arr(arr, ROWS);
	aver = averall_arr(arr, ROWS);
	printf("这15个数据的平均值是%3g\n", aver);
	max = max_arr(arr, ROWS);
	printf("这15个数的最大值是%3g\n", max);
	return 0;
}
void input_arr(double arr[][COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			scanf("%lf,", &arr[i][j]);
	}
	
}
void average_arr( double arr[][COLS], int n)
{
	int i, j;
	double  total;
	for (i = 0; i < n; i++)
	{
		for (total = 0, j = 0; j < COLS; j++)
			total += arr[i][j];
		printf("第%d行的平均值是%3g\n", i + 1, total / COLS);
	}
	
}
double averall_arr(double arr[][COLS], int n)
{
	int i, j;
	double total=0;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			total += arr[i][j];
	}
	return total / (n*COLS);
}
double max_arr(double arr[][COLS], int n)
{
	int i, j;
	double max;
	max = arr[0][0];
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS;j++)
			if (max < arr[i][j])
				max = arr[i][j];
	}
	return max;
}
void show_arr(double arr[][COLS], int n)
{
	int i, j;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j < COLS; j++)
			printf("%3g\t", arr[i][j]);
		printf("\n");
	}
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值