The c Programming language Chapter 7 Arrays Chapter 7 Arrays v derived type v a set of homogeneous elements v can be thought of as a simple variable with an index, or subscript, added u One-dimensional Arrays a Two-dimensional Arrays 口 Character Arrays
The C Programming Language Chapter 7 Arrays Chapter 7 Arrays ✓ derived type; ✓ a set of homogeneous elements; ✓ can be thought of as a simple variable with an index, or subscript,added. ❑ One-dimensional Arrays ❑ Two-dimensional Arrays ❑ Character Arrays
The c Programming language Chapter 7 Arrays 57.1 One-dimensional Arrays ☆ Declaration: specifies the number of elements in the array type array name [constant integral expression] subscript operator as int 1=15 int data 1 /xthe expression can not be variable tny can make reference to eaer elements one by one, > Array element:asa付(i as as int a 101 printf(" %,, a);(×) for(=0;j<10;j++) printf( %odt, aLD
The C Programming Language Chapter 7 Arrays § 7.1 One-dimensional Arrays ❖ Declaration: type array name[constant integral expression]; valid identifier specifies the number of elements in the array; [ ] :subscript operator precedence:(1) as int a[6] ; 0can not a[use 0] ( ) 1 4 5 a[1] a[2] a[3] a[4] a[5] 2 3 a ❖ Reference: ➢ Arrays must be declared before use; ➢ Only can make reference to each elements one by one; ➢ Array element : as a[i] ( i: 0~N-1) as int i=15; int data[i]; //the expression can not be variable as int data[5]; data[5]=10; //out of range as int a[10]; printf(“%d”,a); () => for(j=0; j<10; j++) printf(“%d\t”,a[j]); ()
The c Programming language Chapter 7 Arrays ☆ Initialization as int al!51={1,2,34.5} >:a[0}1;a[1]2;a[2}=3;a[3]=4;a4]=5 >S pecifications Without initialization, array elements are random numbers When a list of initializers is shorter than the number of array elements the remaining elements are initialized to zero inta[5}={6,2,3} a0=6;a[l]=2;a[2]}=3;a[3]=0;a[4]=0 as inta[31=62351 ×) as int al={1,2,3,4,5,6}; // the size of the array is 6
The C Programming Language Chapter 7 Arrays ❖ Initialization ➢ Specifications: ⚫Without initialization, array elements are random numbers. ⚫If an array is declared without a size and is initialized to a series of values ,it is implicitly given the size of the number of initializers. as int a[5]={6,2,3}; =>: a[0]=6; a[1]=2;a[2]=3; a[3]=0; a[4]=0; as int a[3]={6,2,3,5,1}; () ⚫When a list of initializers is shorter than the number of array elements ,the remaining elements are initialized to zero. as int a[]={1,2,3,4,5,6}; // the size of the array is 6 ➢as int a[5] ={1,2,3,4,5}; =>:a[0]=1; a[1]=2; a[2]=3; a[3]=4; a[4]=5;
The C Programming language Chapter 7 Arrays ☆ Examples #include xi] min=x[i] printf("Maximum value is %d n", max printf("Minimum value is %/odn",min)
The C Programming Language Chapter 7 Arrays ❖ Examples Exp1: reads 10 integers and stores in array, find the max , min STEPS: 1. Input :use for to read 10 integers; 2. Process : (a) let max=min= x[0] (b) Compare x[i] with max,min (loops) if maxx[i], let min=x[i] 3. Output :max和min #include #define SIZE 10 main() { int x[SIZE],i,max,min; printf("Enter 10 integers:\n"); for(i=0;ix[i]) min=x[i]; } printf("Maximum value is %d\n",max); printf("Minimum value is %d\n",min); }
The c Programming language Chapter 7 Arrays Exp2: the first 20 numbers of Fibonacci Fl=l (n=1) F2=1 (n=2) #include <stdio. h Fn=Fn1+Fn2(m≥3 maint i int i 0 int20]={1,1} for(i=2;i<20;i++) 2 印i=印i-2]+fi-1] for(i=0;1<20;i++) 235 f3] f14 f if(1%5==0) printf("n") f5] printf( %12d",fD 19m1919
The C Programming Language Chapter 7 Arrays Exp2:the first 20 numbers of Fibonacci f[0] f[1] f[2] f[3] f[4] f[5] f[19] ……... 1 1 f[19] 0 1 4 5 2 3 19 2 3 5 #include 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("%12d",f[i]); } }
The c Programming language Chapter 7 Arrays Exp3: sort 8 numbers with bubble-sort STEPS 38383838131313 n=8 49494913272727 65651327303030 76132730 383838 1327 304949 4949 273065 65 56565 3076 7676767676 97 97 9797979797 234567
The C Programming Language Chapter 7 Arrays Exp3: sort 8 numbers with bubble-sort 38 49 65 76 13 27 30 97 2 38 49 65 13 27 30 76 97 3 38 49 13 27 30 65 76 97 4 38 13 27 30 49 65 76 97 5 13 27 30 38 49 65 76 97 6 13 27 30 38 49 65 76 97 7 49 38 65 97 76 13 27 30 1 n=8 38 49 76 9713 972 9730 97 13 76 76 7627 30 13 6527 6530 65 13 13 49 4930 4927 3827 380 38 STEPS: 30 27
The c Programming language Chapter 7 Arrays #include main Input n numbers to a[1]. a[nl( int a[g], ij, t, for j=l to n-1 printf("Input 8 numbers: n") for(i=1;i<9;i++) for i=1 to n nf("9d",&a[]); lipai+l printf("n") F for(=1j<=7j++) alai+1 for(i=1;i<=8-j;i++) ifal]ai+ID Output al. an it=al]; ai=ai+ll; ai+1] printf("The sorted numbers: n") for(i=1;i<9;i++) printf(" %d",aiD)
The C Programming Language Chapter 7 Arrays Input n numbers to a[1]… a[n] for j=1 to n-1 for i=1 to n-j a[i]>a[i+1] T F a[i]a[i+1] Output a[1] … a[n] #include main() { int a[9],i,j,t; printf("Input 8 numbers:\n"); for(i=1;ia[i+1]) {t=a[i]; a[i]=a[i+1]; a[i+1]=t;} printf("The sorted numbers:\n"); for(i=1;i<9;i++) printf("%d ",a[i]); }
The c Programming language Chapter 7 Arrays Exp4: sort 7 numbers with selected-sort kk STEPS: 1: 13386597764927 k 2 13276597764938 13276597764938 3456 132738197764965 132738491769765 132738496519776 13273849657697
The C Programming Language Chapter 7 Arrays 1: [ 49 38 65 97 76 13 27 ] j 13 49 2: 13 [38 65 97 76 49 27 ] 27 38 3: 13 27 [65 97 76 49 38 ] 4: 13 27 38 [97 76 49 65 ] 5: 13 27 38 49 [76 97 65 ] 6: 13 27 38 49 65 [97 76 ] 13 27 38 49 65 76 97 k k k k j j j j j j j j j j k Exp4: sort 7 numbers with selected-sort STEPS:
The c Programming language #include <stdio. h maino Input n numbers a[1]. a[n] i int a[8,i,j,k,x printf("Input 7 numbers: In") for j=1 to n-1 for(i=1;i<8;i++) scanf("%d", &ai]) printf("n") for j=i+l to n for(i=1;i<7;i++) au]sakk ik-=i for(=i+1j<=7j++) if(al]a[k]= T i=k if(il=k alisa k (]; al=ak]; a[k=x; Output a[l]. a[n] printf("The sorted numbers: In") for(i=1;i<8;i++) printf(" %d"aiD)
The C Programming Language Chapter 7 Arrays Input n numbers a[1]…a[n] for i=1 to n-1 for j=i+1 to n a[j] main() { int a[8],i,j,k,x; printf("Input 7 numbers:\n"); for(i=1;i<8;i++) scanf("%d",&a[i]); printf("\n"); for(i=1;i<7;i++) { k=i; for(j=i+1;j<=7;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<8;i++) printf("%d ",a[i]); }
The c Programming language Chapter 7 Arrays 87.2 Two-dimensional Arrays ☆ Declaration number=row*col type array name [expr]expr 今 Storms Elem int a[3 14] float b[2][5] subscript or co ssed in Int a3, 4 storage oruc int a[3 ][2 a0J[0 aol a[O][0]a0][1] 234 a a[2]01 a[2][0]a[2][1 a[2][
The C Programming Language Chapter 7 Arrays §7.2 Two-dimensional Arrays ❖ Declaration: type array name[expr][expr] ❖ Storage order of elements: ⚫Elements are stored by rows,so the rightmost subscript, or column, varies fastest as elements are accessed in storage order. as int a[3][4]; float b[2][5]; int a[3,4]; () row column number=row*col int a[3][2] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] 0 1 4 5 2 3 a[0][0] a[0][0] a[0][1] a[1][0] a[1][1] a[2][0] a[2][1]