arrays&(1)&santi/cpe102_files/... · arrays& •...

13
Arrays (1) ISNE 261102 Faculty of Engineering Chiang Mai University

Upload: others

Post on 09-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Arrays  (1)  

ISNE  261102  Faculty  of  Engineering  Chiang  Mai  University  

Page 2: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Arrays  

•  In  case  we  want  a  variable  to  hold  many  values.  

•  An  array  is  a  consecu've  group  of  memory  loca'ons  that  all  have  the  same  type.    

•  To  refer  to  a  parHcular  locaHon  or  element  in  the  array,  we  specify  the  name  of  the  array  and  the  posiHon  number  of  the  parHcular  element  in  the  array.    

Page 3: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Arrays  

Page 4: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Arrays  

•  Here  shows  an  integer  array  called  c  that  contains  12  elements.    

•  We  refer  to  any  one  of  these  elements  by  giving  the  array  name  followed  by  the  parHcular  element’s  posi%on  number  (called  index)  in  square  brackets  ([]).    

•  The  first  element  has  index  0  (zero).      

Page 5: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Arrays  •  The  array  c  has  12  elements,  which  are  referred  to  as  c[0]  

to  c[11].    •  The  value  of  c[0]  is  -­‐45,  the  value  of  c[1]  is  6,  the  value  of  

c[2]  is  0,  the  value  of  c[7]  is  62,  and  the  value  of  c[11]  is  78.    •  To  print  the  sum  of  the  values  contained  in  the  first  three  

elements  of  array  c,  we’d  write    

•  To  divide  the  value  of  c[6]  by  2  and  assign  the  result  to  the  variable  x,  we  would  write    

Page 6: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Declaring  Arrays  •  Like  a  regular  variable,  an  array  must  be  declared  before  it  is  used.  •  To  tell  the  compiler  to  reserve  12  elements  for  integer  array  c,  use  

the  declaraHon:  !int c[12];!

•  An  array  can  be  explicitly  iniHalized  to  specific  values  when  it  is  declared  by  enclosing  those  iniHal  values  in  braces  {}.  For  example:    int c[5] = {16, 2, 77, 40, 12071};!

•  The  iniHalizer  can  even  have  no  values  !int c[5] = {};!

•  We  can  also  leave  the  square  brackets  empty  [].  The  compiler  will  automaHcally  assume  the  size  of  the  array  that  matches  the  number  of  values  iniHalized  within  {}.    int c[] = {16, 2, 77, 40, 127};!

Page 7: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  1:  Array  of  zeros  

Page 8: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  2:  IniHalizer  

Page 9: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  3:  Assigning  values  using  operaHon  

const  quanHfier  is  used  to  declare  a  constant  variable,  which  cannot  be  modified  therea_er  –  also  called  read-­‐only  variable.    

Page 10: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  4:  Sum  

Page 11: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  5:  Frequency  

Page 12: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Examples  Using  Arrays  

•  Example  6:  Histogram  

Page 13: Arrays&(1)&santi/cpe102_files/... · Arrays& • The&array&c&has&12&elements,&which&are&referred&to&as&c[0]& to&c[11]. • The&value&of&c[0]&is&T45,&the&value&of&c[1]&is&6,&the&value&of&

Lab  Assignment  #8