明解c语⾔⼊门版答案第五章,明解C语⾔⼊门篇练习题第五章练习5-1#include
int main(void)
{
int i;
int v[5];/* int[5]数组 */
for (i = 0; i < 5; i++)/* 为数组元素赋值 */
v[i] = i;
for (i = 0; i < 5; i++)/* 显⽰元素的值 */
printf("v[%d] = %d\n", i, v[i]);
return 0;
}
练习5-2#include
int main(void)
{
int i;
int v[5];/* int[5]数组 */
for (i = 0; i < 5; i++)/* 为数组元素赋值 */
v[i] = 5 - i;
for (i = 0; i < 5; i++)/* 显⽰元素的值 */
printf("v[%d] = %d\n", i, v[i]);
return 0;
}
练习5-3#include
int main(void)
{
int i;
int v[5] = { 5, 4, 3, 2, 1 };/* 初始化 */
for (i = 0; i < 5; i++)/* 显⽰元素的值 */
printf("v[%d] = %d\n", i, v[i]);
return 0;
}
练习5-4#include
int i;
int a[5] = { 17, 23, 36 };/* 使⽤{17,23,36,0,0} 进⾏初始化 */ int b[5];
for (i = 0; i < 5; i++)
b[i] = a[5 - 1 - i];
明解c语言puts(" a b");
puts("---------");
for (i = 0; i < 5; i++)
printf("%4d%4d\n", a[i], b[i]);
return 0;
}
练习5-5#include
#define NUM 7
int main(void)
{
int i;
int x[NUM];/* int[7] 数组 */
for (i = 0; i < NUM; i++) {/* 输⼊元素的值 */
printf("x[%d] : ", i);
scanf("%d", &x[i]);
}
for (i = 0; i < NUM/2; i++) {/* 对数组元素进⾏倒序排列 */
int temp = x[i];
x[i] = x[NUM - 1 - i];
x[NUM - 1 - i] = temp;
}
puts("倒序排列了。");
for (i = 0; i < NUM; i++)/* 显⽰元素的值 */
printf("x[%d] = %d\n", i, x[i]);
return 0;
}
练习5-6#include
double a;
int b;
a =
b = 1.5;
printf("a = %f\n",a); printf("b = %d\n",b); return 0;
}
练习5-7#include
#define NUM 128 int main(void)
{
int n;
int i;
int v[NUM];
printf("数据个数:"); scanf("%d",&n);
for (i = 0; i < n; i++) {
printf("%d号:",i+1); scanf("%d",&v[i]);
}
printf("{");
for (i = 0; i < n; i++) {
if (i != n - 1)
printf("%d,", v[i]); else
printf("%d",v[i]);
}
printf("}\n");
return 0;
}
练习5-9 待做
练习5-10 待做
练习5-11#include
#define STU_NUM 6
int main(void)
{
int i;
int j;
int score[STU_NUM][2];
int yw_sum = 0;
int yw_avg = 0;
int sx_sum = 0;
int sx_avg = 0;
for (i = 0; i < STU_NUM; i++)
{
j = 0;
printf("学⽣%d\n",i+1);
printf("语⽂分数:");
scanf("%d",&score[i][j]);
yw_sum += score[i][j];
printf("数学分数:");
scanf("%d", &score[i][j+1]);
sx_sum += score[i][j+1];
}
yw_avg = yw_sum / STU_NUM;
sx_avg = sx_sum / STU_NUM;
printf("语⽂总分:%d\n",yw_sum);
printf("数学总分:%d\n",sx_sum);
printf("语⽂平均分:%d\n", yw_avg);
printf("语⽂平均分:%d\n", sx_avg);
for (i = 0; i < STU_NUM; i++)
{
printf("学⽣%d总分为%d,平均分为%d。\n",i+1,score[i][0]+score[i][1], (score[i][0] + score[i][1])/2);
return 0;
}
练习5-12#include
int main(void)
{
int i,j,k;
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] = { 0 };/* 总分 */
for (i = 0; i < 2; i++) /* 求两次考试的分数之和 */
{
for (j = 0; j < 4; j++) /* 4名学⽣的 */
{
for (k = 0; k < 3; k++)/* 3门课程的 */
sum[j][k] += tensu[i][j][k];/* 两次的分数相加 */
}
}
puts("两次考试的分数");
for (i = 0; i < 2; i++)
{
if (i == 0)
printf("第⼀次考试的分数\n");
else
printf("第⼆次考试的分数\n");
for (j = 0; j < 4; j++)
{
for (k = 0; k < 3; k++)
{
printf("%4d", tensu[i][j][k]);
}
putchar('\n');
}
putchar('\n');
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论