SlideShare a Scribd company logo
Chapter 5 : ARRAY 5.1 Introduction  5.2 One Dimensional Array 5.2.1 Declaring Array   5.2.2  Array Initialization   5.2.3  Array Referencing   5.2.4  Array to Function   5.3 Two Dimensional Array 5.3.1 Two Dimensional Array Declaration 5.3.2 Two Dimensional Array Initialization 5.3.3 Two Dimensional Array Referencing
5.1  Introduction Array is a group of memory locations related by the fact that they all have the same  name  & the same  type . group together Data items   a single composite  data  structure  called  array
5.1  Introduction (Cont..) Example 1  : To compute the average of exam score for 5 students, we need to come up with 5 identifiers. Declaration : int exam_score1, exam_score2, exam_score3, exam_score4, exam_score5;  Memory allocation  : exam_score1 exam_score2 exam_score3 exam_score4 exam_score5
5.1  Introduction (Cont..) Better use only 1 identifier to represent all 5 exam scores & an index to specify the student to whom the exam score belongs. By using an array. Declaration  : int exam_score[5]; Memory allocation  : exam_score[0] exam_score[1] exam_score[2] exam_score[3] exam_score[4] Note :  The enumeration of elements(array index/subscript) within an array always start  with 0 not 1.  If the array has N elements, the last elements is in  position N-1.
5.2 One Dimensional Array A single dimensional array Terms  : Array   a collection of data items of the same name & data type. Array name name of array that is an identifier. Array element Maximum no./size of array that can be stored in array. Array subscript/Index a value/expression enclosed in brackets after the array name,  specifying which array element to access. Must start with 0.
5.2.1   Declaring Array When declaring arrays, we have to specify : Type of array Array Name Number of elements Format  : data_type array_name[ number_Of_Elements ]; Example  : int exam_score[28]; Array size Array name Array type
5.2.1   Declaring Array (cont).. Another Examples  : i) int c[10]; ii) float myArray[ 3284 ]; iii) int a[5];   /* use a[0], a[1], .., a[4] */ iv) char b[3] ;   /* use b[0], b[1], b[2] */ v) float c[2] = { .1, .2};   /*initialization at declaration.c[0] = 0.1 , c[1]=0.2 */ vi) double c[3][5];   /* two dimensional array
5.2.1 Declaring Array (cont).. •  Declaring multiple arrays of same type –  Format similar to regular variables Example : int b[100], x[27]; char car[23], lorry[45]; float duck[3], ant[9];
5.2.2 Array Initialization Array can be initialized at compile time and run time. (i) :  Compile-Time Initialization of Arrays (while declaring) An initializer must be a constant value. Example  : int exam_score[4] = { 88, 85, 76, 91 }; 4 initializers, therefore 4 element array We also can declare the no.of element implicitly. Example  : int exam_score[ ] = {88,85,76,91} first element is refer as exam_score[0]  third element is refer as exam_score[2]
5.2.2 Array Initialization (cont..) (ii) :  Run-Time Initialization of Arrays An array can be initialize at run time explicitly. Example  : a) for (int i = 0; i < 27; i++)    exam_score[i] = 0.0; b) for( int b = 0; b < 27; b++) scanf(“%d”, &exam_score[b]);
5.2.2 Array Initialization (cont…) In addition, if we leave some values of initialization,  rightmost elements will become 0. Example  : int exam_score[4] = {88,85}; -> the 3 rd   & 4 th  element are 0 int exam_score[5] = {0};   All elements are 0
5.2.3 Array Referencing Each individual element is referred by specifying array name & index. Format : arrayname [  position number  ] Example  : (i) exam_score[0] = 89; printf( &quot;%d&quot;, exam_score[ 0 ] ); (ii) float   x[ ] = { 4.05,3.45,3.21,6.55,10.23} for( int a = 0; a <= 4; a++)    printf( “ element for index %d = %f\n”, a, x[a]); //output for the fragment code : element for index 0 = 4.05 element for index 1 = 3.45 element for index 2 = 3.21   element for index 3 = 6.55   element for index 4 = 10.23
5.2.3 Array Referencing(cont…) index     0 1 2 3 4 x x[0]  x[1]  x[2]  x[3]  x[4] Name of array ( all elements of this array, have the same name,  x ) Position number of the element within array  x “ array subscript” 4.05  3.45  3.21  6.55  10.23
5.2.3 Array Referencing(cont…)   Manipulation Statement using array : Perform operations in subscript (index). Example  : double Mark [8];   Mark [0]  Mark[1]  Mark[2]  Mark[3]  Mark [4]  Mark [5]  Mark [6]  Mark [7] printf (“ %.f ”, Mark[0]); Mark[3] =25.0; sum= Mark[0] + Mark[1]; sum += Mark[2]; Mark[3] += 1.0; Mark[2] = Mark [0] + Mark [1]; 16.0 12.0 6.0 8.0 2.5 12.0 14.0 10.5
5.2.4  Passing Array to Function •  2 ways to pass array to a function: (i) pass individual element (call by value) (ii) pass the whole elements (call by reference) pass individual element (call by value) Pass individual element of an array. Pass a value parameter. Only the  n  element is passed by using an indexed expression (i.e.,  diod[2] )
5.2.4  Passing Array to Function (cont..) Example Program 5.4 //passing individual elements #include<stdio.h> void print_third_diod (double d ); void main( ) { double  diod[5]={3.3,3.9,4.7,5.1,6.2}; print_third_diod  (diod[2]); } void print_third_diod (double d) { printf(&quot;The value of the third diod is %.1f\n&quot;,d); } //Program Output The value of the third diod is 4.7 Pass the value of  diod[2] , 4.7 to function print_third_diod Copy the value of  diod[2]   from the main function
5.2.4  Passing Array to Function (cont..) (ii)  pass the whole elements (call by reference) Passing the whole array. Passing only the name of array. Name of array is address of first element –  Function knows where the array is stored •  At function definition, formal parameter must be an array type. Size of array does not need to be specified. (i.e., float count_average(float  r[ ] )  ).
5.2.4  Passing Array to Function (cont..) Example Program 5.5 //Passing the whole array #include<stdio.h> float count_average(float r[]); void main( ){ float average_relay; float relay[5]={5,6,9,12,24}; average_relay=count_average (relay) ; printf(&quot;The average of 5 relays is %.2f\n&quot;,average_relay); } float count_average( float r[ ] ) { float total=0; for(int i=0;i<5;i++) total+=r[i]; return(total/(float)5); } // Program Output The average of 5 relays is 11.20 Entire array passed to  function  count_average relay[0] relay[1] relay[2] relay[3] relay[4]
5.2.4  Passing Array to Function (cont..)   Another example : #include<stdio.h> void print_mark( int [ ], int, float);  // Function prototype for  print_mark void main( ) { int mark[4]; float average; mark[0] = 25; mark[1] = 35; mark[2] = 20;  Entire array passed call by reference mark[3] = 20; average = (mark[0] + mark[1] + mark[2] + mark[3])/ 4; print_mark( mark, mark[2], average);  } void print_mark( int m[], int a, float b) { printf(“Mark[2] is %d\n”, a); printf(“Average for 4 marks is %f\n”, b); }  Call Function  print_mark Send the value for mark[2] ,20 to function  print_mark
5.3  Two Dimensional Array Array of array. 2-D array have 2 subscripts showing the no. of rows & the no. of column. 5.3.1  Two Dimensional Array Declaration Similar with 1-D declaration except it have 2 subscripts. Format  :   Data_type array_name [no_of_element1][no_of_element2] data_type :  either int, float, double, char. array_name :  name of array that are an identifier.  no_of_elements1 :  size of array row.  no_of_elements 2 :  size of column.
5.3.1 Two Dimensional Array Declaration(cont..) Example  : (i) int x[10][5]; /*allocate 50 spaces (row:0-9,column:0-4)*/ (ii) float a[3][20];  /*allocate 60 spaces (row:0-2,column:0-19)*/ (iii)  int exam_score[3][4];  /*allocate 12 spaces (row:0-2,column:0-3)*/
5.3.1 Two Dimensional Array Declaration(cont..) Example  : int exam_score[3][4];  row1 row2 row3 column1  column2 column3  column4 array name row subscript column subscript row [0]-[2] column [0]-[3] exam_score[2][3] exam_score[2][2] exam_score[2][1] exam_score[2][0] exam_score[1][3] exam_score[1][2] exam_score[1][1] exam_score[1][0] exam_score[0][3] exam_score[0][2] exam_score[0][1] exam_score[0][0]
5.3.1 Two Dimensional Array Declaration(cont..) The right index of an array is increased first before the left index. Example  : int exam_score[3][4]={70,80,90,72,82,92,71,81,91,67,78,98 }; exam_score[0][0] exam_score[0][1] exam_score[0][2] exam_score[0][3] exam_score[1][0] exam_score[1][1] exam_score[1][2] exam_score[1][3] exam_score[2][0] exam_score[2][1] exam_score[2][2] exam_score[2][3] 98 78 67 91 81 71 92 82 72 90 80 70
5.3.2 Two Dimensional Array Initialization We can initialize 2-D array at compile time and run time. :  Compile-Time Initialization of Arrays (while declaring) Example  : (i)   int exam_score[3][4]={70,80,90,72,82,92,71,81,91,67,78,98}; The best way : nest the data in braces to show the exact nature of the array. Example  :   (ii)  int exam_score[3][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}};
5.3.2 Two Dimensional Array Initialization(cont…) also can declare the far left dimension size implicitly. Example  : (iii) int exam_score[ ][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}}; If we leave some values out of each row, compiler initialize them to 0. Example  : (iv) int exam_score[3 ][4]={{70,80},{82,92,71},{91,67,78,98}};
5.3.2 Two Dimensional Array Initialization(cont..) : Run-Time Initialization Example  : int exam_score[3][4];   //array 3x4 array can be initialized by  nested for  loops.  The 1 st   loop varies the row from 0 - 2.  The 2 nd   loop varies the column from 0 - 3.  for (row=0;row<3;row++) for(column=0; column<4; column++) scanf(“%d”,&exam_score[row][column]); //end of for //end of for run the program, enter 12 values for the elements &  they are stored in the appropriate locations.
5.3.3 Two Dimensional Array Referencing by specifying  array name & its row&column index.  Can access elements in array, using  nested for  .  Example : exam_score[0][2] = 99; exam_score[1][4]= exam_score[0][3]+10;
5.3.3 Two Dimensional Array Referencing (cont…) Example Program 5.8 #include<stdio.h> void main( ) { int exam_score[3][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}}; for(int row=0; row<3; row++) {   for(int column=0; column<4; column++){ exam_score[row][column]= exam_score[row][column]+1; printf(&quot;[%d][%d] = %d\n&quot;,row,column,exam_score[row][column]);   } } } //Program Output [0][0] = 71 [0][1] = 81 [0][2] = 91 [0][3] = 73 [1][0] = 83 [1][1] = 93 [1][2] = 72 [1][3] = 82 [2][0] = 92 [2][1] = 68 [2][2] = 79 [2][3] = 99

More Related Content

PPTX
Network Security
PPTX
secure socket layer
PPT
Chap3 liaison de données
PPT
PPT
Encryption And Decryption
 
PPTX
Ethernet
PDF
The MD5 hashing algorithm
Network Security
secure socket layer
Chap3 liaison de données
Encryption And Decryption
 
Ethernet
The MD5 hashing algorithm

What's hot (20)

PPTX
Merkle Trees and Fusion Trees
PPTX
Data communication and network Chapter -1
PDF
Cours informatique éléments de présentation - 8ème
PDF
Examen principal + Correction ASD
PPTX
Cryptography and Encryptions,Network Security,Caesar Cipher
PPTX
TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)
PPT
12 symmetric key cryptography
PPTX
Tcp header/IP Header/Authentication header
PPTX
What is Socket?
PPT
Symmetric and Asymmetric Encryption.ppt
PPTX
CRYPTOGRAPHY & NETWORK SECURITY - unit 1
PPT
Ip addressing classful
PDF
Physical Layer- Transmission Media.pdf
PPTX
Diffie hellman key exchange algorithm
PPTX
Secure communication
PPSX
Secure socket layer
PPTX
Diffie Hellman Key Exchange
PPT
Cryptography
PPTX
Bluetooth protocol
PPT
IDS and IPS
Merkle Trees and Fusion Trees
Data communication and network Chapter -1
Cours informatique éléments de présentation - 8ème
Examen principal + Correction ASD
Cryptography and Encryptions,Network Security,Caesar Cipher
TCP & UDP ( Transmission Control Protocol and User Datagram Protocol)
12 symmetric key cryptography
Tcp header/IP Header/Authentication header
What is Socket?
Symmetric and Asymmetric Encryption.ppt
CRYPTOGRAPHY & NETWORK SECURITY - unit 1
Ip addressing classful
Physical Layer- Transmission Media.pdf
Diffie hellman key exchange algorithm
Secure communication
Secure socket layer
Diffie Hellman Key Exchange
Cryptography
Bluetooth protocol
IDS and IPS
Ad

Viewers also liked (20)

PPTX
One Dimentional Array
PPTX
Array in c language
PPT
Selection Control Structures
PPT
PPT
Ch3 selection
PPT
Ch8 file processing
PPT
Ch3 repetition
PPT
Ch6 pointers (latest)
DOCX
Question bank
PPT
Ch4 functions
PPT
10 high speedla-ns
PDF
BMS 5th SEM Question Paper:-Logistic & SCM
PPTX
6 arrays injava
PDF
Java question bank
PPT
Ch7 structures
PPT
Ch2 introduction to c
PPT
PPT
6 data linkcontrol
PPT
Ch1 principles of software development
PPT
8 spread spectrum
One Dimentional Array
Array in c language
Selection Control Structures
Ch3 selection
Ch8 file processing
Ch3 repetition
Ch6 pointers (latest)
Question bank
Ch4 functions
10 high speedla-ns
BMS 5th SEM Question Paper:-Logistic & SCM
6 arrays injava
Java question bank
Ch7 structures
Ch2 introduction to c
6 data linkcontrol
Ch1 principles of software development
8 spread spectrum
Ad

Similar to Ch5 array nota (20)

PPTX
Arrays 1D and 2D , and multi dimensional
PPTX
one dimentional array on programming with C
PPT
Array THE DATA STRUCTURE. ITS THE STRUCT
PPT
PPTX
Array ppt you can learn in very few slides.
PPTX
array ppt of c programing language for exam
PPTX
Arrays in c
PDF
Arrays-Computer programming
PPT
presentation_arrays_1443553113_140676.ppt
PPTX
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
PDF
Homework Assignment – Array Technical DocumentWrite a technical .pdf
PPT
Arrays
PPT
C array 1 and 2 dimension array, declaration.ppt
PPT
Arrays in c
PDF
SlideSet_4_Arraysnew.pdf
PDF
SPL 10 | One Dimensional Array in C
PPTX
PDF
Array in C.pdf
PDF
Array.pdf
Arrays 1D and 2D , and multi dimensional
one dimentional array on programming with C
Array THE DATA STRUCTURE. ITS THE STRUCT
Array ppt you can learn in very few slides.
array ppt of c programing language for exam
Arrays in c
Arrays-Computer programming
presentation_arrays_1443553113_140676.ppt
Array.pptx Array.pptxArray.pptx Array.pptxArray.pptxArray.pptx
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Arrays
C array 1 and 2 dimension array, declaration.ppt
Arrays in c
SlideSet_4_Arraysnew.pdf
SPL 10 | One Dimensional Array in C
Array in C.pdf
Array.pdf

More from Hattori Sidek (17)

PPT
Chapter 4 frequency modulation
PPT
Chapter 3 am receivers
PPT
12 wireless la-ns
PPT
11 circuit-packet
PPT
7 multiplexing
PPT
5 digital datacomm
PPT
4 signal encodingtechniques
PPT
3. transmission media
PPT
2[1].1 data transmission
PPT
14 congestionin datanetworks
PPT
01 pengenalan
PPT
01 berkenalan
PPT
Chapter 2 amplitude_modulation
PPT
Comm introduction
PPT
Chapter5 dek3133
PPT
Chapter 6 edit
PPT
Chapter 6 dc motor speed control
Chapter 4 frequency modulation
Chapter 3 am receivers
12 wireless la-ns
11 circuit-packet
7 multiplexing
5 digital datacomm
4 signal encodingtechniques
3. transmission media
2[1].1 data transmission
14 congestionin datanetworks
01 pengenalan
01 berkenalan
Chapter 2 amplitude_modulation
Comm introduction
Chapter5 dek3133
Chapter 6 edit
Chapter 6 dc motor speed control

Recently uploaded (20)

PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Classroom Observation Tools for Teachers
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Microbial diseases, their pathogenesis and prophylaxis
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
2.FourierTransform-ShortQuestionswithAnswers.pdf
Classroom Observation Tools for Teachers
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
O5-L3 Freight Transport Ops (International) V1.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Ch5 array nota

  • 1. Chapter 5 : ARRAY 5.1 Introduction 5.2 One Dimensional Array 5.2.1 Declaring Array 5.2.2 Array Initialization 5.2.3 Array Referencing 5.2.4 Array to Function 5.3 Two Dimensional Array 5.3.1 Two Dimensional Array Declaration 5.3.2 Two Dimensional Array Initialization 5.3.3 Two Dimensional Array Referencing
  • 2. 5.1 Introduction Array is a group of memory locations related by the fact that they all have the same name & the same type . group together Data items a single composite data structure called array
  • 3. 5.1 Introduction (Cont..) Example 1 : To compute the average of exam score for 5 students, we need to come up with 5 identifiers. Declaration : int exam_score1, exam_score2, exam_score3, exam_score4, exam_score5; Memory allocation : exam_score1 exam_score2 exam_score3 exam_score4 exam_score5
  • 4. 5.1 Introduction (Cont..) Better use only 1 identifier to represent all 5 exam scores & an index to specify the student to whom the exam score belongs. By using an array. Declaration : int exam_score[5]; Memory allocation : exam_score[0] exam_score[1] exam_score[2] exam_score[3] exam_score[4] Note : The enumeration of elements(array index/subscript) within an array always start with 0 not 1. If the array has N elements, the last elements is in position N-1.
  • 5. 5.2 One Dimensional Array A single dimensional array Terms : Array a collection of data items of the same name & data type. Array name name of array that is an identifier. Array element Maximum no./size of array that can be stored in array. Array subscript/Index a value/expression enclosed in brackets after the array name, specifying which array element to access. Must start with 0.
  • 6. 5.2.1 Declaring Array When declaring arrays, we have to specify : Type of array Array Name Number of elements Format : data_type array_name[ number_Of_Elements ]; Example : int exam_score[28]; Array size Array name Array type
  • 7. 5.2.1 Declaring Array (cont).. Another Examples : i) int c[10]; ii) float myArray[ 3284 ]; iii) int a[5]; /* use a[0], a[1], .., a[4] */ iv) char b[3] ; /* use b[0], b[1], b[2] */ v) float c[2] = { .1, .2}; /*initialization at declaration.c[0] = 0.1 , c[1]=0.2 */ vi) double c[3][5]; /* two dimensional array
  • 8. 5.2.1 Declaring Array (cont).. • Declaring multiple arrays of same type – Format similar to regular variables Example : int b[100], x[27]; char car[23], lorry[45]; float duck[3], ant[9];
  • 9. 5.2.2 Array Initialization Array can be initialized at compile time and run time. (i) : Compile-Time Initialization of Arrays (while declaring) An initializer must be a constant value. Example : int exam_score[4] = { 88, 85, 76, 91 }; 4 initializers, therefore 4 element array We also can declare the no.of element implicitly. Example : int exam_score[ ] = {88,85,76,91} first element is refer as exam_score[0] third element is refer as exam_score[2]
  • 10. 5.2.2 Array Initialization (cont..) (ii) : Run-Time Initialization of Arrays An array can be initialize at run time explicitly. Example : a) for (int i = 0; i < 27; i++) exam_score[i] = 0.0; b) for( int b = 0; b < 27; b++) scanf(“%d”, &exam_score[b]);
  • 11. 5.2.2 Array Initialization (cont…) In addition, if we leave some values of initialization, rightmost elements will become 0. Example : int exam_score[4] = {88,85}; -> the 3 rd & 4 th element are 0 int exam_score[5] = {0};  All elements are 0
  • 12. 5.2.3 Array Referencing Each individual element is referred by specifying array name & index. Format : arrayname [ position number ] Example : (i) exam_score[0] = 89; printf( &quot;%d&quot;, exam_score[ 0 ] ); (ii) float x[ ] = { 4.05,3.45,3.21,6.55,10.23} for( int a = 0; a <= 4; a++) printf( “ element for index %d = %f\n”, a, x[a]); //output for the fragment code : element for index 0 = 4.05 element for index 1 = 3.45 element for index 2 = 3.21 element for index 3 = 6.55 element for index 4 = 10.23
  • 13. 5.2.3 Array Referencing(cont…) index  0 1 2 3 4 x x[0] x[1] x[2] x[3] x[4] Name of array ( all elements of this array, have the same name, x ) Position number of the element within array x “ array subscript” 4.05 3.45 3.21 6.55 10.23
  • 14. 5.2.3 Array Referencing(cont…) Manipulation Statement using array : Perform operations in subscript (index). Example : double Mark [8];   Mark [0] Mark[1] Mark[2] Mark[3] Mark [4] Mark [5] Mark [6] Mark [7] printf (“ %.f ”, Mark[0]); Mark[3] =25.0; sum= Mark[0] + Mark[1]; sum += Mark[2]; Mark[3] += 1.0; Mark[2] = Mark [0] + Mark [1]; 16.0 12.0 6.0 8.0 2.5 12.0 14.0 10.5
  • 15. 5.2.4 Passing Array to Function • 2 ways to pass array to a function: (i) pass individual element (call by value) (ii) pass the whole elements (call by reference) pass individual element (call by value) Pass individual element of an array. Pass a value parameter. Only the n element is passed by using an indexed expression (i.e., diod[2] )
  • 16. 5.2.4 Passing Array to Function (cont..) Example Program 5.4 //passing individual elements #include<stdio.h> void print_third_diod (double d ); void main( ) { double diod[5]={3.3,3.9,4.7,5.1,6.2}; print_third_diod (diod[2]); } void print_third_diod (double d) { printf(&quot;The value of the third diod is %.1f\n&quot;,d); } //Program Output The value of the third diod is 4.7 Pass the value of diod[2] , 4.7 to function print_third_diod Copy the value of diod[2] from the main function
  • 17. 5.2.4 Passing Array to Function (cont..) (ii) pass the whole elements (call by reference) Passing the whole array. Passing only the name of array. Name of array is address of first element – Function knows where the array is stored • At function definition, formal parameter must be an array type. Size of array does not need to be specified. (i.e., float count_average(float r[ ] ) ).
  • 18. 5.2.4 Passing Array to Function (cont..) Example Program 5.5 //Passing the whole array #include<stdio.h> float count_average(float r[]); void main( ){ float average_relay; float relay[5]={5,6,9,12,24}; average_relay=count_average (relay) ; printf(&quot;The average of 5 relays is %.2f\n&quot;,average_relay); } float count_average( float r[ ] ) { float total=0; for(int i=0;i<5;i++) total+=r[i]; return(total/(float)5); } // Program Output The average of 5 relays is 11.20 Entire array passed to function count_average relay[0] relay[1] relay[2] relay[3] relay[4]
  • 19. 5.2.4 Passing Array to Function (cont..) Another example : #include<stdio.h> void print_mark( int [ ], int, float); // Function prototype for print_mark void main( ) { int mark[4]; float average; mark[0] = 25; mark[1] = 35; mark[2] = 20; Entire array passed call by reference mark[3] = 20; average = (mark[0] + mark[1] + mark[2] + mark[3])/ 4; print_mark( mark, mark[2], average); } void print_mark( int m[], int a, float b) { printf(“Mark[2] is %d\n”, a); printf(“Average for 4 marks is %f\n”, b); } Call Function print_mark Send the value for mark[2] ,20 to function print_mark
  • 20. 5.3 Two Dimensional Array Array of array. 2-D array have 2 subscripts showing the no. of rows & the no. of column. 5.3.1 Two Dimensional Array Declaration Similar with 1-D declaration except it have 2 subscripts. Format : Data_type array_name [no_of_element1][no_of_element2] data_type : either int, float, double, char. array_name : name of array that are an identifier. no_of_elements1 : size of array row. no_of_elements 2 : size of column.
  • 21. 5.3.1 Two Dimensional Array Declaration(cont..) Example : (i) int x[10][5]; /*allocate 50 spaces (row:0-9,column:0-4)*/ (ii) float a[3][20]; /*allocate 60 spaces (row:0-2,column:0-19)*/ (iii) int exam_score[3][4]; /*allocate 12 spaces (row:0-2,column:0-3)*/
  • 22. 5.3.1 Two Dimensional Array Declaration(cont..) Example : int exam_score[3][4]; row1 row2 row3 column1 column2 column3 column4 array name row subscript column subscript row [0]-[2] column [0]-[3] exam_score[2][3] exam_score[2][2] exam_score[2][1] exam_score[2][0] exam_score[1][3] exam_score[1][2] exam_score[1][1] exam_score[1][0] exam_score[0][3] exam_score[0][2] exam_score[0][1] exam_score[0][0]
  • 23. 5.3.1 Two Dimensional Array Declaration(cont..) The right index of an array is increased first before the left index. Example : int exam_score[3][4]={70,80,90,72,82,92,71,81,91,67,78,98 }; exam_score[0][0] exam_score[0][1] exam_score[0][2] exam_score[0][3] exam_score[1][0] exam_score[1][1] exam_score[1][2] exam_score[1][3] exam_score[2][0] exam_score[2][1] exam_score[2][2] exam_score[2][3] 98 78 67 91 81 71 92 82 72 90 80 70
  • 24. 5.3.2 Two Dimensional Array Initialization We can initialize 2-D array at compile time and run time. : Compile-Time Initialization of Arrays (while declaring) Example : (i) int exam_score[3][4]={70,80,90,72,82,92,71,81,91,67,78,98}; The best way : nest the data in braces to show the exact nature of the array. Example : (ii) int exam_score[3][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}};
  • 25. 5.3.2 Two Dimensional Array Initialization(cont…) also can declare the far left dimension size implicitly. Example : (iii) int exam_score[ ][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}}; If we leave some values out of each row, compiler initialize them to 0. Example : (iv) int exam_score[3 ][4]={{70,80},{82,92,71},{91,67,78,98}};
  • 26. 5.3.2 Two Dimensional Array Initialization(cont..) : Run-Time Initialization Example : int exam_score[3][4]; //array 3x4 array can be initialized by nested for loops. The 1 st loop varies the row from 0 - 2. The 2 nd loop varies the column from 0 - 3. for (row=0;row<3;row++) for(column=0; column<4; column++) scanf(“%d”,&exam_score[row][column]); //end of for //end of for run the program, enter 12 values for the elements & they are stored in the appropriate locations.
  • 27. 5.3.3 Two Dimensional Array Referencing by specifying array name & its row&column index. Can access elements in array, using nested for . Example : exam_score[0][2] = 99; exam_score[1][4]= exam_score[0][3]+10;
  • 28. 5.3.3 Two Dimensional Array Referencing (cont…) Example Program 5.8 #include<stdio.h> void main( ) { int exam_score[3][4]={{70,80,90,72},{82,92,71,81},{91,67,78,98}}; for(int row=0; row<3; row++) { for(int column=0; column<4; column++){ exam_score[row][column]= exam_score[row][column]+1; printf(&quot;[%d][%d] = %d\n&quot;,row,column,exam_score[row][column]); } } } //Program Output [0][0] = 71 [0][1] = 81 [0][2] = 91 [0][3] = 73 [1][0] = 83 [1][1] = 93 [1][2] = 72 [1][3] = 82 [2][0] = 92 [2][1] = 68 [2][2] = 79 [2][3] = 99