SlideShare a Scribd company logo
1 of 22
Download to read offline
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 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
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
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] = 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
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
                                                              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
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
  order
10 9 2 4 5 -9 27 -4 -2 14
                            10   9   2   4   5   -9 27 -4      -2 14


27 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

More Related Content

What's hot

รายงานคอม
รายงานคอมรายงานคอม
รายงานคอม
Areeya Onnom
 
Declarative Name Binding and Scope Rules
Declarative Name Binding and Scope RulesDeclarative Name Binding and Scope Rules
Declarative Name Binding and Scope Rules
Eelco Visser
 
Assignment 2 interview preparation work COSC1285
Assignment 2 interview preparation work COSC1285Assignment 2 interview preparation work COSC1285
Assignment 2 interview preparation work COSC1285
MadelineLong2
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 

What's hot (20)

R and data mining
R and data miningR and data mining
R and data mining
 
รายงานคอม
รายงานคอมรายงานคอม
รายงานคอม
 
Extracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code ChangesExtracting Executable Transformations from Distilled Code Changes
Extracting Executable Transformations from Distilled Code Changes
 
Pragmatic metaprogramming
Pragmatic metaprogrammingPragmatic metaprogramming
Pragmatic metaprogramming
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
 
The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180The Ring programming language version 1.5.1 book - Part 36 of 180
The Ring programming language version 1.5.1 book - Part 36 of 180
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Declarative Name Binding and Scope Rules
Declarative Name Binding and Scope RulesDeclarative Name Binding and Scope Rules
Declarative Name Binding and Scope Rules
 
Assignment 2 interview preparation work COSC1285
Assignment 2 interview preparation work COSC1285Assignment 2 interview preparation work COSC1285
Assignment 2 interview preparation work COSC1285
 
Rデバッグあれこれ
RデバッグあれこれRデバッグあれこれ
Rデバッグあれこれ
 
Rのスコープとフレームと環境と
Rのスコープとフレームと環境とRのスコープとフレームと環境と
Rのスコープとフレームと環境と
 
Ggplot2 v3
Ggplot2 v3Ggplot2 v3
Ggplot2 v3
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Groovy intro for OUDL
Groovy intro for OUDLGroovy intro for OUDL
Groovy intro for OUDL
 
The Ring programming language version 1.5.2 book - Part 35 of 181
The Ring programming language version 1.5.2 book - Part 35 of 181The Ring programming language version 1.5.2 book - Part 35 of 181
The Ring programming language version 1.5.2 book - Part 35 of 181
 
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
 
The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189
 
The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210The Ring programming language version 1.9 book - Part 39 of 210
The Ring programming language version 1.9 book - Part 39 of 210
 
The Ring programming language version 1.8 book - Part 30 of 202
The Ring programming language version 1.8 book - Part 30 of 202The Ring programming language version 1.8 book - Part 30 of 202
The Ring programming language version 1.8 book - Part 30 of 202
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 

Viewers also liked

2 2. operators
2 2. operators2 2. operators
2 2. operators
웅식 전
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
웅식 전
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
웅식 전
 

Viewers also liked (9)

2 2. operators
2 2. operators2 2. operators
2 2. operators
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
7 mid term summary
7 mid term summary7 mid term summary
7 mid term summary
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 

Similar to 7. arrays

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
웅식 전
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
riturajj
 
Array in c language
Array in c languageArray in c language
Array in c language
home
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
rantd
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
mrecedu
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 

Similar to 7. arrays (20)

11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Lec23-CS110 Computational Engineering
Lec23-CS110 Computational EngineeringLec23-CS110 Computational Engineering
Lec23-CS110 Computational Engineering
 
C arrays
C arraysC arrays
C arrays
 
Cn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshanCn os-lp lab manual k.roshan
Cn os-lp lab manual k.roshan
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
 
Array
ArrayArray
Array
 
Datastructure tree
Datastructure treeDatastructure tree
Datastructure tree
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2[1062BPY12001] Data analysis with R / week 2
[1062BPY12001] Data analysis with R / week 2
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
C Language Unit-3
C Language Unit-3C Language Unit-3
C Language Unit-3
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
FALLSEM2022-23_BCSE202L_TH_VL2022230103292_Reference_Material_I_08-08-2022_C_...
 

More from 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
웅식 전
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
웅식 전
 

More from 웅식 전 (20)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
13. structure
13. structure13. structure
13. structure
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
4. loop
4. loop4. loop
4. loop
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

7. arrays

  • 2. Contents  One-Dimensional Arrays  Initialization  Subscripting 2
  • 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
  • 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. 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. 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. 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
  • 20. Sort Numbers  Read 10 numbers and print in the descending order 10 9 2 4 5 -9 27 -4 -2 14 10 9 2 4 5 -9 27 -4 -2 14 27 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. 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. 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