Top Banner
Arrays
22

7. arrays

Nov 07, 2014

Download

Technology

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 7. arrays

Arrays

Page 2: 7. arrays

2

Contents

One-Dimensional Arrays

Initialization

Subscripting

Page 3: 7. arrays

3

One-Dimensional Arrays

Array

– A set of variables sharing the name

– 5 variables will be consecutively creased with the name of “num”

int num[5] ;

num

address 1000 1004 1008 1012 1016

Page 4: 7. arrays

4

One-Dimensional Arrays

Accessing Members of Array

– Using index

– The index of the first member is 0

int num[5] ;

num[0] = 10 ;

num[1] = 13 ;

num[2] = 14 ;

num[3] = 17 ;

num[4] = 20 ;

10 13 14 17 20 num

1000 1004 1008 1012 1016

num[0] num[1] num[2] num[3] num[4]

address

Page 5: 7. arrays

5

One-Dimensional Arrays

Accessing Members of Array

char ch[7] ;

ch[0] = ‘a’ ;

ch[1] = ‘b’ ;

ch[2] = ‘c’ ;

ch[3] = ‘d’ ;

ch[4] = ‘e’ ;

ch[5] = ‘f’ ;

ch[6] = ‘g’ ;

a b c d e f g ch

1000 1001 1002 1003 1004

ch[0]

address

ch[1] ch[2] ch[3] ch[4] ch[5] ch[7]

1005 1006

Page 6: 7. arrays

6

One-Dimensional Arrays

Some Frequent Errors

– “size of array” should be a positive constant

– “index” are from 0 to N-1

• In the example, grade[0], grade[1],~ , grade[49]

element-type array_name[size];

[Ex] int grade[50];

data type variable Name

size of Array

Page 7: 7. arrays

7

One-Dimensional Arrays

Example

double student[10] ;

int aaa[10+5] ;

int k = 5 ;

char ch[k] ;

int num[10] ;

int k = 0 ;

num[1] = 1 ;

num[2+3] = 9 ;

num[k] = 0 ;

num[k++] = 2 ;

num[k+3] = 4 ;

For declaration, you cannot use variables

For member access, you can use variables and any expressions.

Page 8: 7. arrays

8

One-Dimensional Arrays

void main() {

int a[10], k ;

for( k = 0 ; k < 10 ; k++ )

a[k] = k ;

}

Example

void main() {

int a[10], k ;

k = 0 ;

while(k < 10)

{

a[k] = k ;

k++ ;

}

}

a[0] = 0, a[1] = 1, a[2] = 2, ..., a[9] = 9

Page 9: 7. arrays

9

One-Dimensional Arrays

int a[5];

a[0] = 1 ;

a[1] = 5 ;

a[2] = 3 ;

a[3] = 7 ;

a[4] = 6 ;

Initialization

int a[5] = {1, 5, 3, 7, 6} ;

int a[5];

a[0] = 1 ;

a[1] = 4 ;

a[2] = 0 ;

a[3] = 0 ;

a[4] = 0 ;

int a[5] = {1, 4} ;

each value will be assigned to each member

if the number is less, other members will be 0

Page 10: 7. arrays

10

Reverse Printing

#include <stdio.h>

void main() {

int a[10], k ;

for( k = 0 ; k < 10 ; k++ )

scanf( “%d”, &a[k] ) ;

for( k = 9 ; k >= 0 ; k-- )

printf( “%d ”, a[k] ) ;

printf( “\n” ) ;

}

Example

#include <stdio.h>

void main() {

int a[10], k;

k=0 ;

while(k < 10)

scanf( “%d”, &a[k++] ) ;

k=9;

while(k >= 0)

printf( “%d ”, a[k--] ) ;

printf( “\n” ) ;

}

Page 11: 7. arrays

Read 2 vectors (3 dimensional) and evaluate the inner product

11

Inner Product

1 2 3

4 -5 6

12

#include <stdio.h>

void main()

{

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] ;

}

Page 12: 7. arrays

Read 5 numbers and rotate it to the right

12

Rotation

num[1] = num[0] ;

num[2] = num[1] ;

num[3] = num[2] ;

num[4] = num[3] ;

num[0] = num[4] ;

10 9 2 4 5 num:

tmp = num[0] ;

num[0] = num[1] ;

num[1] = num[2] ;

num[2] = num[3] ;

num[3] = num[4] ;

num[4] = tmp ;

[0] [1] [2] [3] [4] #include <stdio.h>

void main()

{

int num[5], k, tmp ;

for( k = 0 ; k < 5 ; k++ )

scanf( “%d”, &num[k] ) ;

tmp = num[0] ;

for( k = 0 ; k < 4 ; k++ )

num[k] = num[k+1] ;

num[4] = tmp ;

}

Page 13: 7. arrays

Counting Numbers

Example Program

– Counting numbers

13

1 0 9 4 5 3 2 7 9 6 4 1 -1

0: 1

1: 2

2: 1

3: 1

4: 2

5: 1

6: 1

7: 1

8: 0

9: 2

Start

Stop

k “:” num[k]

true

n

k = 0 ; k < 10 ; k++

T

F

n < 0

num[n]++ break

k = 0 ; k < 10 ; k++

num[k] = 0

num[10], k, n

Page 14: 7. arrays

Counting Numbers

Example Program

14

#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:%d\n”, k, num[k] ) ;

}

Page 15: 7. arrays

Read 10 numbers and print the maximum

15

Find Maximum

10 9 2 4 5 -9 27 -4 -2 14

Maximum: 27

10 9 2 4 5 -9 27 -4 -2 14

Page 16: 7. arrays

Find Maximum

순서도로 그리면…

16

Start

num[10], i, pos

Stop

num[pos]

pos <- 0

i = 0 ; i < 10 ; i++

x

i = 1 ; i < 10 ; i++

num[pos] < num[i]

pos <- i

T

F

Page 17: 7. arrays

Example Program

17

Find Maximum

#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:%d\n”, num[pos] );

}

Page 18: 7. arrays

Read 10 numbers and print the second largest

18

Find 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

Page 19: 7. arrays

Example Program

19

Find Second Largest

#include <stdio.h>

void main(void) {

int num[10], i, pos, tmp ;

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 ;

tmp = num[0] ;

num[0] = num[pos] ;

num[pos] = tmp ;

pos = 1 ;

for( i = 2 ; i < 10 ; i++ )

if( num[pos] < num[i] ) pos = i ;

printf( “Second Largest:%d\n”,

num[pos] ) ;

}

Page 20: 7. arrays

Read 10 numbers and print in the descending order

20

Sort Numbers

10 9 2 4 5 -9 27 -4 -2 14

27 14 10 9 5 4 2 -2 -4 -9

10 9 2 4 5 -9 27 -4 -2 14

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

Page 21: 7. arrays

Example Program

21

Sort Numbers

#include <stdio.h>

void main(void) {

int num[10], i, pos, tmp ;

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 ;

tmp = num[0] ;

num[0] = num[pos] ;

num[pos] = tmp ;

pos = 1 ;

for( i = 2 ; i < 10 ; i++ )

if( num[pos] < num[i] ) pos = i ;

tmp = num[1] ;

num[1] = num[pos] ;

num[pos] = tmp ;

pos = 2 ;

for( i = 3 ; i < 10 ; i++ )

if( num[pos] < num[i] ) pos = i ;

tmp = num[2] ;

num[2] = num[pos] ;

num[pos] = tmp ;

...

}

Page 22: 7. arrays

Example Program

22

Sort Numbers

#include <stdio.h>

void main(void) {

int num[10], i, j, pos, tmp ;

for( i = 0 ; i < 10 ; i++ )

scanf(“%d”, &num[i] ) ;

...

for( j = 0 ; j < 9 ; j++ )

printf( “%d “, num[j] ) ;

}

for( j = 0 ; j < 9 ; j++ )

{

pos = j ;

for( i = j+1 ; i < 10 ; i++ )

if( num[pos] < num[i] ) pos = i ;

tmp = num[j] ;

num[j] = num[pos] ;

num[pos] = tmp ;

}