明解C语言入门篇练习题第六章

练习6-1

#include <stdio.h>
int min2(int a, int b) 
{
	int min;
	min = (a < b) ? a : b;
	return min;
}
int main(void)
{
	int a, b;
	printf("a=");
	scanf("%d",&a);
	printf("b=");
	scanf("%d", &b);
	printf("a和b中较小值为%d\n",min2(a,b));
	return 0;
}

练习6-2

#include <stdio.h>
int min3(int a, int b, int c) 
{
	int min;
	min = a;
	if (b < a)
		min = b;
	if (c < a)
		min = c;
	return min;
}
int main(void)
{
	int a, b, c;
	printf("a=");
	scanf("%d",&a);
	printf("b=");
	scanf("%d", &b);
	printf("c=");
	scanf("%d", &c);
	printf("a,b,c中最小值为%d\n",min3(a,b,c));
	return 0;
}

练习6-3

#include <stdio.h>
int cube(int x) 
{
	return x*x*x;
}
int main(void)
{
	int x;
	printf("x=");
	scanf("%d",&x);
	printf("x的立方为%d\n", cube(x));
	return 0;
}

练习6-4

#include <stdio.h>
int sqr(int x)
{
	return x * x;
}
int pow4(int x)
{
	return sqr(x)*sqr(x);
}
int main(void)
{
	int x;
	printf("整数x:");   
	scanf("%d", &x);
	printf("x的四次幂是%d。\n", pow4(x));
	return 0;
}

练习6-5

#include <stdio.h>
int sumup(int n)
{
	int i;
	int sum = 0;
	for (i = 1; i <= n; i++)
	{
		sum += i;
	}
	return sum;
}
int main(void)
{
	int n;
	printf("整数n:");   
	scanf("%d", &n);
	printf("1到%d之间所有整数的和是%d。\n",n,sumup(n));
	return 0;
}

练习6-6

#include <stdio.h>
void alert(int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		printf("\a");
	}
	return;
}
int main(void)
{
	int n;
	printf("整数n:");   
	scanf("%d", &n);
	printf("连续发出%d次响铃。\n",n);
	alert(n);
	return 0;
}

练习6-7

#include <stdio.h>
void hello(void)
{
	puts("你好。");
	return;
}
int main(void)
{
	hello();
	return 0;
}

练习6-8

#include <stdio.h>
int min_of(const int v[], int n)
{
	int min;
	int i;
	min = v[0];
	for (i = 1; i < n; i++)
	{
		if (v[i] < min)
			min = v[i];
	}
	return min;
}
int main(void)
{
	int min;
	int v[4] = {19,63,34,76};
	min = min_of(v,4);
	printf("min = %d\n",min);
	return 0;
}

练习6-9

#include <stdio.h>
#define NUM 4
void rev_intary(int v[], int n)
{
	int tmp;
	int i;
	for (i = 0; i < n/2; i++)
	{
		tmp = v[i];
		v[i] = v[n - 1 - i];
		v[n - 1 - i] = tmp;
	}
	return;
}
int main(void)
{
	int i;
	int v[NUM] = {19,63,34,76};
	rev_intary(v,NUM);
	for (i = 0; i < NUM; i++)
	{
		printf("v[%d] = %d\n",i,v[i]);
	}
	return 0;
}

练习6-10

#include <stdio.h>
#define NUM 4
void intary_rcpy(int v1[],const int v2[],int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		v1[n - 1 - i] = v2[i];
	}
	return;
}
int main(void)
{
	int i;
	int v2[NUM] = {19,63,34,76};
	int v1[NUM];
	intary_rcpy(v1,v2,NUM);
	for (i = 0; i < NUM; i++)
	{
		printf("v1[%d] = %d\n",i,v1[i]);
	}
	return 0;
}

练习6-11

#include <stdio.h>
int search_idx(const int v[],int idx[],int key,int n)
{
	int i;
	int j = 0;
	int idx_num;
	for (i = 0; i < n; i++)
	{
		if (v[i] == key)
		{
			idx[j] = i;
			j++;
		}
	}
	return j;
}
int main(void)
{
	int i;
	int num;
	int v[] = {1,7,5,7,2,4,7};
	int key = 7;
	int idx[7] = {0};
	num = search_idx(v,idx,key,7);
	printf("和key相等的元素的个数为%d。\n",num);
	for (i = 0; i < 7; i++)
	{
		printf("idx[%d] = %d\n",i,idx[i]);
	}
	return 0;
}

练习6-12

#include <stdio.h>
void mat_mul(const int a[4][3],const int b[3][4],int c[3][3])
{
	int i, j;
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			c[i][j] = b[i][0]*a[0][j] + b[i][1] * a[1][j] + b[i][2] * a[2][j] + b[i][3] * a[3][j];
		}
	}
	return;
}
int main(void)
{
	int i, j;
	int a[4][3] = { {1,2,3} ,{4,5,6} ,{7,8,9} ,{10,11,12} };
	int b[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12} };
	int c[3][3] = { 0 };
	mat_mul(a,b,c);
	printf("a = \n");
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 3; j++)
		{
			printf("%4d",a[i][j]);
		}
		printf("\n");
	}
	printf("b = \n");
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
		{
			printf("%4d", b[i][j]);
		}
		printf("\n");
	}
	printf("c = b * a = \n");
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 3; j++)
		{
			printf("%4d",c[i][j]);
		}		
		printf("\n");
	}
	return 0;
}

练习6-13

#include <stdio.h>
void mat_add(const int a[4][3], const int b[4][3], int c[4][3])
{
	int i, j;
	for (i = 0; i < 4; i++)
	{ 
		for (j = 0; j < 3; j++)
		{
			c[i][j] = a[i][j] + b[i][j];
		}
	}
}
void mat_print(const int m[4][3])
{
	int i, j;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 3; j++)
		{
			printf("%4d", m[i][j]);
		}
		printf("\n");
	}
		putchar('\n');
}
int main(void)
{
	int tensu[2][4][3] = { { { 91, 63, 78 },{ 67, 72, 46 },{ 89, 34, 53 },{ 32, 54, 34 } } ,
						   { { 97, 67, 82 },{ 73, 43, 46 },{ 97, 56, 21 },{ 85, 46, 35 } } };
	int sum[4][3];			/* 总分 */
	mat_add(tensu[0], tensu[1], sum);				/* 求两次考试中成绩的总和 */
	puts("第一次考试的分数");  mat_print(tensu[0]);	/* 显示第一次考试的分数 */
	puts("第二次考试的分数");  mat_print(tensu[1]);	/* 显示第二次考试的分数 */
	puts("总分");        mat_print(sum);		/* 显示总分 */
	return 0;
}

练习6-14

#include <stdio.h>
int main(void)
{
	int i;
	static double a[5];
	for (i = 0; i < 5; i++)
	{
		printf("a[%d] = %.1f\n",i,a[i]);
	}
	return 0;
}

练习6-15

#include <stdio.h>
static int n;
void put_count(void)
{
	return n++;
}
int main(void)
{
	put_count();
	printf("put_count:第%d次\n",n);
	put_count();
	printf("put_count:第%d次\n", n);
	put_count();
	printf("put_count:第%d次\n", n);
	return 0;
}

  • 3
    点赞
  • 1
    评论
  • 11
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值