• 二维数组


    一维数组是以整型、字符等元素组成的数组,而二维数组,其实便是以一维数组为元素的数组.
    格式:数据类型 数组名[下标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(" ");
        }
        printf("最大值是:%d ", 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(" ");
        //3行4列,[50, 60]
        int c[3][4] = {0}, d[4][3] = {0};
        printf("交换前: ");
        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(" ");
        }
        printf(" ");
        printf("交换后: ");
        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(" ");
        }
    字符串数组
    字符串数组内存放的元素是字符数组,所以字符串数组是一个二维的数组.
    如何打印字符串数组
        //1
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 7 && strings[i][j] != ''; j++) {
                printf("%c", strings[i][j]);
                //if (strings[i][j] == '') {
                //    break;
                }
                }
            }
            printf(" ");
        }
        //2
        for (int i = 0; i < 3; i++) {
            printf("%s ", 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("名字最长的是: ");
        for (int i = 0; i < 3; i++) {
            if (strlen(names[i]) == length) {
                printf("%s ", 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 ", 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(" ");
    最后附上今天所做的作业之中一道花费我很长时间的题
    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(" ");
            }printf(" ");
        }
        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(" ");
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 5; j++) {
                printf("%.2f  ", array6[i][j]);
            }
            printf(" ");
        }
  • 相关阅读:
    Django模板
    创建我的第一个django项目
    python django环境配置与安装
    python第九期学习笔记(装饰器进阶)
    python第九期学习笔记(十二)(线程)
    python第九期学习笔记(装饰器)
    python第九期学习笔记(闭包)
    python第九期学习笔记(十二)(面向对象)
    python第九期学习笔记(十一)(面向对象)
    python第九期学习笔记(十)(模块)
  • 原文地址:https://www.cnblogs.com/xlsn0w/p/4831391.html
Copyright © 2020-2023  润新知