C#实例练习4:数组和指针

参考书籍:《C#程序设计实验指导与习题测试》

笔记同步发表在我的个人博客上,欢迎访问!传送门

实验目的

  • 掌握数组的声明、实例化、和初始化
  • 掌握数组元素的使用
  • 掌握一维数组的操作
  • 掌握二维数组的操作
  • 了解System.Array类常用方法和属性的使用
  • 了解指针的基本操作


实验1:求若干学生的平均身高、最高身高、最低身高以及高于平均身高的人数

已知10个学生的身高为156、150、167、178、180、176、173、154、155、158,求平均身高、最高身高

最低身高,并统计高于平均身高的人数。

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();
        }
    }
}
学生身高信息如下:
 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为不及格)

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();
        }
    }
}
学生成绩如下:
  80  90  67  89  78  85  45  69  77  95
成绩优秀的人数为2,所占百分比为20%
成绩良好的人数为3,所占百分比为30%
成绩一般的人数为4,所占百分比为40%
成绩不合格的人数为1,所占百分比为10%

实验3:冒泡排序

随机生成10个学生的成绩(0-100的整数),递减排序

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();
        }
    }
}
 原始数组数据为
 94 13 37 16 89 83 37 42 44 31
 降序排列后的数组为
 94 13 13 13 13 13 37 37 37 31

实验4:选择排序

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();
        }
    }
}

 原始数组数据为
 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之间

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];
            //分别给数组A和B赋值
            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();
            }
            //上三角显示数组A
            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下三角");
            //下三角显示数组B
            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();
        }
    }
}

原始数组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行杨辉三角

扫描二维码关注公众号,回复: 13741056 查看本文章
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
                     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)的维度、长度,并对数组排序,反转。

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();
        }
    }
}
数组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使用指针对其进行升序排列

在这里插入图片描述

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();
        }
    }
}
请输入整数a:3
请输入整数b:2
请输入整数c:1
原始数据a=3,b=2,c=1
升序排列a=1,b=2,c=3

猜你喜欢

转载自blog.csdn.net/weixin_50915462/article/details/115315529