C language--Arrays & sorting

Source: Internet
Author: User

1. Arrays

Array definition form: int score[100];

Array subscript: Starting from 0, score[0],score[1]...score[99];

A[n], a[0].....a[n-1]

Data type identifier [constant expression]

1. The same type of each element in the array

2. The length of the array must be constant (length refers to the number of array elements)

3. The array name must conform to the identifier definition

4. Array subscript starting from 0

5. The array name is an address constant, not a variable

6. Each element in the array is a variable

Size of the array in memory = data type length * Number of array elements

Array access is not allowed to cross operations!

1.1 Array Initialization

Full initialization of//int main (int argc, const char *argv[]) {    int a[5]={1,2,3,4,5};    for (int i=0; i<5; i++) {        printf ("a[%d]==%d\n", I, A[i]);    }        return 0;}

not fully initialized int main (int argc, const char *argv[]) {    int a[5]={3,6};    for (int i=0; i< 5; i++) {        printf ("a[%d]==%d\n", I, A[i]);    }    return 0;}

Initialized to 0int main (int argc, const char *argv[]) {    //int a[5]={};    int a[5]={0};    for (int i=0; i<5; i++) {        printf ("a[%d]==%d\n", I,a[i]);    }    return 0;}

1.2 Simple array operation

Array traversal int main (int argc, const char * argv[]) {    int a[10];    int i;    for (i=0; i<10; i++) {        a[i]=i;    }    for (i=0; i<10; i++) {        printf ("a[%d]==%d\n", I,a[i]);    }    return 0;}

Reverse Operation array int main (int argc, const char *argv[]) {    int a[5];    for (int i = 4; I >=0; i--) {        scanf ("%d", &a[i]);    }    for (int i=0; i<5; i++) {        printf ("a[%d]==%d\n", I, A[i]);    }    return 0;}

eg. ask for a student's average score

int main (int argc, const char *argv[]) {    int total=0;    float average;    int a[10];    for (int i=0; i<10; i++) {        scanf ("%d", &a[i]);        Total+=a[i];    }    Average = (float) (TOTAL/10);    printf ("average = =%.2f\n", average);        return 0;}

eg. enter 10 data to find the maximum number in the data and output it to the screen

int main (int argc, const char *argv[]) {    int a[10];    int Max;   Input array    for (int i=0; i<10; i++) {        scanf ("%d", &a[i]);    }    max = a[0];   Loop Compare size for    (int i=1; i<10; i++) {        if (A[i]>max) {            max = a[i];        }    }    printf ("The max value is:%d \ n", max);    return 0;}

eg. character array

Character array//char str[100];//each element occupies 1 bytes of space//int main (int argc, const char *argv[]) {    //1    char str[100];    scanf ("%s", str);    printf ("%s\n", str);        2   Char str[5]={' A ', ' B ', ' C ', ' d ', ' e '};    for (int i=0; i<5; i++) {        printf ("%c", Str[i]);    }    Putchar (' \ n ');     return 0;}

eg. character array manipulation

string int main (int argc, const char *argv[]) {    char str[100]= "Qianfengjiaoyu";    printf ("%s\n", str);} /*int Main (int argc, const char *argv[]) {    char str[12]= "Hello world!";    str[12]= ' + ';    printf ("%s", str);    for (int i=0; i<12; i++) {        printf ("%c", Str[i]);    }    return 0;} */

2. Two-dimensional array

eg. initialization & traversal

Two-dimensional array initialization//initialization mode 1int main (int argc, const char *argv[]) {    int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};    int I, J;   Iterate over array for    (i=0; i<3; i++) {for        (j=0; j<4; J + +) {            printf ("%d", A[i][j]);}    }    return  0;} Initialization mode 2int main (int argc,const char *argv[]) {    int a[3][4]={{1,2,3,4},                 {5,6,7,8},                 {9,10,11,12}};    for (int i=0; i<3; i++) {        for (int j=0; j<4; J + +) {            printf ("%d", a[i][j]);        }        printf ("\ n");    }    printf ("\ n");    return 0;}

eg. printing Yang Hui triangles (two-dimensional arrays)

1//1 1//1 2 1//1 3 3 1//1 4 6 4 1//..../*    2=1+1    3=1+2    3=2+1    4=1+3    6=3+3    4=3+1    .... */int mai n (int argc,const char *argv[]) {    int a[10][10];    int I, J;    for (i=0; i<10; i++) {        //inner loop print specific each digit for        (j=0; j<=i; J + +) {            //Initialize triangle graphics left and right sides 1            if (j==0 | | i==j) {                a[i][j]=1;            }            else            {                a[i][j]=a[i-1][j-1]+a[i-1][j];            }            printf ("%d  ", a[i][j]);        }        printf ("\ n");    }    return 0;}

3. Array application (sorting algorithm)

3.1 Bubble sort

Bubble sort: The next two numbers compare, the large number moves backwards, the decimal moves forward;//4 5 7 8 9//  9 7 8 5 4//First order, Exchange 4 times//  7 9 8 5 4//  7 8 9 5 4//  7 8 5 9 4//
   
    7 8 5 4 9//Second order, Exchange 3 times//  7 8 5 4//  7 5 8 4//  7 5 4 8 9//Third Order, Exchange 2 times//  5 7 4 8 9//  5 4 7 8 9//Fourth order//
    4 5 7 8 9#define LEN 10int Main (int argc, const char * argv[]) {    int a[len];    int I, J;    int temp;   Traverse input for    (i=0; i<len; i++) {        scanf ("%d", &a[i]);    }    for (i = 0; i<len-1; i++) {for        (j=0; j<len-1-i; J + +) {            //Swap position            if (a[j]>a[j+1]) {                temp = a[j] ;                A[J] = a[j+1];                A[j+1]=temp    ;    }}} Traverse output    for (i=0; i<len; i++) {        printf ("%d", A[i]);    }    Putchar (' \ n ');        return 0;}
   

3.2 Select Sort

Select Sort//9 7 8 5 4////First Order//4 7 8 5 9//Second sort//4 5 8 7 9//Third sort//4 5 7 8 9//Fourth order//4 5 7 8 9int Main (int argc, const char * Argv[]) {    //int a[5]={9, 7, 8, 5, 4};    int A[len];    int i,j;    int k,temp;    for (i=0; i<len; i++) {        scanf ("%d", &a[i]);    }    for (i=0; i<len-1; i++) {        k=i;        for (j=i+1; j<len; J + +) {            if (A[k]>a[j]) {                k=j;            }        }        if (k!=i) {            temp = a[k];            A[K] = A[i];            A[i] = temp;        }    }    for (i = 0; i<len; i++) {        printf ("%d", A[i]);    }    Putchar (' \ n ');    return 0;}

C language--Arrays & sorting

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.