c++ chapter iii

18

Click here to load reader

Upload: sorn-chanratha

Post on 12-Jul-2015

328 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C++ Chapter III

Lecturer:  Hong  Mom   1  

Lecturer:  Hong  Mom  

 Chapter  III  

   

Page 2: C++ Chapter III

Objec8ve  

Lecturer:  Hong  Mom   2  

A<er  completely  this  chapter,  you  will  be  able  to:  •  Declare  ,  ini8alize,  and  use  arrays  •  Insert  value  to  elements  of  array    •  Display  value  of  elements  of  array  •  Sort  array  

Page 3: C++ Chapter III

Arrays  

Defini8on  

An  array  is  a  group  of  variables  of  the  same  data  type.  Why  using  array?  

Ex:    We  can  store  10  values  of  the  type  int  in  an  array  without  having  to  declare  10  different  variables.      

Lecturer:  Hong  Mom   3  

Page 4: C++ Chapter III

Array  Representa8on  How  the  array  represented?  

Ex:  an  array  to  contain  5  integer  values  of  type  int  called  name  could  be  represented  like  this:          Array  declara8on  

       data_type  name[size];  

Lecturer:  Hong  Mom   4  

1  0   2   3   4  name  

int  

Page 5: C++ Chapter III

Array  Assignment  Ex:          int  arr[5];    //defines  array  of  5  elements  as  integer  called  arr    Assigning  values  to  array  

 int    arr[3]=  {4,8,9};    Note:  The  elements  between  the  braces{}  must  not  be  larger  than  the  size.  

 char    leaer[4]  =  {  ‘a’,  ’z’,  ‘j’,  ‘k’  };      int  a[2];    a[0]  =  5;    a[1]  =  11;  

   

Lecturer:  Hong  Mom   5  

Page 6: C++ Chapter III

Array-­‐Insert  value  by  cin  

•  Insert  value  by  cin    int        a[10];  for(int  i=  0;i<10;i++){              cin  >>  a[i];              cout  <<  endl;  }    

Lecturer:  Hong  Mom   6  

Page 7: C++ Chapter III

 //  testArray.cpp  //  Ini8alizing  an  array  with  a  declara8on  #include  <iostream.h>  #include  <iomanip.h>  int  main(){  

 int    n[10]  =  {  20,  32,  27,  98,  13,  40,  37,  23,  70,  73};    cout  <<  “Element”  <<  setw(13)  <<  “Value”  <<  endl;    //output  contents  of  arrays  n  in  tabular  format    for  (  int  i=  0  ;  i<10;  i++)      cout  <<  setw(7)  <<  I  <<setw(13)  <<  n[i]  <<  endl;    return  0;  

 }    

Array-­‐Example  

Lecturer:  Hong  Mom   7  

Page 8: C++ Chapter III

 Array-­‐Example  

 Output:    

Lecturer:  Hong  Mom   8  

                         Element                                                  Value  

0   20  

1   32  

2   27  

3   98  

4   13  

5   40  

6   37  

7   23  

8   70  

9   73  

Page 9: C++ Chapter III

Input  and  Output  of  Array  Example  

 #include  <iostream>  using  namespace  std;  int  main()  {  

const  int  NUMELS  =  5;  int  i,  grade[NUMELS];  for  (i  =  0;  i  <  NUMELS;  i++)  //  Enter  the  grades  {  cout  <<  "Enter  a  grade:  ";  cin  >>  grade[i];  }  cout  <<  endl;  for  (i  =  0;  i  <  NUMELS;  i++)  //  Print  the  grades  cout  <<  "grade  ["  <<  i  <<  "]  is  "  <<  grade[i]  <<  endl;  return  0;  

}    

Lecturer:  Hong  Mom   9  

Page 10: C++ Chapter III

Input  and  Output  of  Array  Example  

Output      Enter  a  grade:  85    Enter  a  grade:  90    Enter  a  grade:  78    Enter  a  grade:  75    Enter  a  grade:  92  

   grade[0]  is  85    grade[1]  is  90    grade[2]  is  78    grade[3]  is  75    grade[4]  is  92  

Lecturer:  Hong  Mom   10  

Page 11: C++ Chapter III

Array  of  char  

•  Strings            -­‐    arrays  of  characters            -­‐    all  strings  end  with  null  (‘\0’)  Example:    

 char  str[  ]  =  “hello”;    //  null  character  implicitly  added    char  str[  ]  =  {‘h’,  ‘e’  ,  ‘l’  ,  ‘l’,  ‘o’,  ‘\0’  };  

               -­‐    Subscrip8ng      str[0]  =  ‘h’    str[2]  =  ‘l’  

 Lecturer:  Hong  Mom   11  

Page 12: C++ Chapter III

Array  of  char-­‐  Example  

Lecturer:  Hong  Mom   12  

#include  <iostream.h>  void  main(){  

 char  str1[20];    char  str2[  ]  =  “string  literal”;    //read  string  from  user    cout  <<  “Enter  the  string  “hello  there”:“;    cin  >>  str1;    //reads  “hello”  [space  terminates  input]        //out  strings    cout  <<  “str1  is  :  “  <<  str1;    cout  <<  “str2  is  :  “  <<  str2;    cout  <<  “\nstr1  with  spaces  between  characters  is  :  \n”;    //output  characters  un8l  null  character  is  reached    for  (  int  i  =  0;  str1[i]  !=  ‘\0’;  i++  )        cout  <<  str1[i]  <<‘  ‘;    cin  >>  str1;    //reads  “there”    cout  <<  “\nstr1  is:  “  <<  str1  <<  endl;  

}                          }  

Page 13: C++ Chapter III

Array  of  char-­‐  Example  

Enter  the  string  “hello  there”:  hello  there  str1  is  :  hello  str2  is  :  string  literal  str1  with  spaces  between  characters  is:    h  e  l  l  o  str1  is:    there  

Lecturer:  Hong  Mom   13  

Page 14: C++ Chapter III

A  two-­‐dimensional  array  

•  Defini8on              A  two-­‐dimensional  array:  consists  of  both  rows  and  columns  of  elements  Example:    two-­‐dimensional  array  of  integer            

Lecturer:  Hong  Mom   14  

8   16   9   52  

3   15   27   6  

14   25   2   10  

Page 15: C++ Chapter III

[0]   [1]   [2]   [3]  

[0]   8   16   9   52  

[1]   3   15   27   6  

[2]   14   25   2   10  

int    a[3][4];      //3  rows  and  4  columns  

A  two-­‐dimensional  array  

Lecturer:  Hong  Mom   15  

a[1][3]    

Row  posi8on  

Column  posi8on  

Page 16: C++ Chapter III

A  two-­‐dimension  array  

•  Ini8alizing  a  two-­‐  dimensional  array  A  two-­‐dimensional  array,  like  a  one-­‐dimensional  array,  is  ini8alized  by  enclosing  by  ini8al  values  in  braces.  Example:      int  val[4][3]  =  {{  4,  9,  5},          {2,  11,3},        {21,  9,  32},        {10,  1,  5}  };    

Lecturer:  Hong  Mom   16  

Page 17: C++ Chapter III

Sor8ng  array  

int  arr[10]={4,8,2,7,11,3,20,68,45,25};  for(int  i=0;i<9;i++)            for(int  j  =  i+1;j<10;j++)                      if(a[i]>a[j]){                        int  temp  =  a[i];                                a[i]  =  a[j];                                a[j]  =  temp;                      }  

Lecturer:  Hong  Mom   17  

Page 18: C++ Chapter III

Sor8ng  array  

Before  sor8ng        4    8    2    7    11    3    20    68    45    25  A<er  sor8ng      2    3    4    7    8    11    20    25    45    68        

Lecturer:  Hong  Mom   18