明解C语言 入门 课后习题练习答案 第六章

明解C语言 入门 课后习题练习答案 第六章,第1张

6-1

#define _CRT_SECURE_NO_WARNINGS
#include
int min2(int x, int y)
{
	return x < y ? x : y;
}
int main()
{
	int a = 0, b = 0;
	puts("请输入两个整数:");
	scanf("%d %d", &a, &b);
	printf("%d", min2(a, b));
	return 0;
}

6-2

#define _CRT_SECURE_NO_WARNINGS
#include
int min3(int x, int y, int z)
{
	int min = x;
	if (y < min)
		min = y;
	if (z < min)
		min = z;
	return min;
}
int main()
{
	int a = 0, b = 0,c=0;
	puts("请输入3个整数:");
	scanf("%d %d %d", &a, &b,&c);
	printf("%d", min3(a, b, c));
	return 0;
}

6-3

#define _CRT_SECURE_NO_WARNINGS
#include
int cube(int x)
{
	return x * x * x;
}
int main()
{
	int a = 0;
	puts("输入一个整数:");
	scanf("%d", &a);
	int b=cube(a);
	printf("其立方是:%d", b);
	return 0;
}

6-4

#define _CRT_SECURE_NO_WARNINGS
#include
int sqr(int x)
{
	return x * x;
}
int pow4(int x)
{
	return (sqr(x) * sqr(x));
}
int main()
{
	int a = 0;
	puts("输入一个整数:");
	scanf("%d", &a);
	int b = pow4(a);
	printf("其四次方是:%d", b);
	return 0;
}

6-5

#define _CRT_SECURE_NO_WARNINGS
#include
int sumup(int n)
{
    int sum =0;
	do
	{
		sum += n;
	} while (n-- > 0);
	return sum;
}
int main()
{
	int n = 0;
	printf("请输入一个整数n:");
	scanf("%d", &n);
	printf("从1到n之间所有的整数和为:%d", sumup(n));
	return 0;
}

6-6

#define _CRT_SECURE_NO_WARNINGS
#include
void alert(int n)
{
	while (n-- > 0)
	{
		printf("\a");
		printf("响铃%d次",n);
	} 

}
int main()
{
	int n = 0;
	printf("请输入响铃次数:");
	scanf("%d", &n);
	alert(n);
	return 0;
}

6-7

#define _CRT_SECURE_NO_WARNINGS
#include
void hello(void)
{
	printf("hello\n");
}
int main(void)
{
	hello();
	return 0;
}

6-8

#define _CRT_SECURE_NO_WARNINGS
#include
int min_of(const int v[], int n)
{
	int i = 0;
	int min = v[0];
	for (i = 0; i < n;i++)
	{
		if (v[i] < min)
			min = v[i];
	}
	return min;
}
int main()
{
	int ary[] = {9,2,3,1,0};
	printf("此数组中最小值为:%d", min_of(ary,5));
	return 0;
}

6-9

#define _CRT_SECURE_NO_WARNINGS
#include
#define NUMBER 6
void rev_intary(int v[], int n)
{
	int i = 0;
	for (i = 0; i < (n / 2); i++)
	{
		int t = v[i];
		v[i] = v[n -1 - i];
		v[n -1 - i] = t;
	}

}
int main()
{
	int i = 0;
	int v[NUMBER] = { 0 };
	for (i = 0; i < NUMBER; i++)
	{
		printf("v[%d]:", i);
		scanf("%d", &v[i]);
	}
	rev_intary(v, NUMBER);
	puts("倒序排列了:");
	for (i = 0; i < NUMBER; i++)
	{
		printf("v[%d]=%d\n", i,v[i]);
	}
	return 0;
}

6-10

#define _CRT_SECURE_NO_WARNINGS
#include
#define number 5
void intary_rcpy(int v1[], const int v2[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		 v1[i] = v2[n -1 -i];
	}

}
int main()
{
	int i = 0;
	int v1[number] = { 0 };
	int v2[number] = { 0 };	
	for (i = 0; i < number; i++)
	{
		printf("v2[%d]=", i);
		scanf("%d", &v2[i]);
	}
	printf("v2={");
	for (i = 0; i < number; i++)
	{
		if (i < number-1)
		{
			printf("%d,", v2[i]);
		}
		else
		{
			printf("%d}\n", v2[i]);
		}
	}
	intary_rcpy(v1, v2, number);
	puts("倒序后放在v1中");
	printf("v1={");
	for (i = 0; i < number; i++)
	{
		if(i

6-11

#define _CRT_SECURE_NO_WARNINGS
#include
#define number 7
void prt(int v[], int n)
{
	int i = 0;
	for (i = 0; i < n; i++)
	{
		printf("v[%d]=", i);
		scanf("%d", &v[i]);
	}
	printf("v={");
	for (i = 0; i < n; i++)
	{
		if (i < n - 1)
			printf("%d,", v[i]);
		else
			printf("%d}\n", v[i]);
	}
}
int search_idx(const int v[], int idx[], int key, int n)
{
	int i = 0, j = 0;
	for (i = 0; i 

6-12

#define _CRT_SECURE_NO_WARNINGS
#include
//这个题我不理解,四行三列矩阵乘三行四列矩阵得到的是一个四行四列矩阵,
//怎么放在三行三列矩阵里?我认为编写错误,所以改为放在四行四列矩阵中
void mat_mul(const int a[4][3], const int b[3][4], int c[4][4])
{
	int i = 0, j = 0, m = 0;
	for (i = 0; i < 4; i++)
	{
		while (m < 4)
		{
			for (j = 0; j < 3; j++)
			{
				c[i][m] += a[i][j] * b[j][m];
			}
			m++;
		}
		m = 0;
	}

}
int main()
{
	int i = 0, j = 0;

	int a[4][3] = { 0 };
	int b[3][4] = { 0 };
	int c[4][4] = { 0 };
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 3; j++)
		{
			scanf("%3d", &a[i][j]);
		}
	}
	puts("矩阵a:");
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 3; j++)
		{
			printf("%3d", a[i][j]);
		}
		putchar('\n');
	}
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
		{
			scanf("%3d", &b[i][j]);
		}
	}
	puts("矩阵b:");
	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
		{
			printf("%3d", b[i][j]);
		}
		putchar('\n');
	}
	mat_mul(a, b, c);
	puts("矩阵c:");
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			printf("%3d", c[i][j]);
		}
		putchar('\n');
	}
	return 0;
}

6-13

#define _CRT_SECURE_NO_WARNINGS
#include 
void mat_add(const int a[4][3], const int b[4][3], int c[2][4][3])
{
    int x, y, z;

    for (x = 0; x < 2; x++)
        for (y = 0; y < 4; y++)
            for (z = 0; z < 3; z++) {
                c[x][y][z] = a[y][z] + b[y][z];
            }
}
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]);
        putchar('\n');
    }
}
int main(void)
{
    int tensu1[4][3] = { {91, 63, 78}, {67, 72, 46}, {89, 34, 53}, {32, 54, 34} };
    int tensu2[4][3] = { {97, 67, 82}, {73, 43, 46}, {97, 56, 21}, {85, 46, 35} };
    int sum[2][4][3];            

    mat_add(tensu1, tensu2, sum);                

    puts("第一次考试的分数");  mat_print(tensu1);    
    puts("第二次考试的分数");  mat_print(tensu2);    
    puts("总分");        mat_print(sum);        
    return 0;
}

6-14

#define _CRT_SECURE_NO_WARNINGS
#include
int main()
{
	int i = 0;
	static double arr[5];
	for (i = 0; i < 5; i++)
	{
		printf("arr[%d]=%f\n", i, arr[i]);
	}
	return 0;
}

6-15

#define _CRT_SECURE_NO_WARNINGS
#include
void put_count()
{
	static int i = 1;
	printf("put_count:第%d次\n",i);
	i++;
}
int main()
{
	put_count();
	put_count();
	put_count();
	return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/674689.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)