An array
11-D Arrays
Count each student's score and calculate the average score of 10 in the class, and then show all the results
Double[] score = new DOUBLE[10];
Double sum = 0;
for (int i = 0; i <; i++)
{
Console.WriteLine ("Please enter student score:");
Score[i] = convert.todouble (Console.ReadLine ());
Sum+=score[i];
}
Console.clear ();//Clear screen
Console.WriteLine ("{0} students ' average score is {1}", score. Length,sum/score. Length);
Console.readkey ();
Use dynamic arrays to count the average number of input people without defining the length of the array (dynamic arrays are the definition and initialization parts of the array are expressed in different statements)
Console.WriteLine ("You want to enter the number of results:");
int numbers = Convert.ToInt32 (Console.ReadLine ());//convert string type to int type, TOInt16 () is single-precision floating point, TOInt32 () is int type TOInt64 () is double type
Double[] score = new Double[numbers];
Double sum = 0;
for (int i = 0; i < numbers; i++)
{
Console.WriteLine ("Please enter student's score:");
Score[i] = convert.todouble (Console.ReadLine ());
Sum + = Score[i];
}
Console.clear ();
Console.WriteLine (the average of {0} students is {1} ", Numbers, Sum/score. Length);
Console.readkey ();
22-D Arrays
int[,] arr = new int[2, 3] {{1,2,3},{3,4,5}}; Array initialization
Console.WriteLine ("Number of rows for array: {0}", arr.) Rank);//arr. Rank represents a few-dimensional array
Console.WriteLine ("Number of columns for array: {0}", arr.) GetUpperBound (arr. Rank-1) +1); Arr. GetUpperBound () indicates that the function returns the largest index
Console.WriteLine ("");
for (int i = 0; i < arr. Rank; i++)
{
String str = "";
for (int j = 0; J < arr. GetUpperBound (arr. RANK-1) + 1; J + +)
{
str = str + convert.tostring (arr[i, J]) + "";
}
Console.WriteLine (str);
}
Console.readkey ();
3 Declaration and use of dynamic two-dimensional arrays
Console.WriteLine ("Please enter the number of rows in the dynamic array:");
int m = Convert.ToInt32 (Console.ReadLine ());
Console.WriteLine ("Please enter the number of columns in the dynamic array:");
int n = Convert.ToInt32 (Console.ReadLine ());
int[,] arr=new int[m,n];
for (int i = 0; i < m; i++)
{
for (int j = 0; J < N; j + +)
{
Console.WriteLine (I+j.tostring ());
}
Console.WriteLine ();
}
Console.readkey ();
4 basic operations for arrays
1 sort, can be divided into select sort, bubble sort also have the sort and reverse method through array class
1) Select sort
int[] Array = new int[]{1,23,21,34,233,45,62,35,77};//sort from large to small
for (int i = 0; i < array. Length; i++)
{
Console.Write ("{0}", array[i]+ "");
}
for (int i = 0; I <array. Length-1; i++)
{
int max = i;
for (int j = i+1; J<array. length;j++)
{
if (Array[j] > Array[max])
{
max = J;
}
}
int temp = Array[max];
Array[max] = Array[i];
Array[i] = temp;
}
Console.WriteLine ("");
Console.WriteLine ("The Order after sorting is:");
foreach (int n in array)
Console.Write ("{0}", n+ "");
Console.WriteLine ("");
Console.readkey ();
2) Bubble sort
int[] array = new int[] {1, 23, 21, 34, 233, 45, 62, 35, 77};//from large to small sort
for (int i = 0; i < array. Length; i++)
{
Console.Write ("{0}", array[i]+ "");
}
for (int i = 0; i < array. Length-1; i++)
{
for (int j = 0; J < Array. Length-i-1; J + +)
{
if (Array[j] >array[j+1])
{
int temp = Array[j];
ARRAY[J] = array[j+1];
ARRAY[J+1] = temp;
}
}
}
Console.WriteLine ("");
Console.WriteLine ("The Order after sorting is:");
foreach (int n in array)
Console.Write ("{0}", n + "");
Console.WriteLine ("");
Console.readkey ();
3) Sort sort and reverse sorting for array class
Note You can only sort one-dimensional arrays
int[] array = new int[] {1, 23, 21, 34, 233, 45, 62, 35, 77};//sorted from small to large
for (int i = 0; i < array. Length; i++)
{
Console.Write ("{0}", Array[i] + "");
}
Console.WriteLine ("");
Array.Sort (array);
Console.WriteLine ("Post-order result is:");
foreach (int n in array)
Console.Write ("{0}", n+ "");
Console.readkey ();
int[] array = new int[] {1, 23, 21, 34, 233, 45, 62, 35, 77};//Reverse sort
for (int i = 0; i < array. Length; i++)
{
Console.Write ("{0}", Array[i] + "");
}
Console.WriteLine ("");
Array.reverse (array);
Console.WriteLine ("Post-order result is:");
foreach (int n in array)
Console.Write ("{0}", n + "");
Console.readkey ();
2 merging and splitting of arrays
Merging one-dimensional arrays
int[] arr1 = new int[] {1, 2, 3, 4, 5, 6};
int[] arr2 = new int[] {7, 8, 9, 10, 11, 12};
int n = arr1. Length + arr2. Length;
int[] Arr3 = new Int[n];
for (int i = 0; i < ARR3. Length; i++)
{
if (I < arr1. Length)
Arr3[i] = Arr1[i];
Else
Arr3[i]=arr2[i-arr1. Length];
}
Console.WriteLine ("Merged one-dimensional array:");
foreach (int i in ARR3)
Console.Write ("{0}", i+ "");
Console.readkey ();
Merging two-dimensional arrays
int[] arr1 = new int[] {1, 2, 3, 4, 5, 6};
int[] arr2 = new int[] {7, 8, 9, 10, 11, 12};
int[,] arr4 = new int[2,6];
for (int i = 0; i < ARR4. Rank; i++)
{
Switch (i)
{
Case 0:
{
for (int j = 0; J < arr1. Length; J + +)
Arr4[i, j] = Arr1[j];
Break
}
Case 1:
{
for (int j=0;j<arr2. length;j++)
ARR4[I,J]=ARR2[J];
Break
}
}
}
Console.WriteLine ("Merged two-dimensional array:");
for (int i = 0; i < ARR4. Rank; i++)
{
for (int j = 0; J < Arr4. GetUpperBound (ARR4. RANK-1) + 1; J + +)
Console.Write (arr4[i,j]+ "");
Console.WriteLine ();
}
Console.readkey ();
Splitting of two-dimensional arrays
int[,] arr3 = new int[2, 3] {{1, 2, 3}, {4, 5, 6}};
Int[] arr1= new int[3];
Int[] Arr2=new int[3];
for (int i = 0; i < 2; i++)
{
for (int j = 0; J < 3; J + +)
{
Switch (i)
{
Case 0:arr1[j]=arr3[i,j];break;
Case 1:arr2[j] = Arr3[i, j]; Break
}
}
}
Console.WriteLine ("Array One:");
foreach (int n in arr1)
Console.Write (n+ "");
Console.WriteLine ();
Console.WriteLine ("Array Two:");
foreach (int n in arr2)
Console.Write (n + "");
Console.readkey ();
An array of C # introductory Learning