Human thought--Computer logical thinking
Logical thinking--code implementation
Writing Calligraphy:
understandriginally--copy--monument--self-contained--grass
Chapter Review:
Arrays: one-dimensional, two-dimensional, multidimensional
One-dimensional: beans. Continuous, same type.
Definition: Data type [] array name =new data type [length]{.,.,.,.};
Assignment: array name [subscript] = value
Value: array name [subscript]
Flexible use: combined with a For loop application.
1. Ask for the maximum value, minimum value.
2. Seek the sum, average.
3. Random (generate subscript) pumping value.
New lesson:
Array of applications:
(i). Bubble sort.
1. Bubble sorting is solved with a double loop. The outer loop is the number of trips, the inner loop is the number of times.
2. Number of =n-1; number of =n-times.
3. The inner loop uses the if comparison to the size of the two numbers, and carries on the numerical exchange.
Homework:
1. First write the bubble sort again.
2. Use the bubble sort to do the grading procedure for the Green Song Contest. Ask for the average score by removing the two highest, two minimum points.
Code.
(b). Binary Find.
The premise: The array must be ordered.
Idea: Use two variables to represent the upper (top) and lower bound (bottom) subscript, and then use a variable to represent the middle (mid) subscript.
1. Intermediate Subscript: Mid = (top+bottom)/2
2. The upper limit subscript moves down: top = mid+1. Suppose the array is in ascending order.
3. Lower limit subscript up: bottom = mid-1;
4. The cycle condition is: bottom>=top
static void Main (string[] args)
{
Int[] A = new int[] {3, 5, 7, 9, 11, 13, 14, 18};
Console.Write ("Please enter the number to find:");
int find = Convert.ToInt32 (Console.ReadLine ());
int top, bottom, mid; Upper subscript, lower subscript, intermediate subscript
top = 0;
bottom = a.length-1;
while (Bottom>=top)//As long as the lower subscript is still below the upper limit subscript, it loops, or the end is not found.
{
Calculate the intermediate subscript
Mid = (top + bottom)/2;
Take the middle value
int n = a[mid];
if (n < find)
{
top = mid + 1; Adjust the subscript of the upper limit
}
else if (n>find)
{
Bottom = mid-1;//subscript for adjusting the lower limit.
}
Else
{
Console.WriteLine ("Found, on the first" + Mid + "elements");
Break
}
}
}
Two-dimensional arrays:
The model of the table.
Defined:
data type [,] array name = new array type [dimension length, dimension length];
int[,] a = new int[3,4];
int[,] A = new int[3, 4] {{1, 2, 3, 4},{5, 6, 7, 8}, {9, 0, 9, 8}};
Assignment value:
Array name [subscript, subscript] = value;
a[0,0] = 5;
a[2,3] = 10;
Value:
Array name [subscript, subscript];
Application:
Example: This is a bubble sort
static void CCC (string[] args)
{
Int[] A = new Int[8] {9, 12, 7, 5, 15, 2, 1, 8};
Bubble sort.
for (int i=1;i<=a.length-1;i++)//number of times
{
for (int j = 1; J <= A.length-i; j + +)//number
{
if (A[j-1] > A[j])
{
int t = a[j-1];
A[J-1] = A[j];
A[J] = t;
}
}
}
Show
for (int k=0;k<a.length;k++)
{
Console.WriteLine (A[k]);
}
}
Example: Binary find
static void Man (string[] args)
{
Int[] A = new int[] {3, 5, 7, 9, 11, 13, 14, 18};
Console.Write ("Please enter the number to find:");
int find = Convert.ToInt32 (Console.ReadLine ());
int top, bottom, mid; Upper subscript, lower subscript, intermediate subscript
top = 0;
bottom = a.length-1;
while (Bottom>=top)//As long as the lower subscript is still below the upper limit subscript, it loops, or the end is not found.
{
Calculate the intermediate subscript
Mid = (top + bottom)/2;
Take the middle value
int n = a[mid];
if (n < find)
{
top = mid + 1; Adjust the subscript of the upper limit
}
else if (n>find)
{
Bottom = mid-1;//subscript for adjusting the lower limit.
}
Else
{
Console.WriteLine ("Found, on the first" + Mid + "elements");
Break
}
}
}
An example of a two-dimensional array
static void Main (string[] args)
{int[,] a = new int[3, 4];
Input
for (int i=0;i<3;i++)
{
Automatic generation of study number
A[i, 0] = i+1;
Language achievement
Console.Write ("Language:");
A[i, 1] = Convert.ToInt32 (Console.ReadLine ());
Math Scores
Console.Write ("Mathematics:");
A[i, 2] = Convert.ToInt32 (Console.ReadLine ());
Calculate Total Score
A[i, 3] = a[i, 1] + a[i, 2];
}
Show
Console.WriteLine ("School number \ t language \ t math T total score");
for (int i=0;i<3;i++)
{
for (int j=0;j<4;j++)
{
Console.Write (A[i, j] + "\ t");
}
Console.WriteLine ();
}
}
C#6 Bubble binary Find two-dimensional arrays