5.1
#include
int main(void)
{
int i;
int v[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];
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 main(void)
{
int i;
int a[5] = {17, 23, 36};
int b[5];
for (i = 0; i < 5; i++)
b[i] = a[4 - i];
puts(" a b");
puts("---------");
for (i = 0; i < 5; i++)
printf("%4d%4d\n", a[i], b[i]);
return 0;
}
5.5
#include
#define NUMBER 7
int main(void)
{
int i;
int x[NUMBER];
for (i = 0; i < NUMBER; i++)
{
printf("x[%d]: ", i);
scanf("%d", &x[i]);
}
for (i = 0; i < NUMBER / 2; i++)
{
int temp = x[i];
x[i] = x[NUMBER - 1 - i];
x[NUMBER - 1 - i] = temp;
}
puts("倒序排列了。");
for (i = 0; i < 7; i++)
printf("x[%d] = %d\n", i, x[i]);
return 0;
}
5.6
#include
int main(void)
{
double a;
int b;
a = b = 1.5;
printf("%.1f %d", a, b);
return 0;
}
5.7
#include
#define NUMBER 100
int main(void)
{
int i, j;
int num, n;
int a[NUMBER];
printf("请输入数据个数: ");
do{
scanf("%d", &n);
if (n < 1 || n > NUMBER)
printf("请输入1~%d的数: ", NUMBER);
}while (n < 1 || n > NUMBER);
for (i = 0; i < n; i++)
{
printf("%d号: ", i + 1);
scanf("%d", &num);
a[i] = num;
}
printf("{");
for (j = 0; j < n; j++){
if (j == n - 1)
printf("%d", a[j]);
else
printf("%d, ", a[j]);
}
printf("}");
return 0;
}
5.8
#include
#define NUMBER 80
int main(void)
{
int i, j;
int num;
int tensu[NUMBER];
int bunpu[11] = {0};
printf("请输入学生人数: ");
do{
scanf("%d", &num);
if (num < 1 || num > NUMBER)
printf("请输入1-%d的数: ", NUMBER);
}while (num < 1 || num > NUMBER);
printf("请输入%d人的分数。\n", num);
for (i = 0; i < num; i++)
{
printf("%2d号: ", i + 1);
do{
scanf("%d", &tensu[i]);
if (tensu[i] < 1 || tensu[i] > 100)
printf("请输入1~100的数: ");
}while (tensu[i] < 1 || tensu[i] > 100);
bunpu[tensu[i] / 10]++;
}
puts("\n---分布图---");
for (i = 0; i <= 9; i++)
{
printf("%3d - %3d:", i * 10, i * 10 + 9);
for (j = 0; j < bunpu[i]; j++)
putchar('*');
putchar('\n');
}
printf(" 100:");
for (j = 0; j < bunpu[10]; j++)
putchar('*');
putchar('\n');
return 0;
}
5.9
#include
#define NUMBER 80
int main(void)
{
int i, j, max;
int num;
int tensu[NUMBER];
int bunpu[11] = {0};
printf("请输入学生人数: ");
do{
scanf("%d", &num);
if (num < 1 || num > NUMBER)
printf("请输入1-%d的数: ", NUMBER);
}while (num < 1 || num > NUMBER);
printf("请输入%d人的分数。\n", num);
for (i = 0; i < num; i++)
{
printf("%2d号: ", i + 1);
do{
scanf("%d", &tensu[i]);
if (tensu[i] < 1 || tensu[i] > 100)
printf("请输入1~100的数: ");
}while (tensu[i] < 1 || tensu[i] > 100);
bunpu[tensu[i] / 10]++;
}
puts("\n---分布图---");
max = bunpu[0];
for (i = 1; i < 11; i++)
{
if (max < bunpu[i])
max = bunpu[i];
}
for (j = max; j > 0; j--)
{
for (i = 0; i < 11; i++)
{
if (bunpu[i] >= j)
printf(" * ");
else
printf(" ");
}
putchar('\n');
}
putchar('\n');
printf("---------------------------------------------\n");
for (j = 0; j <= 100; j += 10)
{
printf(" %d ", j);
}
return 0;
}
5.10
#include
int main(void)
{
int i, j, k;
int a[4][3];
int b[3][4];
int c[4][4];
printf("请输入4行3列矩阵的元素的值:。\n");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
printf("a[%d][%d]: ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("请输入3行4列矩阵的元素的值:。\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 4; j++)
{
printf("b[%d][%d]: ", i, j);
scanf("%d", &b[i][j]);
}
}
for (i = 0; i < 4; i++) /* 计算矩阵的乘积 */
{
for (j = 0; j < 4; j++)
{
c[i][j] = 0;
for (k = 0; k < 3; k++)
{
c[i][j] += a[i][k] + b[k][j];
}
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
printf("c[%d][%d] = %d\n", i, j, c[i][j]);
}
}
return 0;
}
5.11
#include
int main(void)
{
int i, j;
int a[6][2];
int b[6]; /* 学生的总分 */
int c[2]; /* 课程的总分 */
double d[2]; /* 课程的平均分 */
double e[6]; /* 学生的平均分 */
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
printf("请输入第%d位同学第%d门的成绩: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
}
for (i = 0; i < 2; i++)
{
c[i] = 0;
}
for (i = 0; i < 6; i++)
{
b[i] = 0;
}
/* 计算各门课程的总分和平均分 */
for (i = 0; i < 2; j++)
{
for (j = 0; j < 6; j++)
{
c[i] += a[j][i];
}
d[i] = c[i] / 6;
printf("第%d门课程的总分为%d, 平均分为%.2f\n",i + 1, c[i], d[i]);
}
/* 计算每位学生的总分和平均分 */
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
b[i] += a[i][j];
}
e[i] = b[i] / 2;
printf("第%d位学生的总分为%d, 平均分为%.2f\n",i + 1, b[i], e[i]);
}
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];
for (i = 0; i < 4; i++)
{
for (j = 0; i < 3; j++)
{
sum[i][j] = 0;
for (k = 0; k < 2; k++)
{
sum[i][j] += tensu[k][i][j];
}
}
}
for (k = 0; k < 2; k++)
{
printf("第%d次考试的分数", k + 1);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
printf("%4d", tensu[k][i][j]);
}
putchar('\n');
}
}
puts("总分");
for (i = 0; i < 4; i++)
{
for (j = 0; j < 3; j++)
{
printf("%4d", sum[i][j]);
}
putchar('\n');
}
return 0;
}