-----二维数组
->在数组定义当中,行数和列数需要用常量定义
->在定义的时候如果没有数值进行填充,则补零
->第一个数是行,第二个数是列
->行可以不写,但必须定义列
int array [4][3] = {1,2,3,4,5,6,7,8,9,0,1,2}; int array1[4][3] = {{11,12,13},{14,15,16},{17,18,19},{20}}; printf("%d",array[0][0]); printf("\t%d",array1[3][0]); int a = 0 ; int b = 0 ; array[a][b] = 0; printf("\t%d",array[0][0]); int array[][4] = {1,2,3,4,5,6,7,8,9}; int array[][4] = {1,2,3,4,5,6,7,8,9}; printf("%lu",sizeof(array)); int array[][4] = {1,2,3,4,5,6,7,8,9}; // printf("%lu",sizeof(array)); for (int i = 0; i < 3; i ++) { for (int j = 0; j < 4; j++) { printf("%d ",array[i][j]); } printf("\n"); }
->所占用的内存空间
int array[][4] = {1,2,3,4,5,6,7,8,9}; printf("%lu",sizeof(array)); int array[][4] = {1,2,3,4,5,6,7,8,9}; printf("%lu",sizeof(array));
->二维数组打印的话需要两层循环的配合,第一层代表行,第二层代表列
int array[4][5]; for (int i = 0; i < 4; i ++) { for (int j = 0; j < 5; j ++) { array[i][j] = arc4random() % 101; printf("%d\t",array[i][j]); } printf("\n"); }
->数组求和练习
int sum = 0; for (int i = 0; i < 4; i++) { sum = sum + array[0][i]; } printf("\n第0行总和:%d",sum); int sum1 = 0 ; for (int i = 0; i < 3; i ++) { for (int j = 0; j < 4; j++) { sum1 = sum1 + array[i][j]; } printf("\n第%d行的总和:%d",i ,sum1); sum1 = 0; } printf("\n"); int sum2 = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 3; j++) { sum2 = sum2 + array[j][i];//谁变化的快就放到内层循环 } printf("\n第%d列的总和:%d",i,sum2); sum2 = 0; }
——字符串数组(字符数组)
->存储多个字符串
->char string[i][j] 可以保存i个字符串,每个字符串不能大于j-1个
第一位度表示可以放几个字符串,第二维度每个字符串表示最大长度-1;
->可以用一个下标访问,访问的是整个字符串
->两个下标访问的是字符
->不能直接替换
->得用strcpy
char string[3][10] = {"maoyuanbo","maomao","hahahaha"}; printf("%c",string[0][1]); printf("\n%s",string[1]); char string[3][10] = {"iphone","android","winphone"}; printf("%c",string[0][1]); printf("\n%s",string[0]); strcpy(string[0], "nokia"); printf("\n%s",string[0]);
>冒泡排序
1 char string[5][20] = {0}; 2 for (int i = 0; i < 5; i++) { 3 scanf("%s",string[i]); 4 } 5 6 for (int i = 0; i < 4; i++) { 7 for (int j = 0; j < 4 - i; j++) { 8 if (strcmp(string[j], string[j+1]) > 0) { 9 char name[20] = {0}; 10 strcpy(name, string[j]); 11 strcpy(string[j], string[j+1]); 12 strcpy(string[j+1], name); 13 } 14 } 15 } 16 for (int i = 0; i < 5; i++) { 17 printf("\n%s",string[i]); 18 } 19
->选择排序
1 char string[5][20] = {0}; 2 int minindex = 0;//设最小值为第一个数 3 for (int i = 0; i < 5; i++) { 4 scanf("%s",string[i]); 5 } 6 for (int i = 0; i < 4; i++) { 7 for (int j = i + 1; j < 5; j++) { 8 if (strcmp(string[minindex], string[j]) > 0) { 9 minindex = j; 10 } 11 12 } 13 if (minindex != i) { 14 char name[20] = {0}; 15 strcpy(name, string[i]); 16 strcpy(string[i], string[minindex]); 17 strcpy(string[minindex], name); 18 } 19 } 20 for (int i = 0; i < 5; i++) { 21 printf("\n%s",string[i]); 22 } 23
->求最大字符串长度
1 char string[5][20] = {0}; 2 unsigned long maxworld = 0;//定义一个最大的字符串长度 3 for (int i = 0; i < 5; i++) { 4 scanf("%s",string[i]); 5 } 6 //求出最长长度 7 for (int i = 0; i < 5; i++) { 8 if (strlen(string[i]) > maxworld) { 9 maxworld = strlen(string[i]); 10 } 11 } 12 printf("最长长度为:%ld\n",maxworld); 13 for (int i = 0; i < 5; i++) { 14 if (strlen(string[i]) == maxworld) { 15 printf("%s\t",string[i]); 16 } 17 } 18 19 20 char array[5][20] = {0}; 21 unsigned long maxworld = 0; 22 for (int i = 0; i < 5; i++) { 23 scanf("%s",array[i]); 24 } 25 for (int i = 0; i < 5; i++) { 26 if (strlen(array[i]) > maxworld) { 27 maxworld = strlen(array[i]); 28 } 29 } 30 printf("最长长度:%ld\n",maxworld); 31 for (int i = 0; i < 5; i ++) { 32 if (strlen(array[i]) == maxworld) { 33 printf("%s ",array[i]); 34 } 35 } 36
——多维数组
int a[2][2][2] = {0}; int sum = 0,sum1 = 0; for (int i = 0; i < 2; i ++) { for (int j = 0; j < 2; j ++) { for (int k = 0; k < 2; k++) { a[i][j][k] = arc4random() % 11; printf("%d\t",a[i][j][k]); sum1 =a[i][j][k] +sum1; sum = a[i][j][k] +sum; } printf("每一行总和:%d",sum1); sum1 = 0; printf("\n"); } printf("每一层总和:%d",sum); sum = 0; printf("\n\n"); }
时间: 2024-10-02 03:54:35