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 <stdio h Expl: reads 10 integers#define SIZe 10 main i int x[],i, max, min STEPS printf("Enter 10 integers: n") 1.Input: use for to for( i=0 i<SIZE; i++) 2. Process i printf("%/od: ,i+1) (a)let max-min scanf("%d", &x[) (b)Compare x[il if max x[i], max-min=xIO if minx[], for(i=1; K<SIZE; i++) Output: maxIm if(max<x[i]) max-x[i] if(min>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 max<x[i], let max=x[i] if min>x[i], let min=x[i] 3. Output :max和min #include <stdio.h> #define SIZE 10 main() { int x[SIZE],i,max,min; printf("Enter 10 integers:\n"); for(i=0;i<SIZE;i++) { printf("%d:",i+1); scanf("%d",&x[i]); } max=min=x[0]; for(i=1;i<SIZE;i++) { if(max<x[i]) max=x[i]; if(min>x[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 <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("%12d",f[i]); } }