1 2 0 x 0 C FIQ IRQ

  • Slides: 49
Download presentation
第七部分 数组 1 一维数组的定义和引用 2 0 x 0 C FIQ IRQ (Reserved) Data Abort

第七部分 数组 1 一维数组的定义和引用 2 0 x 0 C FIQ IRQ (Reserved) Data Abort Prefetch Abort 0 x 08 Software Interrupt 0 x 04 Undefined Instruction 0 x 00 Reset 0 x 1 C 0 x 18 0 x 14 0 x 10 1 二维数组的定义和引用 3 字符数组

程序举例 #include <stdio. h> #define SIZE 10 例 读 10个整数存入数组,找出其中最大值和最小值 main() { int x[SIZE],

程序举例 #include <stdio. h> #define SIZE 10 例 读 10个整数存入数组,找出其中最大值和最小值 main() { int x[SIZE], i, max, min; printf("Enter 10 integers: n"); for(i=0; i<SIZE; i++) 步骤: { printf("%d: ", i+1); 1. 输入: for循环输入 10个整数scanf("%d", &x[i]); 2. 处理: } (a) 先令max=min=x[0]; (b) 依次用x[i]和max, min比较(循环) for(i=1; i<SIZE; i++) 若max<x[i], 令max=x[i] { if(max<x[i]) max=x[i]; 若min>x[i], 令min=x[i] if(min>x[i]) min=x[i]; 3. 输出: max和min } printf("Maximum value is %dn", max); printf("Minimum value is %dn", min); } 6

例 用数组求 Fibonacci数列 前20个数 7 0 1 2 3 4 5 1 1 2

例 用数组求 Fibonacci数列 前20个数 7 0 1 2 3 4 5 1 1 2 3 5 f[0] f[1] f[2] f[3] f[4] f[5] ……. . . #include <stdio. h> main() { int i; int f[20]={1, 1}; for(i=2; i<20; i++) f[i]=f[i-2]+f[i-1]; for(i=0; i<20; i++) { if(i%5==0) printf("n"); printf("%12 d", f[i]); } } 19 f[19]

38 13 27 30 49 13 27 30 38 13 27 30 13 27

38 13 27 30 49 13 27 30 38 13 27 30 13 27 第 六 趟 第 七 趟 38 49 65 38 49 13 97 76 27 65 13 27 13 97 76 27 76 13 27 27 97 13 76 30 27 30 76 30 65 27 30 76 30 97 97 30 97 49 38 65 n=8 9 第 五 趟 13 13 30 65 65 第 二 趟 第 三 趟 第 四 趟 76 49 第 一 趟 初 始 关 键 字 49 38 30 49 30 38 27 49 27 38 13 13 38 例

#include <stdio. h> main() 输入n 个数给a[1] 到 a[n] { int a[11], i, j, t;

#include <stdio. h> main() 输入n 个数给a[1] 到 a[n] { int a[11], i, j, t; printf("Input 10 numbers: n"); for j=1 to n-1 for(i=1; i<11; i++) for i=1 to n-j scanf("%d", &a[i]); printf("n"); a[i]>a[i+1] 真 假 for(j=1; j<=9; j++) for(i=1; i<=10 -j; i++) a[i] a[i+1] if(a[i]>a[i+1]) 输出a[1] 到 a[n] {t=a[i]; a[i]=a[i+1]; a[i+1]=t; } printf("The sorted numbers: n"); for(i=1; i<11; i++) printf("%d ", a[i]); } 10

k 例 i=1 初始: [ 49 13 k k 38 65 97 76 13

k 例 i=1 初始: [ 49 13 k k 38 65 97 76 13 49 j j j k i=2 一趟: 13 j k [38 27 65 j 12 27 ] 97 76 49 38 27 ] j j 二趟: 13 27 [65 97 76 49 38 ] 三趟: 13 27 38 [97 76 49 65 ] 四趟: 13 27 38 49 [76 97 65 ] 五趟: 13 27 38 49 65 [97 76 ] 六趟: 13 27 38 49 65 76 [97 ]

输入n 个数给a[1] 到 a[n] for i=1 to n-1 k=i for j=i+1 to n 真

输入n 个数给a[1] 到 a[n] for i=1 to n-1 k=i for j=i+1 to n 真 a[j]<a[k] k=j i != k 真 a[i] a[k] 输出a[1] 到 a[n] 13 #include <stdio. h> main() { int a[11], i, j, k, x; printf("Input 10 numbers: n"); for(i=1; i<11; i++) scanf("%d", &a[i]); printf("n"); for(i=1; i<10; i++) 假{ k=i; for(j=i+1; j<=10; j++) if(a[j]<a[k]) k=j; 假 if(i!=k) { x=a[i]; a[i]=a[k]; a[k]=x; } } printf("The sorted numbers: n"); for(i=1; i<11; i++) printf("%d ", a[i]); }

#define N 6 数组定义:必须用常量表达式 main() { int a[N], b[‘B’-60], c[]={1, 2, 3, 4, 5,

#define N 6 数组定义:必须用常量表达式 main() { int a[N], b[‘B’-60], c[]={1, 2, 3, 4, 5, 6}, i; for(i=0; i<N; i++) scanf("%d%d", &a[i], &b[i]); for(i=0; i<N; i++) 数组元素引用 printf("%d ", a[i]); printf("n"); for(i=0; i<N; i++) printf("%d ", b[i]); printf("n"); for(i=0; i<N; i++) c[i]=a[i]+b[N-i-1]; for(i=0; i<N; i++) printf("%d ", c[i]); } 14

二维数组理解 二维数组a是由 3个元素组成 例 int a[3][4]; 2000 a[0][1] 2002 a[0][0] 1 3 2008 2010

二维数组理解 二维数组a是由 3个元素组成 例 int a[3][4]; 2000 a[0][1] 2002 a[0][0] 1 3 2008 2010 a[1][0] a[1][1] 9 11 2016 2018 a[2][0] a[2][1] 17 19 2004 a[0][2] 5 2012 a[1][2] 13 2020 a[2][2] 21 20006 a[0][3] 7 2014 a[1][3] 15 2022 a[2][3] 23 行名 0 1 2 a[0][0] a[0][1] 3 4 a[0][3] a[1][0] a[1][1] a[1][2] a[1][3] a[2][0] a[2][1] a[2][2] a[2][3] 5 6 7 8 9 每个元素a[i]由包含 4个元素 的一维数组组成 16 10 11 a[0][2] a[1] a[2]

程序举例 例 将二维数组行列元素互换,存到另一个数组中 #include <stdio. h> 1 4 1 2 3 a= main() b=

程序举例 例 将二维数组行列元素互换,存到另一个数组中 #include <stdio. h> 1 4 1 2 3 a= main() b= 2 5 4 5 6 { int a[2][3]={{1, 2, 3}, {4, 5, 6}}; 3 6 int b[3][2], i, j; printf("array a: n"); printf("array b: n"); for(i=0; i<=1; i++) for(i=0; i<=2; i++) { for(j=0; j<=2; j++) { for(j=0; j<=1; j++) { printf("%5 d", a[i][j]); printf("%5 d", b[i][j]); b[j][i]=a[i][j]; printf("n"); } } 18

例 求二维数组中最大 元素值及其行列号 max=a[0][0] for i=0 to 2 for j=0 to 3 真 a[i][j]>max

例 求二维数组中最大 元素值及其行列号 max=a[0][0] for i=0 to 2 for j=0 to 3 真 a[i][j]>max max=a[i][j] row=i colum=j 输出:max和row, colum 19 #include <stdio. h> main() { int a[3][4]={{1, 2, 3, 4}, {9, 8, 7, 6}, {-10, -5, 2}}; int i, j, row=0, colum=0, max; max=a[0][0]; for(i=0; i<=2; i++) 假 for(j=0; j<=3; j++) if(a[i][j]>max) { max=a[i][j]; row=i; colum=j; } printf("max=%d, row=%d, colum=%dn", max, row, colum); }

例 读入下表中值到数组,分别求各行、各列 及表中所有数之和 #include <stdio. h> 12 4 6 8 84 15 23 23

例 读入下表中值到数组,分别求各行、各列 及表中所有数之和 #include <stdio. h> 12 4 6 8 84 15 23 23 6 73 322 934 15 27 59 1731 2 5 17 24 37 39 35 111 12 for(i=0; i<5; i++) { for(j=0; j<4; j++) printf("%5 dt", x[i][j]); printf("n"); } } 20 main() { int x[5][4], i, j; for(i=0; i<4; i++) for(j=0; j<3; j++) scanf("%d", &x[i][j]); for(i=0; i<3; i++) x[4][i]=0; for(j=0; j<5; j++) x[j][3]=0; for(i=0; i<4; i++) for(j=0; j<3; j++) { x[i][3]+=x[i][j]; x[4][j]+=x[i][j]; x[4][3]+=x[i][j]; }

二维字符数组初始化 例 char diamond[][5]={{'. ', '*'}, {'. ', '*', '*'}, '. ', '. '

二维字符数组初始化 例 char diamond[][5]={{'. ', '*'}, {'. ', '*', '*'}, '. ', '. ' , '*'}, {'. ', '*', '*'}, {'. ', '*'}}; 例 char{'*', fruit[][7]={"Apple", "Orange", . * . diamond[1] fruit[0] A p * p . l e* * diamond[2] fruit[1] O r. a . n g. e * . diamond[3] fruit[2] G. diamond[4] fruit[3] P r * a . p e* e. a *r e a c diamond[0] . "Grape", "Pear", "Peach"}; fruit[4] P 22 h

例 输出一个字符串 #include <stdio. h> main() { char c[10]={'I', 'a', 'm', 'a', 'b', 'o',

例 输出一个字符串 #include <stdio. h> main() { char c[10]={'I', 'a', 'm', 'a', 'b', 'o', 'y'}; int i; for(i=0; i<10; i++) printf("%c", c[i]); printf("n"); } 23 0 1 2 3 4 5 6 7 8 9 I a m a b o y

例 字符串输入举例 运行情况: 输入:How are you? 输出:a=How b=are c=you? 输入:How are you? 输出:a=How #include

例 字符串输入举例 运行情况: 输入:How are you? 输出:a=How b=are c=you? 输入:How are you? 输出:a=How #include <stdio. h> main() { char a[15], b[5], c[5]; scanf("%s%s%s", a, b, c); printf("a=%snb=%snc=%sn", a, b, c); scanf("%s", a); printf("a=%sn", a); scanf中%s输入时, 遇空格 } 或回车结束 运行情况: 输入:How are you? 29 H o w a r e y o u ?

例 strcpy与strcat举例 0 T u 1 u r 2 r b 3 b o

例 strcpy与strcat举例 0 T u 1 u r 2 r b 3 b o 4 o 5 5 7 …. . . 8 9 ……. 24 6 C 7 9 + + 24 ……. 6 ……. 33 Turbo C++ 0 ……. #include <string. h> 1 #include <stdio. h> 2 void main() 3 { char destination[25]; 4 char blank[] = " ", c[]= "C++", 5 turbo[] = "Turbo"; 6 strcpy(destination, turbo); 7 8 strcat(destination, blank); 9 strcat(destination, c); printf("%sn", destination); } 24 T 8

#include <string. h> 例 strcmp与strlen举例 #include <stdio. h> main() { char str 1[] =

#include <string. h> 例 strcmp与strlen举例 #include <stdio. h> main() { char str 1[] = "Hello!", str 2[] = "How are you? ", str[2 int len 1, len 2, len 3; len 1=strlen(str 1); len 2=strlen(str 2); if(strcmp(str 1, str 2)>0) { strcpy(str, str 1); strcat(str, str 2); } else if (strcmp(str 1, str 2)<0) { strcpy(str, str 2); strcat(str, str 1); } else strcpy(str, str 1); How are you? Hello! len 3=strlen(str); Len 1=6, Len 2=12, Len 3=18 puts(str); printf("Len 1=%d, Len 2=%d, Len 3=%dn", len 1, len 2, le } 35

应用举例 #include <stdio. h> 例 输入一行字符,统 main() 计其中有多少个单词 { char string[81]; int i, num=0,

应用举例 #include <stdio. h> 例 输入一行字符,统 main() 计其中有多少个单词 { char string[81]; int i, num=0, word=0; 输入一字符串给 string char c; i=0 num=0 word=0 gets(string); for(i=0; (c=string[i])!=''; i++) 当((c=string[i])!=‘’) c=空格 if(c==' ') word=0; 真 假 else if(word==0) word==0 真 假 { word=1; num++; } word=0 word=1 printf("There are %d words num=num+1 in the linen", num); i=i+1 } 输出:num 36

例 有三个字符 串,找出其中最 大者 str[0] H o w � str[1] H e l l

例 有三个字符 串,找出其中最 大者 str[0] H o w str[1] H e l l o str[2] H i 38 g h #include <stdio. h> #include <string. h> main() { char string[20], str[3][20]; int i; for(i=0; i<3; i++) gets(str[i]); if(strcmp(str[0], str[1])>0) strcpy(string, str[0]); else strcpy(string, str[1]); if(strcmp(str[2], string)>0) strcpy(string, str[2]); printf("n. The largest string is: n%sn", string); }

例 有十个学生的成绩,求平均分 92 85 68 75 54 88 98 45 61 79 例 有三个学生四门课成绩

例 有十个学生的成绩,求平均分 92 85 68 75 54 88 98 45 61 79 例 有三个学生四门课成绩 数学 英语 化学 物理 a[0] 92 a[1] 85 92 85 68 75 a[2] 54 88 98 45 二维数组a[3] 61 79 81 40 a[4] #include <stdio. h> a[5] main() a[6] { int k , a[10]; a[7] for(k=0; k<10; k++) a[8] scanf("%d", &a[k]); a[9] for(k=0; k<10; k++) sum+=a[k]; printf("Average is %dn", sum/10); } 39 张三 李四 王二 68 75 54 88 98 45 61 79 a

例 char name[0]; float weight[10. 3]; int array[-100]; 例 char str[]="Hello"; char str[]={‘H’, ‘e’,

例 char name[0]; float weight[10. 3]; int array[-100]; 例 char str[]="Hello"; char str[]={‘H’, ‘e’, ‘l’, ‘o’}; 例 int a[10]; float i=3; a[i]=10; 0 1 h e 2 3 l l 2 o 3 l 5 4 4 l o 例 int a[5]; a={2, 4, 6, 8, 10}; 例 int a[][10]; float f[2][]={1. 2 , 2. 2}; 例 比较 int a[2][3]={{5, 6}, {7, 8}}; 与 int a[2][3]={5, 6, 7, 8}; 40 5 7 6 8 0 0 5 8 6 0 7 0

/*ch 6_12. c*/ #include <stdio. h> #define N 11 main() {int i, j, a[N][N];

/*ch 6_12. c*/ #include <stdio. h> #define N 11 main() {int i, j, a[N][N]; 例1: . 输出如下杨辉三角形 for(i=1; i<N; i++) 的前10行。 {a[i][i]=1; a[i][1]=1; } 1 for(i=3; i<N; i++) 1 1 for(j=2; j<=i-1; j++) 1 2 1 a[i][j]=a[i-1][j-1]+a[i-1][j]; 1 3 3 1 for(i=1; i<N; i++) 1 4 6 4 1 {for(j=1; j<=i; j++) printf("%6 d", a[i][j]); printf("n"); } } 41

例2. 输入 10个学生的学号和三科成绩(整数), 统计并输出 3门课埕的总分最高的学生的 学号和总分。 /*ch 6_12. c*/ #include <stdio. h> main() {

例2. 输入 10个学生的学号和三科成绩(整数), 统计并输出 3门课埕的总分最高的学生的 学号和总分。 /*ch 6_12. c*/ #include <stdio. h> main() { int i, max_i, s[10][5]; for(i=0; i<10; i++) { printf("input student No and 3 scoren"); scanf("%d%d", &s[i][0], &s[i][1], &s[i][2], &s[i][3]); s[i][4]= s[i][1]+s[i][2]+s[i][3]; } max=s[0][4]; max_i=0; for(i=1; i<10; i++) if(max<s[i][4]) { max=s[i][4]; max_i=i; } printf("student No =%6 d total=%dn", s[max_i][0], s[max_i][4]); } 42

例3: 求 2~ 2000以 内的素 数。 43 /*ch 6_12. c*/ #include <stdio. h> /"modified

例3: 求 2~ 2000以 内的素 数。 43 /*ch 6_12. c*/ #include <stdio. h> /"modified program to generate a table of prime numbers"/ void main() { int i, p, is_prime, prime[1000], prime_index=2; prime[0]=2; prime[1]=3; for(p=5; p<2000; p=p+2) { is_prime=1; for(i=1; is_prime&&p/prime[i]>=prime[i]; i++) if(p%prime[i]==0) is_prime=0; if(is_prime) { prime[prime_index]=p; prime_index++; } } for(i=0; i<prime_index; i++) { if(i%5==0)printf("n"); printf("%10 d", prime[i]); } }

例: 输入一个字 符串存入数组 a, 将其复制到 数组b, 然后将 数组b中的所有 小写字母改为 大写字母, 再将 a中字符串连接 到b。比较并输 出a,

例: 输入一个字 符串存入数组 a, 将其复制到 数组b, 然后将 数组b中的所有 小写字母改为 大写字母, 再将 a中字符串连接 到b。比较并输 出a, b中字符串 大小, 统计并输 出a, b中字符串 的长度, 最后输 出a, b中的字符 串。 45 /*ch 6_12. c*/ #include <stdio. h> #include "string. h" main() { char a[40], b[80]; gets(a); strcpy(b, a); strupr(b); strcat(b, a); if(strcmp(a, b)<0) printf("a<bn"); else if(strcmp(a, b)>0) printf("a>bn"); else printf("a=bn"); printf("lenth of a(%s)=%dn", a, strlen(a)); printf("lenth of b(%s)=%dn", b, strlen(b)); puts(a); puts(b); }

例: . 输入两个 字符串a和b, 判别字符串b 是否是字符串 a的子串, 是则 输出b串在a串 中的开始位置 ; 否则输出-1。 例如: 串

例: . 输入两个 字符串a和b, 判别字符串b 是否是字符串 a的子串, 是则 输出b串在a串 中的开始位置 ; 否则输出-1。 例如: 串 a="ABCDEF", 若串b="CD", 则输出 3; 若串 b="CE", 则输 出-1。 46 /*ch 6_12. c*/ #include <stdio. h> #include "stdio. h" #include "string. h" main() {char a[80], b[40]; int na, nb, i, j, flag; gets(a); gets(b); na= strlen(a); nb= strlen(b); flag=-1; for(i=0; na-i>=nb; i++) {flag=-2; for(j=0; j<nb; j++) if(a[i+j]!=b[j]) {flag=-1; break; } if(flag==-2) {flag=i+1; break; } } printf("%sn%dn", a, b, flag); }

例: 编一程序, 将两个字符串连接起来, 不用strcat函数。 /*ch 6_12. c*/ #include <stdio. h> main() { char s

例: 编一程序, 将两个字符串连接起来, 不用strcat函数。 /*ch 6_12. c*/ #include <stdio. h> main() { char s 1[8], s 2[40]; int i=0, , j=0; printf("n. Input string 1: "); scanf("%s", s 1); printf("n. Input string 2: "); scanf("%s", s 2); while(s 1[i]!=‘’) i++; while(s 2[j]!=‘’) s 1[i++]=s 2[j++]; s 1[i]=‘’; printf("The new string is : n", s 1); } 47

例: 编一程序, 将串s 2中的内容全部拷贝至s 1中, 不用strcpy函数。拷贝时 , ‘�’也要拷贝过去, �’后面的字符不拷贝过去。 /*ch 6_12. c*/ #include <stdio.

例: 编一程序, 将串s 2中的内容全部拷贝至s 1中, 不用strcpy函数。拷贝时 , ‘’也要拷贝过去, ’后面的字符不拷贝过去。 /*ch 6_12. c*/ #include <stdio. h> #include<string. h> main() { char s 1[80], s 2[80]; int i; printf("n. Input string s 2: "); scanf("%s", s 2); for(i=0; i<=strlen(s 2); i++) s 1[i]=s 2[i]; printf("The string s 1 is : n", s 1); } 48

作业: (P 141页) 7. 5 7. 10 7. 11 49

作业: (P 141页) 7. 5 7. 10 7. 11 49