December 27 Notes

Source: Internet
Author: User

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.

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.

Job: 1. First write the bubble sort once.

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: a tabular model.

Definition: 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: array name [subscript, subscript] = value;

a[0,0] = 5;

a[2,3] = 10;

Value: array name [subscript, subscript];

Application:

December 27 Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.