Upcoming SlideShare
×

# 7. arrays

413 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
413
On SlideShare
0
From Embeds
0
Number of Embeds
153
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 7. arrays

1. 1. Arrays
2. 2. Contents One-Dimensional Arrays Initialization Subscripting 2
3. 3. One-Dimensional Arrays Array – A set of variables sharing the name int num[5] ; – 5 variables will be consecutively creased with the name of “num” num address 1000 1004 1008 1012 1016 3
4. 4. One-Dimensional Arrays Accessing Members of Array – Using index – The index of the first member is 0int num[5] ; num[1] num[2] num[3] num[4] num[0]num[0] = 10 ; num 10 13 14 17 20num[1] = 13 ;num[2] = 14 ; 1000 1004 1008 1012 1016num[3] = 17 ; addressnum[4] = 20 ; 4
5. 5. One-Dimensional Arrays Accessing Members of Arraychar ch[7] ; ch[0] ch[1] ch[2] ch[3] ch[4] ch[5] ch[7]ch[0] = ‘a’ ; ch a b c d e f gch[1] = ‘b’ ;ch[2] = ‘c’ ; 1000 1001 1004 1005 1006 1002 1003ch[3] = ‘d’ ; addressch[4] = ‘e’ ;ch[5] = ‘f’ ;ch[6] = ‘g’ ; 5
6. 6. One-Dimensional Arrays Some Frequent Errors element-type array_name[size]; [Ex] int grade[50]; size of Array data type variable Name – “size of array” should be a positive constant – “index” are from 0 to N-1 • In the example, grade[0], grade[1],~ , grade[49] 6
7. 7. One-Dimensional Arrays Example double student[10] ; int aaa[10+5] ; For declaration, you cannot use variables int k = 5 ; char ch[k] ; int num[10] ; int k = 0 ; For member access, you can use variables and num[1] = 1 ; any expressions. num[2+3] = 9 ; num[k] = 0 ; num[k++] = 2 ; num[k+3] = 4 ; 7
8. 8. One-Dimensional Arrays Example a[0] = 0, a[1] = 1, a[2] = 2, ..., a[9] = 9void main() { void main() { int a[10], k ; int a[10], k ; for( k = 0 ; k < 10 ; k++ ) k = 0 ; a[k] = k ; while(k < 10)} { a[k] = k ; k++ ; } } 8
9. 9. One-Dimensional Arrays Initialization each value will be assigned to each member int a[5]; int a[5] = {1, 5, 3, 7, 6} ; a[0] = 1 ; a[1] = 5 ; a[2] = 3 ; a[3] = 7 ; a[4] = 6 ; int a[5]; if the number is less, a[0] = 1 ; other members will be 0 a[1] = 4 ; a[2] = 0 ; int a[5] = {1, 4} ; a[3] = 0 ; a[4] = 0 ; 9
10. 10. Reverse Printing Example#include <stdio.h> #include <stdio.h>void main() { void main() { int a[10], k ; int a[10], k; for( k = 0 ; k < 10 ; k++ ) k=0 ; scanf( “%d”, &a[k] ) ; while(k < 10) scanf( “%d”, &a[k++] ) ; for( k = 9 ; k >= 0 ; k-- ) printf( “%d ”, a[k] ) ; k=9; while(k >= 0) printf( “n” ) ; printf( “%d ”, a[k--] ) ;} printf( “n” ) ; } 10
11. 11. Inner Product Read 2 vectors (3 dimensional) and evaluate the inner product #include <stdio.h> 1 2 3 4 -5 6 void main() 12 { int a[3], b[3], k, p ; for( k=0;k<3;k++) scanf(“%d”, &a[k] ) ; for( k=0;k<3;k++) scanf(“%d”, &b[k] ) ; p = 0 ; for( k=0;k<3;k++) p += a[k]*b[k] ; } 11
12. 12. Rotation  Read 5 numbers and rotate it to the right [0] [1] [2] [3] [4] #include <stdio.h>num: 10 9 2 4 5 void main() { int num[5], k, tmp ; tmp = num[0] ; num[1] = num[0] ; for( k = 0 ; k < 5 ; k++ ) num[0] = num[1] ; num[2] = num[1] ; scanf( “%d”, &num[k] ) ; num[1] = num[2] ; num[3] = num[2] ; num[2] = num[3] ; num[4] = num[3] ; tmp = num[0] ; num[3] = num[4] ; num[0] = num[4] ; for( k = 0 ; k < 4 ; k++ ) num[4] = tmp ; num[k] = num[k+1] ; num[4] = tmp ; } 12
13. 13. Counting Numbers  Example Program – Counting numbers Start true1 0 9 4 5 3 2 7 9 6 4 1 -1 num[10], k, n n F0: 1 k = 0 ; k < 10 ; k++ n<01: 2 num[k] = 0 T2: 1 num[n]++ break3: 14: 25: 1 k = 0 ; k < 10 ; k++6: 17: 1 k “:” num[k]8: 09: 2 Stop 13
14. 14. Counting Numbers Example Program #include <stdio.h> void main(void) { int num[10], n, k ; for( k = 0 ; k < 10 ; k++ ) num[k] = 0 ; while ( 1 ) { scanf( “%d”, &n ) ; if( n < 0 ) break ; num[n]++ ; } for( k = 0 ; k < 10 ; k++ ) printf( “%d:%dn”, k, num[k] ) ; } 14
15. 15. Find Maximum Read 10 numbers and print the maximum 10 9 2 4 5 -9 27 -4 -2 14 Maximum: 27 10 9 2 4 5 -9 27 -4 -2 14 15
16. 16. Find Maximum 순서도로 그리면… Start pos <- 0 num[10], i, pos i = 1 ; i < 10 ; i++ i = 0 ; i < 10 ; i++ num[pos] < num[i] F x T pos <- i num[pos] Stop 16
17. 17. Find Maximum Example Program #include <stdio.h> void main(void) { int num[10], i, pos; for( i = 0 ; i < 10 ; i++ ) scanf(“%d”, &num[i] ) ; pos = 0 ; for( i = 1 ; i < 10 ; i++ ) { if( num[pos]< num[i] ) pos = i ; } printf( “Maximum:%dn”, num[pos] ); } 17
18. 18. Find Second Largest Read 10 numbers and print the second largest 10 9 2 4 5 -9 27 -4 -2 14 Second Largest: 10 10 9 2 4 5 -9 27 -4 -2 14 27 9 2 4 5 -9 10 -4 -2 14 18
19. 19. Find Second Largest  Example Program#include <stdio.h> pos = 1 ; for( i = 2 ; i < 10 ; i++ )void main(void) { if( num[pos] < num[i] ) pos = i ; int num[10], i, pos, tmp ; printf( “Second Largest:%dn”, for( i = 0 ; i < 10 ; i++ ) num[pos] ) ; scanf(“%d”, &num[i] ) ; } pos = 0 ; for( i = 1 ; i < 10 ; i++ ) if( num[pos] < num[i] ) pos = i ; tmp = num[0] ; num[0] = num[pos] ; num[pos] = tmp ; 19
20. 20. Sort Numbers Read 10 numbers and print in the descending order10 9 2 4 5 -9 27 -4 -2 14 10 9 2 4 5 -9 27 -4 -2 1427 14 10 9 5 4 2 -2 -4 -9 27 9 2 4 5 -9 10 -4 -2 14 27 14 2 4 5 -9 10 -4 -2 9 27 14 10 4 5 -9 2 -4 -2 9 27 14 10 9 5 4 2 -2 -4 -9 20
21. 21. Sort Numbers  Example Program pos = 1 ; for( i = 2 ; i < 10 ; i++ )#include <stdio.h> if( num[pos] < num[i] ) pos = i ;void main(void) { tmp = num[1] ; int num[10], i, pos, tmp ; num[1] = num[pos] ; num[pos] = tmp ; for( i = 0 ; i < 10 ; i++ ) scanf(“%d”, &num[i] ) ; pos = 2 ; for( i = 3 ; i < 10 ; i++ ) pos = 0 ; if( num[pos] < num[i] ) pos = i ; for( i = 1 ; i < 10 ; i++ ) if( num[pos] < num[i] ) pos = i ; tmp = num[2] ; num[2] = num[pos] ; tmp = num[0] ; num[pos] = tmp ; num[0] = num[pos] ; num[pos] = tmp ; ... } 21
22. 22. Sort Numbers  Example Program#include <stdio.h> for( j = 0 ; j < 9 ; j++ )void main(void) { { int num[10], i, j, pos, tmp ; pos = j ; for( i = 0 ; i < 10 ; i++ ) for( i = j+1 ; i < 10 ; i++ ) scanf(“%d”, &num[i] ) ; if( num[pos] < num[i] ) pos = i ; ... tmp = num[j] ; num[j] = num[pos] ; for( j = 0 ; j < 9 ; j++ ) num[pos] = tmp ; printf( “%d “, num[j] ) ; }} 22