C primer plus 第六版 第十章 编程练习 答案

10.2

#include <stdio.h>

void copy_arr(double tr[], double s[], int n);
void copy_ptr(double * tr, double * s, int n);
void copy_ptrs(double * tr, double * s, int *p);

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

	printf("target1: ");
	for (int i = 0; i < 5; i++)
	{ 
		printf("%.2f ", target1[i]);
	}

	printf("\ntarget2: ");
	for (int i = 0; i < 5; i++)
	{
		printf("%.2f ", target2[i]);
	}

	printf("\ntarget3: ");
	for (int i = 0; i < 5; i++)
	{
		printf("%.2f ", target3[i]);
	}
}

void copy_arr(double tr[], double s[], int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		tr[i] = s[i];
	}

	return 0;
}

void copy_ptr(double * tr, double * s, int n)
{
	int i;
	for (i = 0; i < n; i++)
		*(tr + i) = *(s + i);

	return 0;
}

void copy_ptrs(double * tr, double * s, int *p)
{
	while(s < p)
	{ 
		*tr = *s;
		tr++;
		s++;
	}

	return 0;
}

10.3

//编写一个函数,返回储存在int类型数组中的最大值,并测试
#include <stdio.h>
#define SIZE 5

int max(int s[], int n); 

int main(void)
{
	int nums[SIZE];
	int i;

	printf("Please enter %d integers: \n",SIZE);
	for (i = 0; i < SIZE; i++)
		scanf("%d", &nums[i]);
	printf("The max number is %d.", max(nums,SIZE));

	return 0;
}

int max(int s[],int n)
{
	int max_num = 0;
	for (int i = 1; i < SIZE; i++)
	{
		if (max_num < s[i])
			max_num = s[i];
	}

	return max_num;
}

10.4

//编写一个函数,返回储存在double类型数组中的最大值的下标,并测试

#include<stdio.h>
#define SIZE 5

int max(double nums[], int n);

int main()
{
	int i;
	double nums[SIZE];
	printf("please enter %d double numbers: \n", SIZE);

	for (i = 0; i < SIZE; i++)
		scanf("%lf", &nums[i]);

	printf("The index of the largest double number in arr a is %d\n", max(nums, SIZE));
	return 0;
}

int max(double nums[], int n)
{
	int i, j = 0;
	double max = 0;
	for (i = 0; i < n; i++)
		if (max < nums[i])
		{
			max = nums[i];
			j = i;
		}
	return j;
}

10.5

//编写函数,返回储存在double类型数组中最大值与最小值的差值

#include <stdio.h>
#define SIZE 5

double d_value(double nums[], int n);

int main(void)
{
	int i;
	double nums[SIZE];
	
	printf("Please enter %d integers: \n", SIZE);
	for (i = 0; i < SIZE; i++)
		scanf("%lf", &nums[i]);
	printf("The different value between the max and min: %.2lf\n", d_value(nums, SIZE));

	return 0;
}

double d_value(double nums[], int n)
{
	double max = 0; 
	double min = nums[0];
	int i;
	for (i = 0; i < n; i++)
		if (max < nums[i])
			max = nums[i];
	for (i = 0; i < n; i++)
		if (min > nums[i])
			min = nums[i];

	return max - min;

}

10.6

//编写一个函数,把double类型数组中的数据倒序排列,并测试

#include <stdio.h>
#define SIZE 5

void reverse(double n[], int m);

int main(void)
{
	double nums[SIZE];
	int i;

	printf("Please enter %d integers: \n", SIZE);
	for (i = 0; i < SIZE; i++)
		scanf("%lf", &nums[i]);
	printf("The reverse numbers is : \n");
	reverse(nums, SIZE);
	return 0;
}

void reverse(double n[], int m)
{
	int i, j;
	double temp;
	for (i = 0, j = m-1; i < j; i++, j--)
	{
		temp = n[i];
		n[i] = n[j];
		n[j] = temp;
	}

	for (i = 0; i < m; i++)
		printf("%.2lf ", n[i]);

}

10.7

先发了,后面重复代码工作量太大了,思路跟同上面的题目
有空再更。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值