数组
依次输入5名学生的分数,显示出他们的总分和平均分。
代码清单5-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int wang, li, zhang, tian, song; int sum = 0 ; printf ("请输入5名同学的分数。\n" ); printf ("1号:" ); scanf ("%d" , &wang); sum += wang; printf ("2号:" ); scanf ("%d" , &li); sum += li; printf ("3号:" ); scanf ("%d" , &zhang); sum += zhang; printf ("4号:" ); scanf ("%d" , &tian); sum += tian; printf ("5号:" ); scanf ("%d" , &song); sum += song; printf ("它们的总分是%5d,平均值是%.2f。\n" , sum, (double )sum / 5 ); system("pause" ); return 0 ; }
在对多个相同的变量做同一种处理时,可使用数组来实现。
数组的遍历
创建一个元素类型为 int,包含5个元素的数组,依次把1、2、3、4、5赋给它们并进行显示。
代码清单5-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ]; v[0 ] = 1 ; v[1 ] = 2 ; v[2 ] = 3 ; v[3 ] = 4 ; v[4 ] = 5 ; printf ("v[0] = %d\n" , v[0 ]); printf ("v[1] = %d\n" , v[1 ]); printf ("v[2] = %d\n" , v[2 ]); printf ("v[3] = %d\n" , v[3 ]); printf ("v[4] = %d\n" , v[4 ]); system("pause" ); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 v[0 ] = 1 ; 00F B1875 B8 04 00 00 00 mov eax,4 00F B187A 6B C8 00 imul ecx,eax,0 00F B187D C7 44 0 D E8 01 00 00 00 mov dword ptr [ebp+ecx-18 h],1 v[1 ] = 2 ; 00F B1885 B8 04 00 00 00 mov eax,4 00F B188A C1 E0 00 shl eax,0 00F B188D C7 44 05 E8 02 00 00 00 mov dword ptr [ebp+eax-18 h],2 v[2 ] = 3 ; 00F B1895 B8 04 00 00 00 mov eax,4 00F B189A D1 E0 shl eax,1 00F B189C C7 44 05 E8 03 00 00 00 mov dword ptr [ebp+eax-18 h],3 v[3 ] = 4 ; 00F B18A4 B8 04 00 00 00 mov eax,4 00F B18A9 6B C8 03 imul ecx,eax,3 00F B18AC C7 44 0 D E8 04 00 00 00 mov dword ptr [ebp+ecx-18 h],4 v[4 ] = 5 ; 00F B18B4 B8 04 00 00 00 mov eax,4 00F B18B9 C1 E0 02 shl eax,2 00F B18BC C7 44 05 E8 05 00 00 00 mov dword ptr [ebp+eax-18 h],5
对数组的某个下标赋值时,反汇编会先计算指针,然后再将值传送到相应的地址处。
代码清单5-3
使用 for 循环对数组赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ]; v[0 ] = 1 ; v[1 ] = 2 ; v[2 ] = 3 ; v[3 ] = 4 ; v[4 ] = 5 ; for (int i = 0 ; i < 5 ; i++) { printf ("v[%d] = %d\n" , i,v[i]); } system("pause" ); return 0 ; }
代码清单5-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { double x[7 ]; for (int i = 0 ; i < 7 ; i++) { x[i] = 0 ; printf ("x[%d] = %.1f\n" , i,x[i]); } system("pause" ); return 0 ; }
数组初始化
在声明变量的时候,需要对变量进行初始化。数组的初始化操作如下:
int v[5] = {1,2,3,4,5};
数组的初始值就是大括号中的值(元素),各个元素的值用逗号隔开。
代码清单5-5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ] = { 1 ,2 ,3 ,4 ,5 }; for (int i = 0 ; i < 5 ; i++) { printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
反汇编
1 2 3 4 5 6 00391875 C7 45 E8 01 00 00 00 mov dword ptr [ebp-18 h],1 0039187 C C7 45 EC 02 00 00 00 mov dword ptr [ebp-14 h],2 00391883 C7 45 F0 03 00 00 00 mov dword ptr [ebp-10 h],3 0039188 A C7 45 F4 04 00 00 00 mov dword ptr [ebp-0 Ch],4 00391891 C7 45 F8 05 00 00 00 mov dword ptr [ebp-8 ],5
数组的初始化和局部变量初始化相同。数组是一段大小一样且连续的局部变量。
数组的复制
代码清单5-6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ] = { 17 ,23 ,36 }; int z[5 ]; for (int i = 0 ; i < 5 ; i++) { z[i] = v[i]; printf ("z[%d] = %d\n" , i, z[i]); } system("pause" ); return 0 ; }
代码清单5-6a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> v[5 ] = { 17 ,23 ,36 }; int main (void ) { int z[5 ]; for (int i = 0 ; i < 5 ; i++) { z[i] = v[i]; printf ("z[%d] = %d\n" , i, z[i]); } system("pause" ); return 0 ; }
反汇编
1 2 3 4 5 6 7 8 list0506.c 002E187 F C7 45 E4 11 00 00 00 mov dword ptr [ebp-1 Ch],11 h 002E1886 C7 45 E8 17 00 00 00 mov dword ptr [ebp-18 h],17 h 002E188 D C7 45 EC 24 00 00 00 mov dword ptr [ebp-14 h],24 h 002E1894 33 C0 xor eax,eax 002E1896 89 45 F0 mov dword ptr [ebp-10 h],eax 002E1899 89 45 F4 mov dword ptr [ebp-0 Ch],eax
1 2 3 4 5 6 list0506a.c 00 D1188D 8B 45 DC mov eax,dword ptr [ebp-24 h] 00 D11890 8B 4 D DC mov ecx,dword ptr [ebp-24 h] 00 D11893 8B 14 8 D 34 A0 D1 00 mov edx,dword ptr [ecx*4 +0088 A034h] 00 D1189A 89 54 85 E8 mov dword ptr [ebp+eax*4 -18 h],edx
把一个局部变量作为指针,那么这个局部(全局)变量是数组。同时可以看到局部变量是[ebp-xxx],而全局变量并没有用到 ebp或esp 。
C语言中局部变量区的数据都是不固定的,如果不初始化局部变量数据就有可能出问题,所以前面必须要有变量类型。而全局变量区默认数据都是0,所以全局变量可以不使用变量类型。
数组是以下标值来运算当前局部变量的指针。
输入数组元素值
从键盘输入数组元素的值并显示。
代码清单5-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ]; for (int i = 0 ; i < 5 ; i++) { printf ("请输入v[%d]的值:" , i); scanf ("%d" , &v[i]); } for (int i = 0 ; i < 5 ; i++) { printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
对数组的元素进行倒序排列
代码清单5-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ]; int num = sizeof (num) / 2 ; for (int i = 0 ; i < 5 ; i++) { printf ("请输入v[%d]的值:" , i); scanf ("%d" , &v[i]); } for (int k = 0 ; k <= num; k++) { int temp = v[4 - k]; v[4 - k] = v[k]; v[k] = temp; } for (int i = 0 ; i < 5 ; i++) { printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
使用数组进行成绩处理
代码清单5-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int i; int student[5 ]; int sum = 0 ; printf ("输入5名学生的分数。\n" ); for (i = 0 ; i < 5 ; i++) { printf ("%2d号:" , i); scanf ("%d" , &student[i]); sum += student[i]; } printf ("总分:%5d,平均分:%.2f\n" , sum, (double )sum / 5 ); system("pause" ); return 0 ; }
宏定义
我们写的代码有时定义数字太多,当分析代码或者需要修改其中的某一项时会很麻烦。为了解决这个问题C语言允许使用宏定义,将一个定义为常量。
#define指令,定义一个常量,语法如下:
#define 常量 值
代码清单5-10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 5 int main (void ) { int i; int student[5 ]; int sum = 0 ; printf ("输入%d名学生的分数。\n" , NUMBER); for (i = 0 ; i < NUMBER; i++) { printf ("%2d号:" , i); scanf ("%d" , &student[i]); sum += student[i]; } printf ("总分:%5d,平均分:%.2f\n" , sum, (double )sum / NUMBER); system("pause" ); return 0 ; }
数组元素的最大值和最小值
使用数组判断学生成绩最高分和最低分各自是多少。
代码清单5-11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 8 int main (void ) { int i; int tensu[NUMBER]; int max, min; printf ("请输入%d名学生的分数。\n" , NUMBER); for (i = 0 ; i < NUMBER; i++) { printf ("%2d号:" , i + 1 ); scanf ("%d" , &tensu[i]); } min = max = tensu[0 ]; for (i = 1 ; i < NUMBER; i++) { if (tensu[i] > max) max = tensu[i]; if (tensu[i] < min) min = tensu[i]; } printf ("最高分:%d\n" , max); printf ("最低分:%d\n" , min); system("pause" ); return 0 ; }
数组的元素的个数
我们现在定义的数组都很小,如果需要更大的数组空间,就需要修改对程序进行修改,重新编译执行。因此我们可以定义一个比较大的数组,然后从头开始仅使用其中需要的部分。
代码清单5-12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 80 int main (void ) { int num; int student[NUMBER]; int distribution[11 ] = { 0 }; printf ("请输入学生人数:" ); scanf ("%d" , &num); do { if (num<1 || num>NUMBER) { printf ("\a请输入1~%d之间的数\n" , NUMBER); } } while (num<1 || num>NUMBER); printf ("请输入%d人的分数。\n" , num); for (int i = 0 ; i < num; i++) { printf ("%2d号:" , i + 1 ); do { scanf ("%d" , &student[i]); if (student[i] < 0 || student[i]>100 ) { puts ("分数在1~100之间。请重新输入。" ); } } while (student[i] < 0 || student[i]>100 ); distribution[student[i] / 10 ]++; } puts ("\n-----分布图-----\n" ); for (int i = 0 ; i < 10 ; i++) { printf ("%d - %d: " , i * 10 , i * 10 + 9 ); for (int j = 0 ; j < distribution[i]; j++) { printf ("*" ); } printf ("\n" ); } printf (" 100: " ); for (int i = 0 ; i < distribution[10 ]; i++) { printf ("*" ); } printf ("\n" ); system("pause" ); return 0 ; }
多维数组
所谓多维数组,就是多个数组集合在一起形成的数组,即元素本身是数组的数组。
代码清单5-13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include <stdio.h> #include <stdlib.h> int main (void ) { int testsu1[4 ][3 ] = { {30 ,40 ,50 },{60 , 70 , 80 },{70 , 80 , 90 },{65 ,75 ,85 } }; int testsu2[4 ][3 ] = { {75 ,85 ,95 },{65 , 75 , 86 },{74 , 88 , 97 },{68 ,85 ,95 } }; int sum[4 ][3 ]; for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { sum[i][j] = testsu1[i][j] + testsu2[i][j]; } } puts ("第一次考试的分数" ); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { printf ("%4d" , testsu1[i][j]); } putchar ('\n' ); } puts ("第二次考试的分数" ); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { printf ("%4d" , testsu2[i][j]); } putchar ('\n' ); } puts ("总分" ); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { printf ("%4d" , sum[i][j]); } putchar ('\n' ); } return 0 ; }
总结
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main (void ) { int sum; int a[SIZE]; int b[SIZE] = { 1 ,2 ,3 }; int c[2 ][3 ] = { {11 ,22 ,33 },{44 ,55 ,66 } }; for (int i = 0 ; i < SIZE; i++) { a[i] = b[i]; } printf ("显示数组 a 的所有元素\n" ); for (int i = 0 ; i < SIZE; i++) { printf ("%5d," , a[i]); } printf ("\n" ); printf ("显示数组 b 的所有元素\n" ); for (int i = 0 ; i < SIZE; i++) { printf ("%5d," , b[i]); } printf ("\n" ); sum = 0 ; for (int i = 0 ; i < SIZE; i++) { sum += a[i]; printf ("计算并显示所有数组 a 的所有元素的和=%d\n" ,sum); } for (int i = 0 ; i < 2 ; i++) { for (int j = 0 ; j < 3 ; j++) { printf ("c[%d][%d] = %d\n" , i, j, c[i][j]); } } return 0 ; }
课后练习
练习5-1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ] = { 0 ,1 ,2 ,3 ,4 }; for (int i = 0 ; i < 5 ; i++) { v[i] = i + 1 ; printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
练习5-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int v[5 ] = { 5 ,4 ,3 ,2 ,1 }; for (int i = 0 ; i < 5 ; i++) { v[i] = i+1 ; printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
练习5-3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int array1[5 ] = { 1 ,2 ,3 ,4 ,5 }; for (int i = 0 ; i < 5 ; i++) { printf ("array[%d]=%d\n" , i, array1[i]); } system("pause" ); return 0 ; }
练习5-4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> int main (void ) { int a[5 ] = { 1 ,2 ,3 ,4 ,5 }; int b[5 ]; int j = 4 ; for (int i = 0 ; i < 5 ; i++) { b[j] = a[i]; j--; } for (int i = 0 ; i < 5 ; i++) { printf ("b[%d] = %d\n" , i, b[i]); } system("pause" ); return 0 ; }
练习5-5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define ARRAYNUM 5 int main (void ) { int v[ARRAYNUM]; int num = ARRAYNUM / 2 ; for (int i = 0 ; i < ARRAYNUM; i++) { printf ("请输入v[%d]的值:" , i); scanf ("%d" , &v[i]); } for (int k = 0 ; k <= num; k++) { int temp = v[4 - k]; v[4 - k] = v[k]; v[k] = temp; } for (int i = 0 ; i < ARRAYNUM; i++) { printf ("v[%d] = %d\n" , i, v[i]); } system("pause" ); return 0 ; }
练习5-6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define ARRAYNUM 5 int main (void ) { double a; int b; a = b = 1.5 ; printf ("a=%lf,b=%d\n" , a, b); system("pause" ); return 0 ; }
练习5-7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 80 int main (void ) { int num; int student[NUMBER]; int distribution[11 ] = { 0 }; printf ("数据个数:" ); scanf ("%d" , &num); do { if (num<1 || num>NUMBER) { printf ("\a请输入1~%d之间的数\n" , NUMBER); } } while (num<1 || num>NUMBER); for (int i = 0 ; i < num; i++) { printf ("%2d号:" , i + 1 ); do { scanf ("%d" , &student[i]); if (student[i] < 0 || student[i]>1000 ) { puts ("分数在1~1000之间。请重新输入。" ); } } while (student[i] < 0 || student[i]>1000 ); } for (int i = 0 ; i < num; i++) { if (i == 0 ) { printf ("{ %d," , student[i]); } else if (i > 0 && i < num-1 ) { printf ("%d," , student[i]); } else if (i == num-1 ) { printf ("%d }" , student[i]); } } printf ("\n" ); system("pause" ); return 0 ; }
练习5-8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 80 int main (void ) { int num; int student[NUMBER]; int distribution[11 ] = { 0 }; printf ("请输入学生人数:" ); scanf ("%d" , &num); do { if (num<1 || num>NUMBER) { printf ("\a请输入1~%d之间的数\n" , NUMBER); } } while (num<1 || num>NUMBER); printf ("请输入%d人的分数。\n" , num); for (int i = 0 ; i < num; i++) { printf ("%2d号:" , i + 1 ); do { scanf ("%d" , &student[i]); if (student[i] < 0 || student[i]>100 ) { puts ("分数在1~100之间。请重新输入。" ); } } while (student[i] < 0 || student[i]>100 ); distribution[student[i] / 10 ]++; } printf (" 100: " ); for (int i = 0 ; i < distribution[10 ]; i++) { printf ("*" ); } printf ("\n" ); puts ("\n-----分布图-----\n" ); for (int i = 9 ; i > 0 ; i--) { printf ("%d - %d: " , i * 10 , i * 10 + 9 ); for (int j = 0 ; j < distribution[i]; j++) { printf ("*" ); } printf ("\n" ); } system("pause" ); return 0 ; }
练习5-9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> #include <stdlib.h> #define NUMBER 80 int main (void ) { int num, space, min, max = 0 ; int student[NUMBER]; int distribution[11 ] = { 0 }; int nums[11 ] = { 0 }; char xh[2 ] = { '*' ,0x20 }; printf ("请输入学生人数:" ); scanf ("%d" , &num); do { if (num<1 || num>NUMBER) { printf ("\a请输入1~%d之间的数\n" , NUMBER); } } while (num<1 || num>NUMBER); printf ("请输入%d人的分数。\n" , num); for (int i = 0 ; i < num; i++) { printf ("%2d号:" , i + 1 ); do { scanf ("%d" , &student[i]); if (student[i] < 0 || student[i]>100 ) { puts ("分数在1~100之间。请重新输入。" ); } } while (student[i] < 0 || student[i]>100 ); } puts ("\n-----分布图-----\n" ); for (int i = 1 ; i <= 10 ; i++) { min = 0 ; for (int j = 0 ; j < num; j++) { if ((student[j]>(i-1 )*10 )&&(student[j] <= i * 10 )) { distribution[i-1 ]++; } } printf ("\n\n" ); if (distribution[i-1 ] > max) { max = distribution[i-1 ]; } } for (int i = 1 ; i <= max; i++) { printf (" " ); for (int j = 0 ; j < 10 ; j++) { if (distribution[j]>0 ) { printf ("%c" , xh[0 ]); printf (" " ); distribution[j]--; } else { printf (" " ); } } printf ("\n" ); } printf ("\n------------------------------------------------\n" ); for (int i = 0 ; i <= 10 ; i++) { printf ("%d" , i * 10 ); printf (" " ); } printf ("\n" ); system("pause" ); return 0 ; }
练习5-10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main (void ) { int num[2 ][2 ] = { 0 }; int len = sizeof (num) / sizeof (num[0 ][0 ]); for (int i = 0 ; i < len / 2 ; i++) { int produ = 0 ; printf ("请输入第 %d 个矩形的长和和宽:\n" , i + 1 ); for (int j = 0 ; j < len / 2 ; j++) { scanf ("%d" , &num[i][j]); if (j == 0 ) { produ += num[i][j]; } else { produ *= num[i][j]; } } printf ("该矩形的乘积是:%d\n" , produ); } return 0 ; }
练习5-11
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> int main (void ) { int num[6 ][2 ] = { 0 }; int len = sizeof (num) / sizeof (num[0 ][0 ]); for (int i = 0 ; i < len/2 ; i++) { int sum = 0 ; int equ = 0 ; printf ("请输入第 %d 个学生的语文和数学成绩:" ,i+1 ); for (int j = 0 ; j < len / 6 ; j++) { scanf ("%d" , &num[i][j]); sum += num[i][j]; } printf ("学生 %d 的总分是:%d,平均分是:%d\n\n" , i+1 ,sum,sum/2 ); } return 0 ; }
练习5-12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 #include <stdio.h> #include <stdlib.h> int main (void ) { int testsu[2 ][4 ][3 ] = { {{30 ,40 ,50 },{60 , 70 , 80 },{70 , 80 , 90 },{65 ,75 ,85 }}, {{75 ,85 ,95 },{65 , 75 , 86 },{74 , 88 , 97 },{68 ,85 ,95 }} }; int len = sizeof (testsu)/sizeof (testsu[0 ][0 ][0 ]); int sum[4 ][3 ] = { 0 }; for (int i = 0 ; i < 2 ; i++) { for (int j = 0 ; j < 4 ; j++) { for (int k = 0 ; k < 3 ; k++) { sum[j][k] += testsu[i][j][k]; } } } for (int i = 0 ; i < 2 ; i++) { printf ("第 %d 次考试的分数" ,i+1 ); for (int j = 0 ; j < 4 ; j++) { for (int k = 0 ; k < 3 ; k++) { printf ("%4d" , testsu[i][j][k]); } } putchar ('\n' ); } puts ("总分" ); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { printf ("%4d" , sum[i][j]); } putchar ('\n' ); } return 0 ; } void test (void ) { puts ("第二次考试的分数" ); for (int i = 0 ; i < 4 ; i++) { for (int j = 0 ; j < 3 ; j++) { } putchar ('\n' ); } }