SlideShare a Scribd company logo
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

PDF
The Ring programming language version 1.10 book - Part 40 of 212
Mahmoud Samir Fayed
 
PDF
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
KEY
Haskellで学ぶ関数型言語
ikdysfm
 
PDF
A tour of Python
Aleksandar Veselinovic
 
PDF
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
PDF
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward
 
PDF
The Ring programming language version 1.6 book - Part 38 of 189
Mahmoud Samir Fayed
 
DOCX
รายงานคอม
Areeya Onnom
 
The Ring programming language version 1.10 book - Part 40 of 212
Mahmoud Samir Fayed
 
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
Haskellで学ぶ関数型言語
ikdysfm
 
A tour of Python
Aleksandar Veselinovic
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Dr. Volkan OBAN
 
Flink Forward Berlin 2017: David Rodriguez - The Approximate Filter, Join, an...
Flink Forward
 
The Ring programming language version 1.6 book - Part 38 of 189
Mahmoud Samir Fayed
 
รายงานคอม
Areeya Onnom
 

What's hot (20)

PDF
R and data mining
Chaozhong Yang
 
DOCX
รายงานคอม
Areeya Onnom
 
PDF
Extracting Executable Transformations from Distilled Code Changes
stevensreinout
 
PPTX
Pragmatic metaprogramming
Mårten Rånge
 
PDF
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
Sebastian Marek
 
PDF
The Ring programming language version 1.5.1 book - Part 36 of 180
Mahmoud Samir Fayed
 
PPT
Computer Programming- Lecture 8
Dr. Md. Shohel Sayeed
 
PDF
Declarative Name Binding and Scope Rules
Eelco Visser
 
PPTX
Assignment 2 interview preparation work COSC1285
MadelineLong2
 
PDF
Rデバッグあれこれ
Takeshi Arabiki
 
PDF
Rのスコープとフレームと環境と
Takeshi Arabiki
 
PPTX
Ggplot2 v3
Josh Doyle
 
PPTX
Scala best practices
Alexander Zaidel
 
ODP
Groovy intro for OUDL
J David Beutel
 
PDF
The Ring programming language version 1.5.2 book - Part 35 of 181
Mahmoud Samir Fayed
 
PDF
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
PDF
The Ring programming language version 1.6 book - Part 35 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.9 book - Part 39 of 210
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 30 of 202
Mahmoud Samir Fayed
 
PPTX
Scala - where objects and functions meet
Mario Fusco
 
R and data mining
Chaozhong Yang
 
รายงานคอม
Areeya Onnom
 
Extracting Executable Transformations from Distilled Code Changes
stevensreinout
 
Pragmatic metaprogramming
Mårten Rånge
 
PHP Benelux 2012: Magic behind the numbers. Software metrics in practice
Sebastian Marek
 
The Ring programming language version 1.5.1 book - Part 36 of 180
Mahmoud Samir Fayed
 
Computer Programming- Lecture 8
Dr. Md. Shohel Sayeed
 
Declarative Name Binding and Scope Rules
Eelco Visser
 
Assignment 2 interview preparation work COSC1285
MadelineLong2
 
Rデバッグあれこれ
Takeshi Arabiki
 
Rのスコープとフレームと環境と
Takeshi Arabiki
 
Ggplot2 v3
Josh Doyle
 
Scala best practices
Alexander Zaidel
 
Groovy intro for OUDL
J David Beutel
 
The Ring programming language version 1.5.2 book - Part 35 of 181
Mahmoud Samir Fayed
 
(chapter 6) A Concise and Practical Introduction to Programming Algorithms in...
Frank Nielsen
 
The Ring programming language version 1.6 book - Part 35 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 39 of 210
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 30 of 202
Mahmoud Samir Fayed
 
Scala - where objects and functions meet
Mario Fusco
 
Ad

Viewers also liked (9)

PDF
2 2. operators
웅식 전
 
PDF
10장 문자열클래스와파일클래스
웅식 전
 
PDF
7. variable scope rule,-storage_class
웅식 전
 
PDF
6. function
웅식 전
 
PDF
7 mid term summary
웅식 전
 
PDF
12 1. multi-dimensional array
웅식 전
 
PDF
11. array & pointer
웅식 전
 
PDF
2 3. standard io
웅식 전
 
PDF
15 2. arguement passing to main
웅식 전
 
2 2. operators
웅식 전
 
10장 문자열클래스와파일클래스
웅식 전
 
7. variable scope rule,-storage_class
웅식 전
 
6. function
웅식 전
 
7 mid term summary
웅식 전
 
12 1. multi-dimensional array
웅식 전
 
11. array & pointer
웅식 전
 
2 3. standard io
웅식 전
 
15 2. arguement passing to main
웅식 전
 
Ad

Similar to 7. arrays (20)

PDF
Arrays
Learn By Watch
 
PPTX
Array in c language
home
 
PDF
Notes
Hitesh Wagle
 
PDF
Array notes
Hitesh Wagle
 
PPT
Unit3 jwfiles
mrecedu
 
PPT
C Language Unit-3
kasaragadda srinivasrao
 
PPT
Array
Mayank Saxena
 
PPTX
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
Jiraporn Chaijaroen
 
PPTX
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
Jiraporn Chaijaroen
 
PDF
Arrays and library functions
Swarup Kumar Boro
 
PPT
Fp201 unit4
rohassanie
 
PDF
Plc (1)
James Croft
 
DOCX
Array assignment
Ahmad Kamal
 
PPTX
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
PPTX
Lecture1 classes2
Noor Faezah Mohd Yatim
 
PPTX
array ppt of c programing language for exam
shimishtrivedi
 
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 
Array in c language
home
 
Array notes
Hitesh Wagle
 
Unit3 jwfiles
mrecedu
 
C Language Unit-3
kasaragadda srinivasrao
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
Jiraporn Chaijaroen
 
ตัวแปรชุดและตัวแปรกลุ่มอักขระ
Jiraporn Chaijaroen
 
Arrays and library functions
Swarup Kumar Boro
 
Fp201 unit4
rohassanie
 
Plc (1)
James Croft
 
Array assignment
Ahmad Kamal
 
Module_3_Arrays - Updated.pptx............
ChiragKankani
 
Lecture1 classes2
Noor Faezah Mohd Yatim
 
array ppt of c programing language for exam
shimishtrivedi
 
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
CandiceNoraineGarcia1
 

More from 웅식 전 (20)

PDF
15 3. modulization
웅식 전
 
PDF
14. fiile io
웅식 전
 
PDF
13. structure
웅식 전
 
PDF
12 2. dynamic allocation
웅식 전
 
PDF
10. pointer & function
웅식 전
 
PDF
9. pointer
웅식 전
 
PDF
5 2. string processing
웅식 전
 
PDF
5 1. character processing
웅식 전
 
PDF
15 1. enumeration, typedef
웅식 전
 
PDF
4. loop
웅식 전
 
PDF
3 2. if statement
웅식 전
 
PDF
3 1. preprocessor, math, stdlib
웅식 전
 
PDF
2 3. standard io
웅식 전
 
PDF
2 1. variables & data types
웅식 전
 
PDF
Goorm ide 교육용버전 for skku(학생)
웅식 전
 
PDF
구름 기본 소개자료
웅식 전
 
PDF
Goorm ide 소개 슬라이드(교육용 버전)
웅식 전
 
PDF
W14 chap13
웅식 전
 
PDF
13th chapter12 slide
웅식 전
 
PDF
Week12 chapter11
웅식 전
 
15 3. modulization
웅식 전
 
14. fiile io
웅식 전
 
13. structure
웅식 전
 
12 2. dynamic allocation
웅식 전
 
10. pointer & function
웅식 전
 
9. pointer
웅식 전
 
5 2. string processing
웅식 전
 
5 1. character processing
웅식 전
 
15 1. enumeration, typedef
웅식 전
 
4. loop
웅식 전
 
3 2. if statement
웅식 전
 
3 1. preprocessor, math, stdlib
웅식 전
 
2 3. standard io
웅식 전
 
2 1. variables & data types
웅식 전
 
Goorm ide 교육용버전 for skku(학생)
웅식 전
 
구름 기본 소개자료
웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
웅식 전
 
W14 chap13
웅식 전
 
13th chapter12 slide
웅식 전
 
Week12 chapter11
웅식 전
 

Recently uploaded (20)

PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The Future of Artificial Intelligence (AI)
Mukul
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 

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