The sixth day of IOS Learning (array), and the sixth day of ios

Source: Internet
Author: User

The sixth day of IOS Learning (array), and the sixth day of ios

Knowledge Point sorting for IOS Learning (C language)

I. Integer Array

1) concept: a group of data with the same type and continuous spatial distribution.

2) int a [10] int type contains 10 elements. The array name is a, and a is the first address of the array.

3) Reference element a [I] in the array: array name + subscript. The subscript starts from 0.

4) The number of array elements is equivalent to the length of the array, which is called full initialization.

5) The traversal of the array accesses all elements in the array.

6) element address & a [0] Get address character + array name + subscript

7) traversal of input, access not out-of-bounds, and out-of-bounds compiler cannot check, posing a high security risk

8) the sizeof of the int array is equal to the length of the array multiplied by 4.

9) after partial initialization, all unspecified parts are assigned 0 values.

10) int a [5] = {0}; int a [5] = {}; partially initialized, complete array

11) the maximum subscript of the array is the length of the array minus one for example: int a [] = {1, 2, 3, 4, 5} len = sizeof (a)/sizeof (int ); the maximum subscript is the len-1.

12) the array length [] can only be a constant or a symbolic constant.

13) array Initialization

①. Int list [5] = {2, 13, 58,55, 19}; // general syntax

②. Int list [5] = {6, 17}; // only assign values to the first two elements

③. Int list [5] = {[3] = 25, [4] = 51}; // assign values to the specified element. Here, the values are the third and fourth.

④ Int list [] = {11,12, 13}. // correct. If the element on the right is determined, the number can be omitted as 3.

⑤ Int list []; // error. The compiler cannot know how much storage space should be allocated.

6. Int list [5]; list = {17,18, 19,20, 21}; // error. initialization can only be performed when an array is defined.

7. Int list ['a'] = {1, 2, 3}; // correct, equivalent to ages [65]

Counter. Int count = 5; int list [count]; // If Initialization is not performed, this method is correct and the compiler will not report an error for allocating 20 bytes of storage space to it,

List [0] = 1; list [1] = 2; you can assign values to elements in the array like this, but the values of elements such as 2, 3, and 4 are uncertain.

Invalid. int count = 5; int list [count] = {1, 2, 3, 4, 5}; // This method is incorrect. When defining an array, initialize the array, the number of elements must be

Constant or not written. It cannot be a variable.

14) Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, 21, 34...

Implementation Code:

 1 int main(){ 2    int arr[20]={}; 3     for (int i=0; i<20; i++) { 4         if(i<=1) 5             arr[i]=1; 6          else 7           arr[i]=arr[i-1]+arr[i-2]; 8     } 9     for (int i=0; i<20; i++) {10         printf("%d ",arr[i]);11     }12     return 0;13  }

Ii. Sorting

1) Select sorting: select the smallest number from the unordered series each time and place it behind the ordered queue.

For example: int a [] = {, 0 };

1. 0] 3,556, 78

2 .],

3. 12,556, 9], 78

4 .,]

5. 556 ,]

Implementation Code:

1 int main () {2 int a [] = {12, 3, 556,0, 9,78}; 3 int len = sizeof (a)/sizeof (int); 4 int k; // The minimum number of records subscript 5 int temp; 6 // a total of 7 for len-1 (int I = 0; I <len-1; I ++) {8 // from a [I] ~ Find the smallest value in a [len-1] and place it in the position of a [I] 9 k = I; 10 for (int j = I + 1; j <len; j ++) {11 if (a [j] <a [k]) {12 k = j; 13} 14} 15 16 // a [k] And a [I] exchange once 17 if (k! = I) {18 temp = a [k]; 19 a [k] = a [I]; 20 a [I] = temp; 21} 22} 23 24 for (int I = 0; I <len; I ++) {25 printf ("% d", a [I]); 26} 27 return 0; 28}

2) Bubble Sorting: The large number is sunk, And the decimal number is floating up.

Compares adjacent elements. If the first is bigger than the second, exchange the two of them.

Int a [] = {, 0 };

Five comparisons are required.

1.1 [],

1.2 3, [12,556], 78

1.3 0,556, [],

1.4 9,556, 12, 0, [], 78

1.5, 12, 78,556, []-> end of a round, the maximum number is sorted at the bottom.

Implementation Code:

1 int main () 2 {3 int a [] = {12, 3, 556,0, 9,78}; 4 int temp; 5 int len = sizeof (a)/sizeof (int ); 6 // row len-1 times 7 for (int I = 0; I <len-1; I ++) {8 // unordered series: a [0] ~ A [len-1-i], by comparing 9 // front> next, exchange 10 // I = 0, the last pair: a [len-2], a [len-1] 11 for (int j = 0; j <len-1-i; j ++) {12 if (a [j]> a [j + 1]) {13 temp = a [j]; 14 a [j] = a [j + 1]; 15 a [j + 1] = temp; 16} 17} 18} 19 20 for (int I = 0; I <len; I ++) {21 printf ("% d", a [I]); 22} 23 return 0; 24}

3) Insert sorting

Int a [] = {, 0 };

Initialize 12 [3,556, 78]

1. 3,12 [556,0, 9,78]

2. 556, [, 78]

3. 12,556, []

4. 556, [78]

5. 78,556

}

Implementation Code:

1 int main () 2 {3 int a [] = {12, 3, 556,0, 9,78}; 4 int len = sizeof (a)/sizeof (int ); 5 int insert = 0; 6 // a total of 7 for len-1 (int I = 0; I <len-1; I ++) {8 // insert a [I + 1] to the first ordered sequence a [0] ~ A [I] 9 insert = a [I + 1]; 10 for (int j = 0; j <= I; j ++) {11 if (a [j]> insert) {12 // insert a [j] This position 13 // move a [j] ~ A [I] moves a 14 printf ("insert % d, pos = % d \ n", insert, j); 15 for (int k = I; k> = j; k --) {16 a [k + 1] = a [k]; 17} 18 // enter insert19 a [j] = insert; 20 break; 21} 22} 23 24 for (int ii = 0; ii <len; ii ++) {25 printf ("% d", a [ii]); 26} 27 printf ("\ n"); 28} 29 30 for (int ii = 0; ii <len; ii ++) {31 printf ("% d ", a [ii]); 32} 33 return 0; 34}

3. character array

1) Calculate the valid length of the character Array Using strlen (array name)

2) instance:

Code for converting uppercase to lowercase:

1 int main () {2 char str [10] = {'h', 'E', 'l', 'l', 'O '}; 3 int len = (int) strlen (str); 4 // convert lowercase to uppercase 5 for (int I = 0; I <len; I ++) {6 if (str [I]> = 'A' & str [I] <= 'Z ') {7 str [I] = str [I]-('A'-'A'); 8} 9} 10 printf ("% s", str ); 11 12 return 0; 13}

Iv. Two-dimensional array

1) instance:

Print the Yang Hui triangle using a two-dimensional array

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

Implementation Code:

 1  int main(){ 2     int a[10][10]; 3     for(int i=0;i<10;i++){ 4         for(int j=0;j<=i;j++){ 5             if(j==0||i==j) 6                a[i][j]=1; 7              else 8                 a[i][j]=a[i-1][j]+a[i-1][j-1]; 9         }10     }11 12     for(int i=0;i<10;i++){13         for(int j=0;j<=i;j++){14             printf("%5d",a[i][j]);15         }16         printf("\n");17     }18     return 0;19 }

5. Data exchange without third-party Variables

Implementation Code:

1 int main(){2   int a=20,b=30;3   a=a+b;4   b=a-b;5   a=a-b;6   printf("a=%d;b=%d",a,b);7 8   return 0;9 }

 

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.