"Learning the path of iOS: C" one-dimensional array, array sorting, character array

Source: Internet
Author: User
Tags array definition array sort

1. Arrays

Array to quickly define multiple variables.

Array Definition: Data type array name [number of elements of array] = {value 1, value 2, value 3};

The amount of storage space the array occupies = the number of array elements * The amount of storage space each element occupies.

%lu unsigned long unsigned longer integer. Unsigned >=0 sizeof stores the size value//used to calculate a variable, the type, and the size of the storage space the array occupies. int a[5] = {2, 7, 4, 3, 6}; printf ("%lu\n", sizeof (arr)); 5 int B[5] = {2, 7, 4}; printf ("%lu\n", sizeof (arr)); 5 int C[5] = {0}; printf ("%lu\n", sizeof (arr)); 5, because the defined space size is 5 int d[] = {2, 7, 4, 3, 6}; printf ("%lu\n", sizeof (arr)); 5

</pre><pre>
 

Gets the elements in the array: by array name + subscript. The C language does not detect array subscript out of bounds.

    ARR[5] =;    printf ("%d", arr[5]);//corner mark out of Bounds

1. The space occupied by the string is less than the string size 1, because the string ends with a "\" table. The string handler provided by the system determines whether the string ends in terms of '% '.

     Char arr[] = "IPhO";     printf ("%lu\n", sizeof (arr)); Storage size is 5 because there are default   <pre name= "code" class= "CPP" ><pre name= "code" class= "CPP" >     printf ("%lu\n" , strlen (arr)); String length is 4


Example:

1. Tell the output of each element in the array

   int arr[5] = {1, 3, 5, 6, 7};    for (int i = 0; i < 5; i++) {        printf ("%d\n", Arr[i]);    }

2. Assign a value to each element in the array [20, 40]

  int a[10] = {0};    for (int i = 0; i < ten; i++) {        A[i] = arc4random ()% (40-20 + 1) +;        printf ("a[%d" is%d\n ", I, A[i]);    }

3. Find the smallest value in the array and assign a value to each element in the array [20, 40].

    int a[10] ={0};</span>     <span style= "color: #000000;" >for (int i = 0; i < ten; i++) {        A[i] = arc4random ()% (40-20 + 1) +;        printf ("a[%d" is%d\n ", I, A[i]);    }       int  min = 0;    for (int i = 0; i < i++) {        if (i = = 0) {            min =a[0];        } else {            if (min > A[i]) {                min = a[i] ;            }        }    }    printf ("Maximum value:%d\n", Min);

4.defines a 10-bit array in which the values of the elements in the array are [30,50], and all elements are evaluated.
    int arr[10] = {0};    for (int i = 0; i < ten; i++) {        Arr[i] = arc4random ()% (50-30 + 1) +;        printf ("%d\n", Arr[i]);    }    int sum = 0;    for (int i = 0; i < i++) {        sum + = Arr[i];    }    printf ("Sum of all elements:%d\n", sums);

2. Array sorting

Bubble Sort:

Involves double loops:
The outer loop controls the number of trips, the outer layer minus one, can be reduced, 1 the purpose is to provide the execution efficiency of the program.
The inner loop controls the number of comparisons. The inner layer is reduced by one and must be reduced to prevent cross-border comparisons.
The inner layer-I, can not be reduced,-I is to reduce the scope of the disordered area, improve the efficiency of the program execution.

The code is as follows:

    int  a[10] = {0};    for (int i = 0; i < ten; i++) {        A[i] = arc4random ()% (15-10 + 1) +;        printf ("%d  ", A[i]);    }    for (int i = 0, i < 9; i++) {for        (int j = 0, J < 10-1-I; j + +) {            if (A[j] < a[j+1]) {//Descending                Order int  temp  =a[j];                A[J] = a[j + 1];                A[j + 1] = temp;}}    }    printf ("\ n");    for (int  i = 0; i < i++) {        printf ("%d  ", A[i]);    }

3. Character array

Char str[4]= {' F ', ' R ', ' ', ' L ', ' N '};
printf ("%lu\n", sizeof (STR5));

Char str[] = "Phone"; String

String to hide the character, as a string end flag.
If you do not specify the number of elements, the system opens up space and automatically identifies the number of elements in the initial value.

    Char str5[]= {' F ', ' R ', ' L ', ' N ', ' d '};//if the space size is defined [6],sizeof is 6, length is 5    char str6[]= "FRLN";    printf ("STR5 =%lu\n", sizeof (STR5));  The size of the space is 5    printf ("STR6 =%lu\n", sizeof (STR6)),///Space size 5, hide    ("Str5 =%lu\n", strlen (STR5));//Length 5    printf ("Str33 =%s\n", STR5); Frlnd    printf ("Str44 =%s\n", STR6);//Frln    printf ("Str6 =%lu\n", strlen (STR6));//Length 4
How do I output a string?
Array Name: The first address that can represent an array, is a constant.cannot be changed, cannot be assigned.
Char str[] = "iphone";
printf ("%s\n", &str[4]);//Take out NE, starting from fourth, take out the contents of the future
printf ("%c\n", str[4]);

3. String processing

Char str[10] = "language";    The length of the string. printf ("%lu\n", strlen (str));    8//String copy strcpy (str, "AA"); printf ("%s\n", str);    Because there are hidden characters, you can only print out AA.    String stitching strcat (str, "BB"); printf ("%s\n", str);//Because there is a hidden character, you can only print out AABB. But there are values, as follows printf ("%c\n", str[6]);      The result is a G//string comparison of printf ("%d\n", strcmp ("AA", "BB"));       //b-a =-1 printf ("%d\n", strcmp ("Da", "BB"));     //(D-b) + (b-a) =2 printf ("%d\n", strcmp ("BBA", "BB"));  //As on 97


Learning iOS: C language One-dimensional array, array sort, character array

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.