C语言程序设计(王立柱)第三章答案 指针和数组

1

#include<stdio.h>
int IndexOfMax(const int* p, int n);
void Selection(int* p, int n);
void OutputArray(const int* p, int n);
void SwapByAddress(int* a, int* b);

int main() {
	int a[10];
	int i;
	printf("Enter 10 integers:\n");
	for (i = 0; i < 10; i++) {
		scanf_s("%d", a + i);
	}
	Selection(a, 10);
	OutputArray(a, 10);
	return 0;
}

int IndexOfMax(const int* p, int n) {
	int i;
	int max = 0;
	for (i = 1; i < n; i++) {
		if (p[max] < p[i])
			max = i;
	}
	return max;
}
void Selection(int* p, int n) {
	int max;
	while (n > 1) {
		max = IndexOfMax(p, n);
		SwapByAddress(p+max, p+n-1);
		n--;
	}
}
void OutputArray(const int* p, int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", p[i]);
	printf("\n");
}
void SwapByAddress(int* a, int* b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

2,3题

#include<stdio.h>
int IndexOfMin(const int* p, int n);
void Selection(int* p, int n);
void OutputArray(const int* p, int n);
void SwapByAddress(int* a, int* b);

int main() {
	int a[10];
	int i;
	printf("Enter 10 integers:\n");
	for (i = 0; i < 10; i++) {
		scanf_s("%d", a + i);
	}
	Selection(a, 10);
	OutputArray(a, 10);
	return 0;
}

int IndexOfMin(const int* p, int n) {
	int i;
	int min = 0;
	for (i = 1; i < n; i++) {
		if (p[min] > p[i])
			min = i;
	}
	return min;
}
void Selection(int* p, int n) {
	int min;
	int temp = n;
	while (n > 1) {
		min = IndexOfMin(p + temp - n, n) + temp - n;
		//返回的min是后面部分数组的index,而不是原本数组的index
		SwapByAddress(p + min, p + temp - n);
		n--;
	}
}
void OutputArray(const int* p, int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", p[i]);
	printf("\n");
}
void SwapByAddress(int* a, int* b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

4,5,6题

#include<stdio.h>
void BubbleDown(int p[], int n);
void BubbleUp(int p[], int n);
void UpDown(int p[], int n);
void OutputArray(const int* p, int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", p[i]);
	printf("\n");
}
void SwapByAddress(int* a, int* b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

int main() {
	const int num = 10;
	int a[10];
	printf("Enter %d integers:\n", num);
	for (int i = 0; i < num; i++) {
		scanf_s("%d", a + i);
	}

	//for (int i = 0; i < num; i++)
	//	BubbleUp(a + i, num - i);

	//for (int i = 0; i < num; i++)
	//	BubbleDown(a, num - i);

	int i = 0;
	int j = num;
	for (; i < j; i++, j--)
		UpDown(a + i, j - i);

	OutputArray(a, 10);
	return 0;
}
void BubbleDown(int p[], int n) {
	for (int i = 0; i < n - 1; i++) {
		if (p[i] > p[i + 1])
			SwapByAddress(p + i, p + i + 1);
	}
}
void BubbleUp(int p[], int n) {
	for (int i = n - 1; i >= 0; i--) {
		if (p[i] < p[i - 1])
			SwapByAddress(p + i, p + i - 1);
	}
}
void UpDown(int p[], int n) {
	BubbleUp(p, n);
	BubbleDown(p + 1, n - 1);
}

7

#include<stdio.h>
void Partition1(int p[], int n);
void Partition2(int p[], int n);
void OutputArray(const int* p, int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", p[i]);
	printf("\n");
}
void SwapByAddress(int* a, int* b) {
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

int main() {
	const int num = 10;
	int a[10];
	printf("Enter %d integers:\n", num);
	for (int i = 0; i < num; i++) {
		scanf_s("%d", a + i);
	}

	//支点为第一个元素
	Partition2(a, num);
	OutputArray(a, 10);
	return 0;
}
void Partition1(int p[], int n) {
	int* a;
	int* b;
	int* c;
	a = (int*)malloc(n * sizeof(int));
	b = (int*)malloc(n * sizeof(int));
	c = (int*)malloc(n * sizeof(int));
	int temp = p[0];
	for (int i = 1; i < n; i++) {
		if (temp >= p[i])
			a[i] = p[i], c[i] = 0;
		else
			b[i] = p[i], c[i] = 1;
	}
	int ai = 0;
	for (int i = 0; i < n; i++)
		if (c[i] == 0)
			p[ai++] = a[i];
	p[ai++] = temp;
	for (int i = 0; i < n; i++)
		if (c[i] == 1)
			p[ai++] = b[i];
	free(a);
	free(b);
	free(c);
}
void Partition2(int p[], int n) {
	int i = 0;
	int j = n - 1;
	while (i != j)
	{
		if (p[i] < p[i + 1])
		{
			SwapByAddress(p + i + 1, p + j);
			j--;
		}
		else
		{
			SwapByAddress(p + i + 1, p + i);
			i++;
		}
	}
}

8

#include<stdio.h>
#include<stdlib.h>

int main() {
	int n;
	printf("Enter an integer:\n");
	scanf_s("%d", &n);

	int* a;
	a = (int*)calloc(n + 1, sizeof(int));
	if (a == 0)
	{
		printf("allocation failure!\n");
		exit(1);
	}
	a[0] = a[1] = 1;
	for (int i = 2; i < n + 1; i++)
		if (a[i] == 0)
			for (int j = i + 1; j < n + 1; j++)
				if (j % i == 0)
					a[j] = 1;

	int hang = 0;
	for (int i = 0; i < n + 1; i++)
		if (a[i] == 0)
			if ((hang % 5 != 0) || (hang == 0))
			{
				printf("%d\t", i);
				hang++;
			}
			else
			{
				printf("\n");
				hang = 0;
			}
	free(a);
	return 0;
}

9

这里有个问题,不管把函数声明定义,函数指针声明定义怎么改,只要不是在定义函数指针时直接赋值,都会出现问题C2373,一直查不到问题所在

#include<stdio.h>
int indexofmax(const int* p, int n) {
	int i = 1;
	int max = 0;
	for (; i < n; i++)
		if (p[max] < p[i])
			max = i;
	return max;
}
int (*pofi)(const int*, int) = indexofmax;

void Selection(int* p, int n) {
	int max;
	int temp;
	while (n > 1) {
		max = pofi(p, n);
		temp = p[max];
		p[max] = p[n - 1];
		p[n - 1] = temp;
		n--;
	}
}
void (*pofs)(int* ,int) = Selection;

void OutputArray(const int* p, int n) {
	for (int i = 0; i < n; i++)
		printf("%d\t", p[i]);
	printf("\n");
}

int main() {
	int a[10];
	printf("Enter 10 integers:\n");
	for (int i = 0; i < 10; i++) {
		scanf_s("%d", a + i);
	}

	(*pofs)(a, 10);
	OutputArray(a, 10);
	return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值