9. C # basic sorting (multi-dimensional array ),

Source: Internet
Author: User

9. C # basic sorting (multi-dimensional array ),
Multi-dimensional array 1 and two-dimensional array:

Representation Method:

Int [y, x], x and y are indexes, y represents rows, and x represents columns.

Example:

Int [,] second = new int [2, 3] {3, 2, 5}, {6, 7, 8}; // {} can be left empty

Modification method:

Second [0, 1] = 3; // indicates that the number in column 0th of row 1st is changed to 3.

Exercise: Use a two-dimensional array for Bubble Sorting:

Enter the number of people, enter the age, height, and name of each person, and calculate the average age, sorted by height from high to low

Console. writeLine ("Enter the number of students:"); int n = int. parse (Console. readLine (); string [,] ren = new string [n, 3]; // enter the information of each student for (int I = 0; I <n; I ++) {Console. writeLine ("Enter name, age, and height, separated by the Enter key:"); for (int j = 0; j <3; j ++) {ren [I, j] = Console. readLine () ;}} double sum = 0; // calculate the total age, print the average age for (int I = 0; I <n; I ++) {sum = sum + int. parse (ren [I, 1]);} Console. writeLine ("Average age: {0}", Math. floor (sum/n); Console. writeLine ("name, age, height"); // sort by height (int I = 0; I <n; I ++) {for (int j = I; j <n; j ++) {if (int. parse (ren [j, 2])> int. parse (ren [I, 2]) {string [] zhong = {ren [j, 0], ren [j, 1], ren [j, 2]}; // exchange all information so that the height order is consistent with the name and age. ren [j, 0] = ren [I, 0]; ren [j, 1] = ren [I, 1]; ren [j, 2] = ren [I, 2]; ren [I, 0] = zhong [0]; ren [I, 1] = zhong [1]; ren [I, 2] = zhong [2] ;}} int [,] AB = new int [0, 0]; for (int I = 0; I <n; I ++) {for (int j = 0; j <3; j ++) {Console. write (ren [I, j] + "");} Console. write ("\ n ");}
* 2. Multi-dimensional array

Description: int [z, y, x]: z indicates several two-dimensional arrays.

 

Related Article

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.