Introduction to C language: array

What is an array?

An array is a collection of a set of data of the same data type. Each variable in an array is called an element of the array, and the number of elements an array can hold is called the length of the array.

One-dimensional array

Syntax: array element type [] array name; example: int [] array;

Initialization:

  • Method 1: assign values to each element, for example: int[] a = new int[3]; a[0] = 1; a[1] = 2; a[2] = 3;
  • Method 2: assign values to the whole array directly, for example: int b [] = New Int {4, 5, 6};
  • Method 3: omit the new keyword example: int c = {7, 8, 9};

Two-dimensional array

Syntax:

  • Method 1: data type [,] array name; example: int [,] array;
  • Method 2: data type [] [] array name; example: String [] [] array;

Statement:

  • Method 1: specify the number of rows and columns example: int [,] a = new int[2][4];
  • Method 2: only specify the number of rows. For example: int[][] b = new int[2] []; b[0] = new int[2]; b[1] = new int[3]; / / when using this method, you can specify a different number of columns for each row. (similar to irregular array)

Note: int[][] a = new int[3][3]; this is the wrong way to create an array.

Initialization:

  • Method 1: assign values to single array elements, for example: int [,] arry = new int[2,2]; arry[0,0] = 0; arry[0,1] = 1
  • Method 2: line by line initialization example: int[][] arry = new int[2] []; arry[0] = new int[]{0,1}; arry[1] = new int[]{1,0};
  • Method 3: directly assign values to the whole 2D array, for example: int [,] = new int [2,2] {{0,1}, {1,0}; int [,] = new int [,] {0,1}, {1,0}; or int arry = {0,1}, {1,0};

The use of 2D array

Title: transpose row data and column data in two-dimensional array.

Answer:

            int[,] arry = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
            for (int i = 0; i < 3; i++)
            {
               for (int j = 0; j < 3; j++)
                {
                    Console.Write(arry[i, j]+"  ");
                }
                Console.WriteLine("\n");
            }
            int temp;
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < i; j++)
                {
                    temp = arry[i, j];
                    arry[i, j] = arry[j, i];
                    arry[j, i] = temp;
                }
            }
            Console.WriteLine("new data: ");
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.Write(arry[i, j] + "  ");
                }
                Console.WriteLine("\n");
            }


Basic operation of array

  • Input and output of one dimensional array
            int[] arry = new int[100];
            for (int i = 0; i < 100; i++)
            {
                arry[i] = i + 1;
            }
            for (int i = 0; i < arry.Length; i++)
            {
                Console.Write(arry[i] + " ");
            }
  • Input and output of two dimensional array

Topic: input the student number and Chinese, mathematics and English scores in the console, and then output the student's scores, average scores and total scores.

Answer:

            Console.WriteLine("Please enter the total number of people:");
            int count = Convert.ToInt32(Console.ReadLine());
           
            int[,] record = new int[count,4];
            for (int i = 0; i < count; i++)
            {
                Console.Write("Please enter student ID: ", i+1);
                record[i, 0] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter your Chinese score: ");
                record[i, 1] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter your math score: ");
                record[i, 2] = Convert.ToInt32(Console.ReadLine());
                Console.Write("Please enter your English score: ");
                record[i, 3] = Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine("Student ID\t Chinese\t Mathematics\t English?\t Total score\t Average");
            for (int i = 0; i < count; i++)
            {
                int sum = 0, ave = 0;
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(record[i, j] + "\t");
                    if (j > 0)
                    {
                        sum += record[i,j];
                    }
                }
                ave = sum / 3;
                Console.Write(sum + "\t" + string.Format("{0:F2}", ave) + "\n");
            }

Traversing arrays with foreach statement

Syntax: foreach ([type] [variable name] in [set]) {statement}

Description: [set] must have a display conversion from the element type of the set to the type of iteration variable.

Title: use string array to store in the main function module of inventory management system, and then use foreach statement to traverse and output.

Answer:

            string[] names = { "Stock management", "Sales management", "Inventory management", "System setup", "Common tools" };
            foreach (string str in names)
            {
                Console.WriteLine(str);
            }

Sort arrays

Method: use the sort method in the Array class to sort the Array elements in order, and reverse to reverse the Array elements.

Example:

            int[] arry = { 3, 4, 1, 7, 5, 6, 9, 8, 2, 0 };
            Array.Sort(arry);
            foreach (int i in arry)
            {
                Console.WriteLine(i);
            }

Sorting method of array

  • Bubble sort
            int[] arry = { 3, 4, 1, 7, 5, 6, 9, 8, 2, 0 };
            for (int i = 0; i < arry.Length - 1; i++)
            {
                for (int j = i + 1; j < arry.Length; j++)
                {
                    if (arry[i] > arry[j])
                    {
                        int temp = arry[i];
                        arry[i] = arry[j];
                        arry[j] = temp;
                    }
                }
            }
            for (int i = 0; i < arry.Length; i++)
            {
                Console.Write(arry[i]+" ");
            }
  • Selection sort
            int[] arry = { 3, 4, 1, 7, 5, 6, 9, 8, 2, 0 };
            int min;
            for (int i = 0; i < arry.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < arry.Length; j++)
                {
                    if (arry[j] < arry[min])
                    {
                        min = j;
                    }
                }
                int temp = arry[min];
                arry[min] = arry[i];
                arry[i] = temp;
            }
            for (int i = 0; i < arry.Length; i++)
            {
                Console.Write(arry[i] + " ");
            }

 

Published 9 original articles, praised 0, visited 52
Private letter follow

Posted on Tue, 10 Mar 2020 04:51:54 -0400 by warran