Introduction To C Programming: Array and S T R I ngs/0
Introduction To C Programming: Array and S T R I ngs/0
Programming Lecture 6:
Ar r ay a n d S t r i n g s \0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
1 / 50
Outline
1 Arrays
1D Array
2D Array
2 Strings
Introduction to C Programming 2 / 50
Opening Discussion (1)
• Given we have following problem
• We have 10 students in the class
• We want to get average/sum/max/min score of their math course
• We also want to rank the scores
• Based on what we learned
• We should keep 10 variables of the same type
• How about we have 100 students??
1 #i n c l u d e < s t d i o . h>
2 v o i d main ( )
3 {
4 f l o a t x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 ;
5 f l o a t sum = 0 , avg = 0 ;
6 s c a n f ( ”%f ” , &x1 ) ;
7 sum += x1 ;
8 s c a n f ( ”%f ” , &x2 ) ;
9 sum += x2 ;
10 ...
11 avg = sum / 1 0 ;
12 }
Introduction to C Programming 3 / 50
Opening Discussion (2)
1 #i n c l u d e < s t d i o . h>
2 v o i d main ( )
3 {
4 f l o a t x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , x9 , x10 ;
5 f l o a t sum = 0 , avg = 0 ;
6 s c a n f ( ”%f ” , &x1 ) ;
7 sum += x1 ;
8 s c a n f ( ”%f ” , &x2 ) ;
9 sum += x2 ;
10 ...
11 avg = sum / 1 0 ;
12 }
Introduction to C Programming 4 / 50
Outline
1 Arrays
1D Array
2D Array
2 Strings
Introduction to C Programming 5 / 50
1D Array: declaration (1)
type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0
i n t a [ 0 ] ; // i t i s grammar OK, b u t m e a n i n g l e s s
Introduction to C Programming 6 / 50
1D Array: declaration (2)
type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0
1 i n t main ( ) 1 i n t main ( )
1 i n t main ( )
2 { 2 {
2 {
3 const i n t N = 40; 3 i n t N = 40;
3 float x [40];
4 f l o a t x [N ] ; 4 f l o a t x [N ] ;
4 ....
5 .... 5 ....
5 return 0;
6 return 0; 6 return 0;
6 }
7 } 7 }
Introduction to C Programming 7 / 50
1D Array: declaration (3)
type arrayName[size];
• type could be any type defined in C, e.g. int, float,...
• “arrayName” should be unique
• It is actually a variable/constant, so rules to other variables/constants
apply too
• “size” should be an integer or an integer constant greater than 0
1 #d e f i n e N 40 1 i n t main ( ) 1 #d e f i n e N 40
2 i n t main ( ) 2 { 2 i n t main ( )
3 { 3 const i n t N = 40; 3 {
4 f l o a t x [N ] ; 4 f l o a t x [N ] ; 4 f l o a t x [ 3 ∗N ] ;
5 .... 5 .... 5 ....
6 return 0; 6 return 0; 6 return 0;
7 } 7 } 7 }
Introduction to C Programming 8 / 50
1D Array: visit array element (1)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 float x [40];
5 x [0] = 5.0;
6 x [2] = 3.1;
7 p r i n t f ( ” x [ 0 ] = %f ” , x [ 0 ] ) ;
8 return 0;
9 }
Introduction to C Programming 9 / 50
1D Array: visit array element (2)
1 i n t main ( )
2 {
3 float x [40];
4 int i = 0;
5 f o r ( i = 0 ; i < 4 0 ; i ++)
6 {
7 p r i n t f ( ” I n p u t %d : ” , i ) ;
8 /∗−−be c a r e f u l below−−∗/
9 s c a n f ( ”%f ” , &(x [ i ] ) ) ;
10 }
11 return 0;
12 }
Introduction to C Programming 10 / 50
1D Array: how array looks like (1)
4 bytes 1 byte
10127 x[0] 3.1 10127 ch[0] c
10131 x[1] 4.2 10128 ch[1] b
10135 x[2] 5.0 10129 ch[2] e
… ... ... … ... ...
10279 x[38] 3.3 10165 ch[38] f
10283 x[39] 4.2 10166 ch[39] x
Introduction to C Programming 11 / 50
1D Array: how array looks like (2)
1 #i n c l u d e < s t d i o . h>
4 bytes 2 i n t main ( )
3 {
10127 x[0] 3.1 4 int a [10] , b = 3;
10131 x[1] 4.2 5 char c [ 1 0 ] ;
6 p r i n t f ( ” a : %d\n” , s i z e o f ( a ) ) ;
10135 x[2] 5.0 7 p r i n t f ( ”b : %d\n” , s i z e o f ( b ) ) ;
… ... ... 8 p r i n t f ( ” c : %d\n” , s i z e o f ( c ) ) ;
return 0;
10279 x[38] 3.3 9
10 }
10283 x[39] 4.2
Introduction to C Programming 12 / 50
1D Array: how array looks like (3)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 { [Output]
4 int a [10] , b = 3;
5 char c [ 1 0 ] ; 1 a : 40
6 p r i n t f ( ” a : %d\n” , s i z e o f ( a ) ) ; 2 b: 4
7 p r i n t f ( ”b : %d\n” , s i z e o f ( b ) ) ; 3 c : 10
8 p r i n t f ( ” c : %d\n” , s i z e o f ( c ) ) ;
9 return 0;
10 }
Introduction to C Programming 13 / 50
1D Array: initialization (1)
Introduction to C Programming 14 / 50
1D Array: initialization (2)
1 #i n c l u d e < s t d i o . h>
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
2 i n t main ( )
3 {
3 {
4 i n t a [ 1 0 ] = {3 , 2 , 5 ,
4 i n t a [ ] = {3 , 2 , 5 , 1};
1};
5 int i = 0;
5 int i = 0;
6 f o r ( ; i < 4 ; i ++)
6 f o r ( ; i < 1 0 ; i ++)
7 p r i n t f ( ”%d ” , a [ i ] ) ;
7 p r i n t f ( ”%d ” , a [ i ] ) ;
8 return 0;
8 return 0;
9 }
9 }
Introduction to C Programming 15 / 50
1D Array: initialization (3)
1 #i n c l u d e < s t d i o . h>
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
2 i n t main ( )
3 {
3 {
4 int a [10];
4 i n t a = {3 , 2 , 5 , 1};
5 a [ 1 0 ] = {3 , 2 , 5 , 1};
5 int i = 0;
6 int i = 0;
6 f o r ( ; i < 4 ; i ++)
7 f o r ( ; i < 1 0 ; i ++)
7 p r i n t f ( ”%d ” , a [ i ] ) ;
8 p r i n t f ( ”%d ” , a [ i ] ) ;
8 return 0;
9 return 0;
9 }
10 }
Introduction to C Programming 16 / 50
1D Array Example-1 (1)
Introduction to C Programming 17 / 50
1D Array Example-1 (2)
Introduction to C Programming 18 / 50
1D Array Example-1 (3)
Introduction to C Programming 19 / 50
1D Array Example-1 (4)
1 For i from 0 to N2 do
2 Exchange a[i] with a[N-i-1]
3 End-for
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t a [ 1 0 ] ={3 ,21 ,5 ,8 ,5 ,11 ,22 ,14 ,9 ,51};
5 int t = 0 , i = 0;
6 f o r ( ; i < 5 ; i ++){
7 t = a[ i ];
8 a [ i ] = a [10 − i − 1 ] ;
9 a [10 − i −1] = t ;
10 }
11 f o r ( i = 0 ; i < 1 0 ; i ++){
12 p r i n t f ( ”%d ” , a [ i ] ) ;
13 }
14 return 0;
15 }
Introduction to C Programming 20 / 50
Dynamic Array Example-2 (1)
Introduction to C Programming 21 / 50
Dynamic Array Example-2 (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 int n = 0 , i = 0;
5 s c a n f ( ”%d” , &n ) ;
6 int a[n ];
7 f o r ( i = 0 ; i < n ; i ++)
8 {
9 s c a n f ( ”%d” , &a [ i ] ) ;
10 }
11 p r i n t f ( ”%d\n” , s i z e o f ( a ) ) ;
12 f o r ( i = 0 ; i < n ; i ++)
13 {
14 p r i n t f ( ” a[%d ] = %d\n” , i , a [ i ] ) ;
15 }
16 return 0;
17 }
Introduction to C Programming 22 / 50
1D Array Example-3 (1)
Introduction to C Programming 23 / 50
1D Array Example-3 (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( ) {
3 i n t a [ 7 ] = {3 ,14 ,15 ,18 ,22 ,35};
4 int b = 7 , i = 0 , j = 0;
5 s c a n f ( ”%d” , &b ) ;
6 f o r ( i = 0 ; i < 6 ; i ++){
7 i f (b < a [ i ]) {
8 break ;
9 }
10 }
11 f o r ( j = 6 ; j > i ; j −−){
12 a [ j ] = a [ j −1];
13 }
14 a[ j ] = b;
15 f o r ( i = 0 ; i < 7 ; i ++){
16 p r i n t f ( ” a[%d ] = %d\n” , i , a [ i ] ) ;
17 }
18 return 0;
19 }
Introduction to C Programming 24 / 50
1D Array Example-4 (1)
Introduction to C Programming 25 / 50
1D Array Example-4 (2)
Introduction to C Programming 26 / 50
1D Array Example-4 (3)
21 3 5 8 5 11 22 14 51 9
3 21 5 8 5 11 22 14 51 9
3 5 21 8 5 11 22 14 51 9
3 5 8 21 5 11 22 14 51 9
3 5 8 5 21 11 22 14 51 9
3 5 8 5 11 21 22 14 51 9
3 5 8 5 11 21 22 14 51 9
3 5 8 5 11 21 14 22 51 9
3 5 8 5 11 21 14 22 51 9
3 5 8 5 11 21 14 22 9 51
Max
Introduction to C Programming 27 / 50
1D Array Example-4 (4)
Introduction to C Programming 28 / 50
1D Array Example-4 (5): the code
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 i n t a [ 1 0 ] = {3 , 5 , 5 , 8 , 9 , 11 , 14 , 21 , 22 , 51};
5 int i = 0 , j = 0 , t = 0;
6 f o r ( i = 0 ; i < 1 0 ; i ++) {
7 f o r ( j = 0 ; j < (10− i −1) ; j ++) {
8 i f ( a [ j ] > a [ j +1])
9 {
10 t = a[ j ];
11 a [ j ] = a [ j +1];
12 a [ j +1] = t ;
13 } // i f ( a [ j ] )
14 } // f o r ( j )
15 } // f o r ( i )
16 f o r ( i = 0 ; i < 1 0 ; i ++) {
17 p r i n t f ( ”%d ” , a [ i ] ) ;
18 }
19 return 0;
20 }
Introduction to C Programming 29 / 50
Outline
1 Arrays
1D Array
2D Array
2 Strings
Introduction to C Programming 30 / 50
Opening Discussion: 2D Array
1 i n t main ( )
2 { 1 i n t main ( )
3 f l o a t math [ 4 0 ] ; 2 {
4 float c [40]; 3 float courses
5 float phis [40]; [40][4];
6 f l o a t bio [ 4 0 ] ; 4 ...
7 ... 5 }
8 }
Introduction to C Programming 31 / 50
2D Array: declaration
type arrayName[row][column];
• Similar as 1D array, type is required
• “arrayName” should be unique
• “row” and “column” should be constant expressions
1 i n t main ( )
2 {
3 f l o a t a [ 4 0 ] [ 4 ] ; // t h e r e 40 r o w s and 4 c o l u m n s i n e a c h row
4 a [ 3 ] [ 2 ] = 3.14;
5 return 0;
6 }
Introduction to C Programming 32 / 50
2D Array: initialization (1)
1 i n t main ( )
2 {
3 f l o a t a [ 3 ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }
1 i n t main ( )
2 {
3 f l o a t a [ 3 ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }
Introduction to C Programming 33 / 50
2D Array: initialization (2)
1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }
1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }
Introduction to C Programming 34 / 50
2D Array: initialization (3)
1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {{1 ,3 ,1 ,1} ,{1 ,2 ,1 ,3} ,{1 ,12 ,1 ,2}};
4 return 0;
5 }
1 i n t main ( )
2 {
3 f l o a t a [ 4 ] [ ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 return 0;
5 }
Introduction to C Programming 35 / 50
2D Array: how it looks like
4 bytes
10127 a[0][0] 3.1
10131 a[0][1] 4.2
10135 a[0][2] 5.0
… a[0][3] 0
a[1][0] 7
... ...
??? a[2][1] 3.1
??? a[2][2] 3.3
10171 a[2][3] 4.2
• 3(row)×4(column)×4 bytes
Introduction to C Programming 36 / 50
2D Array: visit element of the array
1 i n t main ( )
2 {
3 f l o a t a [ ] [ 4 ] = {1 ,3 ,1 ,1 ,1 ,2 ,1 ,3 ,1 ,12 ,1 ,2};
4 int i = 0 , j = 0;
5 f o r ( i = 0 ; i < 3 ; i ++)
6 {
7 f o r ( j = 0 ; j < 4 ; j ++)
8 {
9 p r i n t f ( ”%f ” , a [ i ] [ j ] ) ;
10 }
11 p r i n t f ( ” \n” ) ;
12 }
13 return 0;
14 }
Introduction to C Programming 37 / 50
Example-1: transpose a matrix (1)
Introduction to C Programming 38 / 50
Example-1: transpose a matrix (2)
1 #i n c l u d e < s t d i o . h>
2 i n t main ( ) {
3 int a [12] = {12 ,13 ,5 ,5 ,7 ,21 ,6 ,4 ,10 ,5 ,5 ,9};
4 i n t b [ 1 2 ] = {0};
5 i n t i = 0 , j = 0 , tmp = 0 ;
6 f o r ( i = 0 ; i < 4 ; i ++){
7 f o r ( j = 0 ; j < 3 ; j ++){
8 b [ j ∗4+ i ] = a [ i ∗3+ j ] ;
9 }
10 }
11 f o r ( i = 0 ; i < 3 ; i ++){
12 f o r ( j = 0 ; j < 4 ; j ++){
13 p r i n t f ( ”%4d” , b [ i ∗4+ j ] ) ;
14 }
15 p r i n t f ( ” \n” ) ;
16 }
17 return 0;
18 }
Introduction to C Programming 39 / 50
Outline
1 Arrays
1D Array
2D Array
2 Strings
Introduction to C Programming 40 / 50
Opening discussion
• Now, we are going to discuss a special kind of array
• Array of chars, we give it a new name string
• Different from integer array, empty elements are set to ‘\0’
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
1 #i n c l u d e < s t d i o . h>
3 {
2 i n t main ( )
4 c h a r h i [ 8 ] ={ ’ h ’ , ’ e ’ , ’ l ’
3 {
, ’ l ’ , ’o ’ };
4 c h a r h i [ 8 ] ={ ’ h ’ , ’ e ’ , ’ l ’
5 int i = 0;
, ’ l ’ , ’o ’ };
6 f o r ( i < 8 ; i ++)
5 int i = 0;
7 {
6 p r i n t f ( ”%s ” , h i ) ;
8 p r i n t f ( ”%c ” , h i [ i ] ) ;
7 return 0;
9 }
8 }
10 return 0;
9 [ Output : h e l l o ]
11 }
10
12 [ Output : h e l l o ]
13
Introduction to C Programming 41 / 50
String: definition and initialization
1 #i n c l u d e < s t d i o . h>
2 i n t main ( )
3 {
4 c h a r ch [ 6 ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ , ’ \0 ’ } ;
5 c h a r ch [ ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ , ’ \0 ’ } ;
6 /∗we h a v e 6 c h a r s t h e r e ∗/
7 c h a r ch [ 6 ] = { ’H ’ , ’ e ’ , ’ l ’ , ’ l ’ , ’ o ’ } ;
8 /∗ ’ \ 0 ’ i s a u t o m a t i c a l l y appended ∗ ∗/
9 c h a r ch [ 6 ] = { ” H e l l o ” } ;
10 c h a r ch [ 6 ] = ” H e l l o ” ;
11 c h a r ch [ ] = ” H e l l o ” ;
12 return 0;
13 }
Introduction to C Programming 42 / 50
String Operation: strcpy
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 { [Output:]
5 c h a r ch [ 1 0 ] ;
6 s t r c p y ( ch , ” h i ” ) ; 1 hi
7 p r i n t f ( ”%s \n” , ch ) ; 2 ha
8 s t r c p y ( ch , ” ha ” ) ;
9 p r i n t f ( ”%s \n” , ch ) ;
10 return 0;
11 }
Introduction to C Programming 43 / 50
String Operation: strcmp (1)
• Compare whether two strings are equal or not
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” h i ” ) ;
7 s t r c p y ( ch2 , ” ha ” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) == 1 ) {
9 p r i n t f ( ” ch1 > ch2 \n” ) ; [Output]
10 } e l s e i f ( s t r c m p ( ch1 , ch2 )==−1)
ch1 > ch2
11 {
12 p r i n t f ( ” ch1 < ch2 \n” ) ;
13 }
14 e l s e i f ( s t r c m p ( ch1 , ch2 )==0){
15 printf (” identical ”) ;
16 }
17 }
Introduction to C Programming 44 / 50
String Operation: strcmp (2)
• Compare whether two strings are equal or not
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” he ” ) ;
7 s t r c p y ( ch2 , ”we” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) == 1 )
9 {
10 p r i n t f ( ” ch1 > ch2 \n” ) ; [Output]
11 } e l s e i f ( s t r c m p ( ch1 , ch2 )==−1)
ch1 < ch2
12 {
13 p r i n t f ( ” ch1 < ch2 \n” ) ;
14 }
15 e l s e i f ( s t r c m p ( ch1 , ch2 )==0)
16 {
17 printf (” identical ”) ;
18 }
19 }
Introduction to C Programming 45 / 50
String Operation: strcmp (3)
• Compare whether two strings are equal or not
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 c h a r ch1 [ 1 0 ] , ch2 [ 1 0 ] ;
6 s t r c p y ( ch1 , ” h i ” ) ;
7 s t r c p y ( ch2 , ” h i ” ) ;
8 i f ( s t r c m p ( ch1 , ch2 ) !=0)
9 { [Output]
10 printf (” different ”) ;
identical
11 }
12 e l s e i f ( s t r c m p ( ch1 , ch2 )==0)
13 {
14 printf (” identical ”) ;
15 }
16 return 0;
17 }
Introduction to C Programming 46 / 50
String Operation: strlen (1)
Introduction to C Programming 47 / 50
String Operation: strlen (2)
Introduction to C Programming 48 / 50
String Operation: strcat
1 #i n c l u d e < s t d i o . h>
2 #i n c l u d e < s t r i n g . h>
3 i n t main ( )
4 {
5 char a [ 2 0 ] = ” h e l l o ” ;
1 hello
6 char b [ 1 0 ] = ” world ” ;
2 world
7 p r i n t f ( ” a=%s \n” , a ) ;
3 h e l l o world
8 p r i n t f ( ”b=%s \n” , b ) ;
9 strcat (a , b) ;
10 p r i n t f ( ” a=%s \n” , a ) ;
11 return 0;
12 }
Introduction to C Programming 49 / 50
Summary over string and char array
Usage Comments
Copy “str2” to “str1”, the content
strcpy(str1, str2)
of “str1” will be overwriten
strlen(str1) Calculate the number of characters before ‘\0’
strcat(str1, str2) Concatenate “str2” to “str1” and save to “str1”
Compare two strings,
strcmp(str1, str2)
returns -1, 1 or 0 if they are identical
Introduction to C Programming 50 / 50