数组

mac2025-11-18  3

数组

1、数组的定义和使用 格式: 数据类型 数组名[元素个数] 元素个数,代表该数组有多少个相同数据类型的变量 下标 用来表示数组中的某一个元素 例如 int arr[10]; arr[1]代表数组的第二个元素 数组下标是从0开始的 到数组元素个数-1 数组下标越界:超出了数组元素个数的下标,如果操作越界数据会出现程序错误 1、乱码结果 2、报错 求出数组元素个数: int (size_t) unsigned int 个数 = sizeof(数组名)/sizeof(数组元素 | 数组数据类型)

求出数组地址: printf("%p\n",数组名) printf("%p\n",数组元素) 数组元素+1 (sizeof(数据类型)) 数组名+1(sizeof(数组名))

练习:十只小猪称体重 定义一个数组 存储小猪体重 通过遍历找到最重的小猪 找到数组中数据最大值的下标 根据下标打印数据

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int fits[10]; for (int i = 0; i < 10; i++) { scanf("%d", &fits[i]); } //小猪成体重 //int max = 0; //for (int i = 0; i < 10; i++) //{ // if (max < fits[i]) // { // max = fits[i]; // } //} int max = fits[0]; for (int i = 1; i < 10; i++) { if (max < fits[i]) { max = fits[i]; } } printf("最重的小猪体重为:%d\n", max); system("pause"); return EXIT_SUCCESS; }

练习: 找到小猪中第二重的 练习:冒泡排序

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int arr[10] = { 7,4,2,3,5,8,9,6,1,10 }; int len = sizeof(arr) / sizeof(arr[0]) - 1; //冒泡排序 从小到大 for (int i = 0; i < len-1; i++) { for (int j = 0; j < len - 1 - i; j++) { if (arr[j] < arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for (int i = 0; i < 10; i++) { printf("%d\n", arr[i]); } system("pause"); return EXIT_SUCCESS; }

二维数组: 格式:数据类型 数组名【行个数】【列个数】 初始化方式: int arr[2][3] = { {1,2,3},{2,3,4} }; int arr[][3] = { {1,2,3},{2,3,4},{3,4,5} }; int arr[4][3] = { 1,2,3,4,5,6,7,8,9,10 };//arr[3][1] arr[3][2] int arr[4][3] = { {1},{1} ,{1} }; int arr[4][3] = { 1,2,3,4 };

求行数:sizeof(数组名)/sizeof(数组名[0]); 求列数:sizeof(数组名[0])/sizeoef(数组名[0][0])

二维数组首地址表示方式: printf("%p\n",数组名);

练习:10名学生 三门成绩 scores【10】【3】 求出每名学生的总成绩和平均成绩 求出班级的语文 数学 英语的平均成 语数外 【0】【0】 【0】【1】 【0】【2】

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { //定义二维数组 int scores[5][3]; //int stuSum[5] = 0; //录入学生成绩 for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { switch (j) { case 0: printf("请输入语文成绩:\n"); scanf("%d", &scores[i][j]); break; case 1: printf("请输入数学成绩:\n"); scanf("%d", &scores[i][j]); break; case 2: printf("请输入外语成绩:\n"); scanf("%d", &scores[i][j]); break; //stuSum[i] += scores[i][j]; } } } //求出学生的平均成绩 int stuSum = 0; ///int stuAvg = 0; for (int i = 0; i < 5; i++) { stuSum = 0; //stuAvg = 0; for (int j = 0; j < 3; j++) { stuSum += scores[i][j]; //stuAvg = stuSum / 3; } printf("第%d名学生的平均成绩为:%d\n", i + 1, stuSum / 3); } //学科平均成绩 int cSum = 0, mSum = 0, eSum = 0; for (int i = 0; i < 5; i++) { for (int j = 0; j < 3; j++) { switch (j) { case 0: cSum += scores[i][j]; break; case 1: mSum +=scores[i][j]; break; case 2: eSum += scores[i][j]; break; } } } printf("班级的语文平均成绩为:%d\n", cSum / 5); printf("班级的数学平均成绩为:%d\n", mSum / 5); printf("班级的英语平均成绩为:%d\n", eSum / 5); system("pause"); return EXIT_SUCCESS; }

字符数组和字符串: 字符数组和字符串区别在于是否有字符串结束标志 //字符数组 //char arr[10] = { ‘H’,‘e’,‘l’,‘l’,‘o’ }; //char arr[] = { ‘H’,‘e’,’’,‘0’,‘o’ }; //char arr[] = “h\n\0e\nllo”; char arr[100]; scanf("%[^\n]", arr);

//%s会接收字符串结束标志【'\0'】之前的所有字符 在ASCII中就是数字0 printf("%s", arr); //for (int i = 0; i < 10; i++) //{ // printf("%c", arr[i]); //} //printf("%d\n", sizeof(arr));

练习:字符串追加

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> int main14() { char arr1[] = "hello";//01234 char arr2[] = "world"; char arrBuf[100];//'\0' int index = 0; while (arr1[index] != '\0') { //1、将非\0的字符添加到arrbuf arrBuf[index] = arr1[index]; //2、计数器增长 index++; } while (arr2[index - 5] != '\0') { arrBuf[index] = arr2[index - 5]; index++; } //添加字符串结束标志 arrBuf[index] = '\0'; printf("%s", arrBuf); system("pause"); return EXIT_SUCCESS; }

随机数: 1、添加头文件 time.h stdlib.h 2、添加随机数种子 srand((unsigend int )time(NULL)); 3、生成随机数 rand() %

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include <time.h> /* 1、添加头文件 time.h stdlib.h 2、添加随机数种子 srand((unsigend int )time(NULL)); 3、生成随机数 rand() % */ int main15() { srand((unsigned int)time(NULL)); for (int i = 0; i < 100; i++) { printf("%d\n", rand() % 11 + 50);//50-60 } //system("pause"); return EXIT_SUCCESS; }

练习:双色球

#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<string.h> #include<stdlib.h> #include <time.h> int main() { srand((unsigned int)time(NULL)); //双色球 两种原色 红球 + 蓝球 (6+1) 红球 1-33 蓝球 1-16 打印双色球中奖信息 //红色球不能重复 蓝球和红球可以重复 int ball[6]; //红色 for (int i = 0; i < 6; i++) { // qiu = rand()%33+1; ball[i] = rand() % 33 + 1; //去重 for (int j = 0; j < i; j++) { if (ball[i] == ball[j]) { i--; continue; } } } //排序 for (int i = 0; i < 6; i++) { printf("%d ", ball[i]); } printf("+ %d\n", rand() % 16 + 1); system("pause"); return EXIT_SUCCESS; }
最新回复(0)