二維數組

一維數組是以整型、字符等元素組成的數組,而 二維數組,其實即是以一維數組爲元素的數組.
格式:數據類型 數組名[下標1][下標2]  = {值1, 值2, ...};
注:下標1 = 行數; 下標2 = 列數;
二維數組的元素個數 = 行數 * 列數;
須要注意的是:
1.賦值能夠省略,默認值爲0
    int array1[2][3] = {1, 2, 3};
2.行數能夠省略,由 賦值的元素個數 與 列數 決定;
3.列數不能省略;
    int array3[2][] = {1, 2, 3, 4, 5};//error
    int array4[][] = {1, 2, 3, 4};//error
作一個小練習:定義一個5行6列的數組,並隨機給每一個數組賦值,取值範圍[55, 88],並求出最大值和最大值所在的位置
    //1.定義數組
    int array3[5][6] = {0}, max = 0;
    //2.給每一個元素賦值
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 6; j++) {
            array3[i][j] = arc4random() % 34 + 55;
            printf("%d ", array3[i][j]);
            max = max > array3[i][j] ? max : array3[i][j];
        }
        printf("\n");
    }
    printf("最大值是:%d\n", max);
    //因爲是最大值可能出現多個,因此先遍歷數組求出最大值,再次遍歷數組找出值所在的位置
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 6; j++) {
            if (array3[i][j] == max) {
                printf("最大值所在位置爲第%d行 第%d列 ",i + 1, j + 1);
            }
        }
    }
    printf("\n");
    //3行4列,[50, 60]
    int c[3][4] = {0}, d[4][3] = {0};
    printf("交換前:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++) {
            c[i][j] = arc4random() % 11 + 50;
            printf("%d ", c[i][j]);
        }
        printf("\n");
    }
    printf("\n");
    printf("交換後:\n");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            d[i][j] = c[j][i];
            printf("%d ", d[i][j]);
        }
        printf("\n");
    }
字符串數組
字符串數組內存放的元素是字符數組,因此字符串數組是一個二維的數組.
如何打印字符串數組
    //1
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 7 && strings[i][j] != '\0'; j++) {
            printf("%c", strings[i][j]);
            //if (strings[i][j] == '\0') {
            //    break;
            }
            }
        }
        printf("\n");
    }
    //2
    for (int i = 0; i < 3; i++) {
        printf("%s\n", strings[i]);
    }
作兩個小練習,
1.建立一個字符串數組(內容是你周圍⼀一圈人的姓名),輸出最⻓字符串的長度
    char names[3][20] = {"zhangchenghang", "yangxiang", "like"};
    unsigned long length = 0;
    for (int i = 0; i < 3; i++) {
        if (strlen(names[i]) > length) {
            length = strlen(names[i]);
        }
    }
    printf("名字最長的是:\n");
    for (int i = 0; i < 3; i++) {
        if (strlen(names[i]) == length) {
            printf("%s\n", names[i]);
        }
    }
    //輸入字符
    char c = 0;
    scanf("%c", &c);
    //輸入字符串
    char m[10] = {0};
    scanf("%s", m);//不須要加&
    //輸入字符串數組
    char k[3][10] = {0};
    for (int i = 0; i < 3; i++) {
        scanf("%s", k[i]);
    }
    scanf("%s %s %s", k[0], k[1], k[2]);
2.對周圍的學生姓名,進行排序
    char names[3][15] = {"zhangchenghang", "yangxiang", "like"};
    //字符串數組冒泡排序
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2 - i; j++) {
            if (strcmp(names[j], names[j + 1]) > 0) {
                char temp[20] = {0};
                strcpy(temp, names[j]);
                strcpy(names[j], names[j + 1]);
                strcpy(names[j + 1], temp);
            }
        }
    }
    //輸出最後的結果
    for (int i = 0; i < 3; i++) {
        printf("%s\n", names[i]);
    }
   
    int array3[5] = {12, 33, 22, 1, 20};
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4 - i; j++) {
            if (array3[j] > array3[j + 1]) {
                int temp = array3[j];
                array3[j] = array3[j + 1];
                array3[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < 5; i ++) {
        printf("%d ", array3[i]);
    }
    printf("\n");
最後附上今天所作的做業之中一道花費我很長時間的題
3.隨機生成一個三維數組,編程求深度的平均值,保存在一個二維數組中(至關於一個魔方從上面看)
    //[10, 50]
    int array5[2][3][5] = {0}, sum = 0;
    float array6[3][5] = {0.0};
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 5; k++) {
                array5[i][j][k] = arc4random() % 41 +  10;
                printf("%d ", array5[i][j][k]);
            }printf("\n");
        }printf("\n");
    }
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 2; k++) {
                sum += array5[k][j][i];
            }
            array6[j][i] = sum / 2.0;
            printf("%.2f  ", array6[j][i]);
            sum = 0;
        }
        printf("\n");
    }
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 5; j++) {
            printf("%.2f  ", array6[i][j]);
        }
        printf("\n");
    }
相關文章
相關標籤/搜索