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
§ 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)
can not use ( )as int a[6] ; a[0]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
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
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
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]);
}
}
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
9727
9730
97
13
76
76
7627
30
13
6527
6530
65
13
13
49
4930
4927
3827
3830
38
STEPS:
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 <stdio.h>
main()
{ int a[9],i,j,t;
printf("Input 8 numbers:\n");
for(i=1;i<9;i++)
scanf("%d",&a[i]);
printf("\n");
for(j=1;j<=7;j++)
for(i=1;i<=8-j;i++)
if(a[i]>a[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
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
kk
k
j j j j j
j j j j j
k
Exp4,sort 7 numbers with selected-sort
STEPS:
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]<a[k]T F
k=j
Output a[1] … a[n]
k=i
a[i]?a[k]
i != kT F
#include <stdio.h>
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
§ 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 columnnumber=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]
The C Programming Language Chapter 7 Arrays
Specifications,
as int a[3][4];
a[0][0] a[0][1] a[0][2] 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]
each elements a[i] is also
an array who has 4 elements
a is really a one-dimensional
array who has 3 elements;
a[0]
a[1]
a[2]
row
0
1
4
5
2
3
a[0][1]
a[0][2]
a[0][3]
a[1][0]
a[1][1]
a[0][0]
a[1][3]
a[2][0]
a[2][1]
a[2][2]
a[2][3]
a[1][2]6
7
10
11
8
9
a[0]
a[1]
a[2]
The C Programming Language Chapter 7 Arrays
Reference:
as a[i][j] ( i,0~N-1,j,0~M-1)
Initialization
as int a[2][3]={{1,2,3},{4,5,6}};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 6
fully initialized
as int a[2][3]={1,2,3,4,5,6};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 6
fully initialized
as int a[][3]={1,2,3,4,5};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 3 4 5 0
the rows size can be omitted
as int a[2][3]={1,2,4};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
1 2 4 0 0 0
fewer initializers than elements
as int a[][3]={{1},{4,5}};
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
0 0 0
the row size can be omitted
int a {1,2},{4}};
0 4
f r initiali rs th le ts
The C Programming Language Chapter 7 Arrays
Examples
例 将二维数组行列元素互换,存到另一个数组中
a= 1 2 34 5 6 b= 1 42 5
3 6
#include <stdio.h>
main()
{ int a[2][3]={{1,2,3},{4,5,6}};
int b[3][2],i,j;
printf("array a:\n");
for(i=0;i<=1;i++)
{ for(j=0;j<=2;j++)
{ printf("%5d",a[i][j]);
b[j][i]=a[i][j];
}
printf("\n");
}
printf("array b:\n");
for(i=0;i<=2;i++)
{ for(j=0;j<=1;j++)
printf("%5d",b[i][j]);
printf("\n");
}
}
The C Programming Language Chapter 7 Arrays
例 求二维数组中最大元素值及其行列号
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
#include <stdio.h>
main()
{ int a[3][4]={{1,2,3,4},
{9,8,7,6},
{-10,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=%d\n",max,row,colum);
}
The C Programming Language Chapter 7 Arrays
例 读入下表中值到数组,分别求各行、各列及表中所有数之和
12 4 6
15 7 9
8 23 3
2 5 17
12 4 6 22
15 7 9 31
8 23 3 34
2 5 17 24
37 39 35 111
#include <stdio.h>
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];
}
for(i=0;i<5;i++)
{ for(j=0;j<4;j++)
printf("%5d\t",x[i][j]);
printf("\n");
}
}
The C Programming Language Chapter 7 Arrays
§ 7.3 Character Arrays
Declaration:
Initialization
Initialized by each character;
Initialized by string constant.
as char c[10],ch[3][4];
as char ch[6]={“Hello”};
char ch[6]=“Hello”;
char ch[]=“Hello”;
initialized by string constant
ch[0]
H e l l o
ch[1] ch[2] ch[3] ch[4]
\0
ch[5]
as char ch[5]=“Boy”;
ch[0]
B o y \0 \0
initialized by string constant
ch[1] ch[2] ch[3] ch[4]
char ch[5]={?B?,?o?,?y?};
ch[0]
B o y \0 \0
initialized by each character
ch[1] ch[2] ch[3] ch[4]
as char ch[5]={?H?,?e?,?l?,?l?,?o?};
ch[0]
H e l l o
initialized by each character
ch[1] ch[2] ch[3] ch[4]
The C Programming Language Chapter 7 Arrays
as char diamond[][5]={{'.','.','*'},{'.','*','.','*'},
{'*','.','.','.','*'},{'.','*','.','*'},{'.','.','*'}};
initializations of two-
dimensional
character arrays
.,* \0 \0
,*,* \0
*,,,*
,*,* \0
.,* \0 \0
diamond[0]
diamond[1]
diamond[2]
diamond[3]
diamond[4]
as char fruit[][7]={“Apple”,”Orange”,
”Grape”,”Pear”,”Peach”};
initializations of two-dimensional
character arrays
fruit[0]
fruit[1]
fruit[2]
fruit[3]
fruit[4]
A p p l e \0 \0
O r a n g e \0
G r a p e \0 \0
P e a r \0 \0 \0
P e a c h \0 \0
The C Programming Language Chapter 7 Arrays
Exp1,output a string
#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");
}
I
a
m
a
b
o
y
0
1
2
3
4
5
6
7
8
9
The C Programming Language Chapter 7 Arrays
Strings,
There are no string variables,strings are arrays of type
char ;
A string is terminated by the end-of-string sentinel \0
(or null character)
as? hello” has 5 characters,stored in 6 bytes
char a[]=“hello”
h e l l o \0
The C Programming Language Chapter 7 Arrays
Input and output of strings
character I/O,%c
string I/O,%s
as use %c
main()
{ char str[5];
int i;
for(i=0;i<5;i++)
scanf(“%c”,&str[i]);
for(i=0;i<5;i++)
printf(“%c”,str[i]);
}
as use %s
main()
{ char str[5];
scanf(“%s”,str);
printf(“%s”,str);
}
uses array name,needn?t &
the number of input
characters
< the size of the array
terminated by blank or
newline
adds?\0? automatically
uses array name
terminated by?\0?
The C Programming Language Chapter 7 Arrays
as main( )
{ char a[5]={?H?,?e?,?l?,?l?,?o?};
printf(“%s”,a);
}
as main( )
{ char a[ ]=“Hello”;
printf(“%s”,a);
}
output,Hello#-=*
h e l l o
0 2 31 4
output,Hello
as main()
{
char a[]={'h','e','l','\0','l','o','\0'};
printf("%s",a);
}
output,hel
h e l \0 l o \0
output is terminated by
\0?
The C Programming Language Chapter 7 Arrays
main()
{
int i;
char a[5];
scanf("%s",a);
for(i=0;i<5;i++)
printf("%d,",a[i]);
}
running:
( 1) input,hel,output,hel ;
( 2) input,hell,output,hell ;
( 3) input,hello,output,hello#-=* ;
h e l \0
h e l l \0
h e l l o
the number of input characters
< the size of the array
The C Programming Language Chapter 7 Arrays
Exp,string input
H o w \0
a r e \0
y o u? \0
#include <stdio.h>
main()
{ char a[15],b[5],c[5];
scanf("%s%s%s",a,b,c);
printf("a=%s\nb=%s\nc=%s\n",a,b,c);
scanf("%s",a);
printf("a=%s\n",a);
}
running:
input,How are you?
output,a=How
b=are
c=you?
input,How are you?
output,a=How
useing %s in scanf,
input
is terminated by
blank or newline
The C Programming Language Chapter 7 Arrays
as If you want to store,This is a string.” into array s,
which input is wrong as follows:
( A) scanf(“%20s”,s);
( B) for(k=0;k<17;k++)
s[k]=getchar();
( C) while((c=getchar())!=?\n?)
s[k++]=c;
The C Programming Language Chapter 7 Arrays
String-handling functions in the standard library
puts(str) stdio.h
puts writes the string and a newline to stdout;
chararter array must be terminated by?\0?
gets(str) stdio.h
gets reads the next input line into the array ;
it replaces the terminating newline with?\0?;
the number of input characters < the size of the array
as #include <stdio.h>
main( )
{ char string[80];
printf(“Input a string:”);
gets(string);
puts(string);
}
input,How are you?
output,How are you?
The C Programming Language Chapter 7 Arrays
strcat(str1,str2) string.h
concatenate str2 to end of str1;
put the result in str1,return str1;
ensure that str1 has enough space to hold the result ;
strcpy(str1,str2) string.h
copy str2 to str1,including?\0?,return str1;
ensure that str1 has enough space to hold the result;
can not use assignment statement.
as char str1[20],str2[20];
str1={“Hello!”}; (?)
str2=str1; (?)
The C Programming Language Chapter 7 Arrays
Exp,strcpy and strcat
#include <string.h>
#include <stdio.h>
main()
{ char destination[25];
char blank[] = " ",c[]= "C++",
turbo[] = "Turbo";
strcpy(destination,turbo);
strcat(destination,blank);
strcat(destination,c);
printf("%s\n",destination);
}
Turbo C++
T
r
b
o
C
+
+
0
1
2
3
4
5
6
7
8
9
u
\0
24
…….
\0
…….
…….\0
…….
…...
The C Programming Language Chapter 7 Arrays
strcmp(str1,str2) string.h
compare str1 to str2 by ASCII value of each character
(left-to-right);
if str1 < str2,return <0 ;
if str1 == str2,return =0 ;
if str1 > str2,return >0 ;
can not use == to compare two strings.
strlen(str) string.h
return the length of str ;
the length is the number of characters in the string,
not counting ‘ \0’,as the results of strlen(s) as follows:
( 1) char s[10]={?A?,?\0?,?B?,?C?,?\0?,?D?};
( 2) char s[ ]=“\t\b\\\0will\n”;
( 3) char s[ ]=“\x69\082\n”; results,1 3 1
The C Programming Language Chapter 7 Arrays
#include <string.h>
#include <stdio.h>
main()
{ char str1[] =,Hello!",str2[] =,How are you?”,str[20];
int len1,len2,len3;
len1=strlen(str1); len2=strlen(str2);
if(strcmp(str1,str2)>0)
{ strcpy(str,str1); strcat(str,str2); }
else if (strcmp(str1,str2)<0)
{ strcpy(str,str2); strcat(str,str1); }
else strcpy(str,str1);
len3=strlen(str);
puts(str);
printf(”Len1=%d,Len2=%d,Len3=%d\n”,len1,len2,len3);
}
How are you?Hello!
Len1=6,Len2=12,Len3=18
Exp,strcmp and strlen
The C Programming Language Chapter 7 Arrays
Y
0
Y
1
1
Y
1
N
0
1
N
0
Y
1
2
N
1
N
1
2
Y
1
N
0
2
N
0
Y
1
3
Y
1
N
0
3
N
0
Y
1
4
N
1
N
1
4
N
1
N
1
4
N
1
N
1
4
Exp,input a line of characters,count the number of words
present character
==blank
Y
N
new word doesn’t appear,word=0,
num doesn’t change
if preceding word is blank (word==0),new
word appears,word=1,num++
if preceding word isn’t blank(word==1),new
word doesn’t appear,num doesn’t change
as input,I?am?a?boy.
present char
blank?
former value(word)
new word appears?
new value(word)
value of num
I a m a b o y,
The C Programming Language Chapter 7 Arrays
input a string
i=0 num=0 word=0
while ((c=string[i])!=?\0?)
c==T
T
F
F
word=0 word=1
num=num+1
i=i+1
output,num
word==0
#include <stdio.h>
main()
{ char string[81];
int i,num=0,word=0;
char c;
gets(string);
for(i=0;(c=string[i])!='\0';i++)
if(c==' ') word=0;
else if(word==0)
{ word=1; num++; }
printf("There are %d words \
in the line\n",num);
}
The C Programming Language Chapter 7 Arrays
H o w \0
H e l l o \0
H i g h \0
str[0]
str[1]
str[2]
Str[3][20]
Exp,here are three strings,find the max.
(using two-dimensional character array)
#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("\nThe largest string \
is:\n%s\n",string);
}
The C Programming Language Chapter 7 Arrays
as compare int a[2][3]={{5,6},{7,8}};
and int a[2][3]={5,6,7,8};
5 6 0
7 8 0
5 6 7
8 0 0
as int a[][10];
float f[2][]={1.2,2.2};
as int a[5];
a={2,4,6,8,10};
as int a[10];
float i=3;
a[i]=10;
as char name[0];
float weight[10.3];
int array[-100];
as char str[]=“Hello”;
char str[]={?H?,?e?,?l?,?l?,?o?};
h e l l o
0 2 31 4
h e l l o \0
0 2 31 4 5
NOTICE: