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. One-Dimensional Arrays
Accessing Members of Array
– Using index
– The index of the first member is 0
int num[5] ; num[1] num[2] num[3] num[4]
num[0]
num[0] = 10 ; num 10 13 14 17 20
num[1] = 13 ;
num[2] = 14 ; 1000 1004 1008 1012 1016
num[3] = 17 ;
address
num[4] = 20 ;
4
5. One-Dimensional Arrays
Accessing Members of Array
char 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 g
ch[1] = ‘b’ ;
ch[2] = ‘c’ ; 1000 1001 1004 1005 1006
1002 1003
ch[3] = ‘d’ ; address
ch[4] = ‘e’ ;
ch[5] = ‘f’ ;
ch[6] = ‘g’ ;
5
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. 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. One-Dimensional Arrays
Example
a[0] = 0, a[1] = 1, a[2] = 2, ..., a[9] = 9
void 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. 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
13. Counting Numbers
Example Program
– Counting numbers Start
true
1 0 9 4 5 3 2 7 9 6 4 1 -1 num[10], k, n
n
F
0: 1 k = 0 ; k < 10 ; k++
n<0
1: 2
num[k] = 0 T
2: 1
num[n]++ break
3: 1
4: 2
5: 1
k = 0 ; k < 10 ; k++
6: 1
7: 1 k “:” num[k]
8: 0
9: 2
Stop
13
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. 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. 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. 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. 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. 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