7. arrays
Upcoming SlideShare
Loading in...5
×
 

7. arrays

on

  • 370 views

 

Statistics

Views

Total Views
370
Views on SlideShare
187
Embed Views
183

Actions

Likes
0
Downloads
3
Comments
0

1 Embed 183

http://class.skku.goorm.io 183

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

7. arrays 7. arrays Presentation Transcript

  • Arrays
  • Contents One-Dimensional Arrays Initialization Subscripting 2
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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