参考书籍:《C#程序设计实验指导与习题测试》
掌握数组的声明、实例化、和初始化
掌握数组元素的使用
掌握一维数组的操作
掌握二维数组的操作
了解System.Array类常用方法和属性的使用
了解指针的基本操作
实验1:求若干学生的平均身高、最高身高、最低身高以及高于平均身高的人数
已知10个学生的身高为156、150、167、178、180、176、173、154、155、158,求平均身高、最高身高
最低身高,并统计高于平均身高的人数。
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 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int [] height = new int [10 ] { 156 , 150 , 167 , 178 , 180 , 176 , 173 , 154 , 155 , 158 }; int avg = 0 ,sum=0 , max = 0 , min = 0 , count = 0 ; int i = 0 ; int j = 0 ; int temp = 0 ; Console.WriteLine("学生身高信息如下:" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,4}" , height[i]); } Console.WriteLine(); for (i=0 ;i<10 ;i++) { sum += height[i]; } avg = sum / 10 ; for (i=0 ;i<9 ;i++) { for (j=0 ;j<9 -i;j++) { if (height[j]>height[j+1 ]) { temp = height[j]; height[j] = height[j + 1 ]; height[j + 1 ] = temp; } } } max = height[9 ]; min = height[0 ]; for (i=0 ;i<10 ;i++) { if (height[i]>avg) { count++; } } Console.WriteLine("这组学生的平均身高为{0}" , avg); Console.WriteLine("这组学生中最高身高为{0}" , max); Console.WriteLine("这组学生中最低身高为{0}" , min); Console.WriteLine("这组学生中高于平均身高的学生个数为{0}" , count); Console.ReadKey(); } } }
1 2 3 4 5 6 学生身高信息如下: 156 150 167 178 180 176 173 154 155 158 这组学生的平均身高为164 这组学生中最高身高为180 这组学生中最低身高为150 这组学生中高于平均身高的学生个数为5
实验2:统计各分数段学生的人数和百分比
已知某班10个学生的英语考试成绩为80、90、67、89、78、85、45、69、77、95,统计优良中差各分数段的人数和所占百分比。
(0-100为优秀、80-89为良好、60-79为合格、0-59为不及格)
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 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int [] score = new int [10 ] { 80 , 90 , 67 , 89 , 78 , 85 , 45 , 69 , 77 , 95 }; int i = 0 ; int very_good = 0 ; int good = 0 ; int normal = 0 ; int bad = 0 ; Console.WriteLine("学生成绩如下:" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,4}" , score[i]); } Console.WriteLine(); for (i=0 ;i<10 ;i++) { if (score[i]>=90 &&score[i]<=100 ) { very_good++; } if (score[i] >= 80 && score[i] <= 89 ) { good++; } if (score[i] >= 60 && score[i] <= 79 ) { normal++; } if (score[i] >= 0 && score[i] <= 59 ) { bad++; } } Console.WriteLine("成绩优秀的人数为{0},所占百分比为{1:#0.##%}" , very_good,very_good/10.0 ); Console.WriteLine("成绩良好的人数为{0},所占百分比为{1:#0.##%}" , good,good/10.0 ); Console.WriteLine("成绩一般的人数为{0},所占百分比为{1:#0.##%}" , normal,normal/10.0 ); Console.WriteLine("成绩不合格的人数为{0},所占百分比为{1:#0.##%}" , bad,bad/10.0 ); Console.ReadKey(); } } }
1 2 3 4 5 6 学生成绩如下: 80 90 67 89 78 85 45 69 77 95 成绩优秀的人数为2 ,所占百分比为20 % 成绩良好的人数为3 ,所占百分比为30 % 成绩一般的人数为4 ,所占百分比为40 % 成绩不合格的人数为1 ,所占百分比为10 %
实验3:冒泡排序
随机生成10个学生的成绩(0-100的整数),递减排序
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 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int [] Array = new int [10 ]; int i; int j; int temp; Random rNum = new Random(); for (i=0 ;i<10 ;i++) { Array[i] = rNum.Next(101 ); } Console.WriteLine(" 原始数组数据为" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,3}" ,Array[i]); } Console.WriteLine(); for (i=0 ;i<9 ;i++) { for (j=0 ;j<9 -i;j++) { if (Array[j] < Array[j + 1 ]) { temp = Array[j]; Array[j] = Array[j++]; Array[j++] = temp; } } } Console.WriteLine(" 降序排列后的数组为" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,3}" , Array[i]); } Console.ReadKey(); } } }
1 2 3 4 原始数组数据为 94 13 37 16 89 83 37 42 44 31 降序排列后的数组为 94 13 13 13 13 13 37 37 37 31
实验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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int [] Array = new int [10 ]; int i; int j; int temp; Random rNum = new Random(); for (i=0 ;i<10 ;i++) { Array[i] = rNum.Next(101 ); } Console.WriteLine(" 原始数组数据为" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,3}" ,Array[i]); } Console.WriteLine(); for (i=0 ;i<9 ;i++) { int Max = i; for (j=0 ;j<9 ;j++) { if (Array[j] < Array[Max]) { temp = Array[j]; Array[j] = Array[Max]; Array[Max] = temp; } } } Console.WriteLine(" 降序排列后的数组为" ); for (i=0 ;i<10 ;i++) { Console.Write("{0,3}" , Array[i]); } Console.ReadKey(); } } }
1 2 3 4 原始数组数据为 78 27 61 93 73 23 7 39 3 81 降序排列后的数组为 93 78 73 61 39 27 23 7 3 81
实验5:矩阵加减
利用随机数生成两个4行4列的数组,数组A的范围在50-100之间,数组B的取值范围在0-100之间
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 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int [,] A = new int [4 , 4 ]; int [,] B = new int [4 , 4 ]; int [,] C = new int [4 , 4 ]; int [,] D = new int [4 , 4 ]; Random rNum = new Random(); int i, j; for (i=0 ;i<4 ;i++) { for (j=0 ;j<4 ;j++) { A[i, j] = rNum.Next(50 , 101 ); } } for (i = 0 ; i < 4 ; i++) { for (j = 0 ; j < 4 ; j++) { B[i, j] = rNum.Next(0 , 101 ); } } Console.WriteLine("原始数组A" ); for (i=0 ;i<4 ;i++) { for (j=0 ;j<4 ;j++) { Console.Write("{0,4}" , A[i,j]); } Console.WriteLine(); } Console.WriteLine("原始数组B" ); for (i = 0 ; i < 4 ; i++) { for (j = 0 ; j < 4 ; j++) { Console.Write("{0,4}" , B[i, j]); } Console.WriteLine(); } Console.WriteLine("\n上三角" ); for (i=0 ;i<4 ;i++) { for (j=0 ;j<4 ;j++) { if (i>j) { Console.Write(" " ); } if (i<=j) { Console.Write("{0,4}" ,A[i,j]); } } Console.WriteLine(); } Console.WriteLine("\n下三角" ); for (i = 0 ; i < 4 ; i++) { for (j = 0 ; j < 4 ; j++) { if (i >= j) { Console.Write("{0,4}" , B[i, j]); } } Console.WriteLine(); } Console.WriteLine("\n数组相加" ); for (i = 0 ; i < 4 ; i++) { for (j = 0 ; j < 4 ; j++) { Console.Write("{0,4}" , A[i, j] + B[i, j]); } Console.WriteLine(); } Console.WriteLine("\n数组相减" ); for (i = 0 ; i < 4 ; i++) { for (j = 0 ; j < 4 ; j++) { Console.Write("{0,4}" , A[i, j] - B[i, j]); } Console.WriteLine(); } Console.WriteLine(); Console.ReadKey(); } } }
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 原始数组A 72 55 65 53 86 57 84 74 65 86 80 84 95 62 50 90 原始数组B 79 41 46 88 7 28 88 17 7 61 40 82 99 4 47 86 上三角 72 55 65 53 57 84 74 80 84 90 下三角 79 7 28 7 61 40 99 4 47 86 数组相加 151 96 111 141 93 85 172 91 72 147 120 166 194 66 97 176 数组相减 -7 14 19 -35 79 29 -4 57 58 25 40 2 -4 58 3 4
实验6:打印杨辉三角
打印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 36 37 38 39 40 41 42 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int rows = 10 ; int space = 0 ; int i, j = 0 ; int [,] A = new int [10 , 10 ]; for (i = 0 ; i < rows; i++) { for (space = 1 ; space <= rows - i; space++) { Console.Write(" " ); } for (j = 0 ; j <= i; j++) { if (j == 0 || i == 0 ) { A[i,j] = 1 ; } else { A[i, j] = A[i - 1 , j] + A[i - 1 , j - 1 ]; } Console.Write("{0,4}" , A[i,j]); } Console.WriteLine(); } Console.ReadKey(); } } }
1 2 3 4 5 6 7 8 9 10 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
实验7:使用System.Array类的方法操作数组
使用System.Array类的方法获取一直数组(数组元素为80、90、67、89、78、85、45、69、77、95)的维度、长度,并对数组排序,反转。
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 using System;namespace ConsoleApp4 { class Program { static void Main (string [] args ) { int i; int [] A = new int [] { 80 ,90 ,67 ,89 ,78 ,85 ,45 ,69 ,77 ,95 }; Console.WriteLine("数组A的维度:{0}" ,A.Rank); Console.WriteLine("数组A的长度:{0}" ,A.Length); Console.WriteLine("数组A的内容:" ); for (i=0 ;i<A.Length;i++) { Console.Write("{0,4}" ,A[i]); } Console.WriteLine(); Array.Sort(A); Console.Write("数组排序后的内容:\n" ); for (i = 0 ; i < A.Length; i++) { Console.Write("{0,4}" , A[i]); } Console.WriteLine(); Array.Reverse(A); Console.Write("数组反转后的内容:\n" ); for (i = 0 ; i < A.Length; i++) { Console.Write("{0,4}" , A[i]); } Console.ReadKey(); } } }
1 2 3 4 5 6 7 8 数组A的维度:1 数组A的长度:10 数组A的内容: 80 90 67 89 78 85 45 69 77 95 数组排序后的内容: 45 67 69 77 78 80 85 89 90 95 数组反转后的内容: 95 90 89 85 80 78 77 69 67 45
实验8:指针操作
从控制台输入任意三个整数a,b,c使用指针对其进行升序排列
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 using System;namespace ConsoleApp4 { unsafe class Program { static void Main (string [] args ) { int a, b, c; int * pa, pb, pc; pa = &a; pb = &b; pc = &c; int temp; Console.Write("请输入整数a:" ); String s = Console.ReadLine(); a = int .Parse(s); Console.Write("请输入整数b:" ); s = Console.ReadLine(); b = int .Parse(s); Console.Write("请输入整数c:" ); s = Console.ReadLine(); c = int .Parse(s); Console.WriteLine("原始数据a={0},b={1},c={2}" ,a,b,c); if (*pa>*pb) { temp = *pa; *pa = *pb; *pb = temp; } if (*pa>*pc) { temp = *pa; *pa = *pc; *pc = temp; } if (*pb>*pc) { temp = *pb; *pb = *pc; *pc = temp; } Console.WriteLine("升序排列a={0},b={1},c={2}" , a, b, c); Console.ReadKey(); } } }
1 2 3 4 5 请输入整数a:3 请输入整数b:2 请输入整数c:1 原始数据a=3 ,b=2 ,c=1 升序排列a=1 ,b=2 ,c=3