Chapter 5 : ARRAY 5.1 Introduction  5.2 One Dimensional Array 5.2.1 Declaring Array   5.2.2  Array Initialization   5.2.3 ...
5.1  Introduction <ul><li>Array is a group of memory locations related by the fact that they all have the same  name  & th...
5.1  Introduction (Cont..) <ul><li>Example 1  : </li></ul><ul><li>To compute the average of exam score for 5 students, we ...
5.1  Introduction (Cont..) <ul><li>Better use only 1 identifier to represent all 5 exam scores & an index to specify the s...
5.2 One Dimensional Array <ul><li>A single dimensional array </li></ul><ul><li>Terms  : </li></ul><ul><li>Array   </li></u...
5.2.1   Declaring Array <ul><li>When declaring arrays, we have to specify : </li></ul><ul><li>Type of array </li></ul><ul>...
5.2.1   Declaring Array (cont).. <ul><li>Another Examples  : </li></ul><ul><li>i) int c[10]; </li></ul><ul><li>ii) float m...
5.2.1 Declaring Array (cont).. <ul><li>•  Declaring multiple arrays of same type </li></ul><ul><li>–  Format similar to re...
5.2.2 Array Initialization <ul><li>Array can be initialized at compile time and run time. </li></ul><ul><li>(i) :  Compile...
5.2.2 Array Initialization (cont..) <ul><li>(ii) :  Run-Time Initialization of Arrays </li></ul><ul><ul><li>An array can b...
5.2.2 Array Initialization (cont…) <ul><li>In addition, if we leave some values of initialization,  </li></ul><ul><li>righ...
5.2.3 Array Referencing <ul><li>Each individual element is referred by specifying array name & index. </li></ul><ul><li>Fo...
5.2.3 Array Referencing(cont…) <ul><li>index     0 1 2 3 4 </li></ul><ul><li>x </li></ul><ul><li>x[0]  x[1]  x[2]  x[3]  ...
5.2.3 Array Referencing(cont…)   <ul><li>Manipulation Statement using array : </li></ul><ul><li>Perform operations in subs...
5.2.4  Passing Array to Function <ul><li>•  2 ways to pass array to a function: </li></ul><ul><li>(i) pass individual elem...
5.2.4  Passing Array to Function (cont..) <ul><li>Example Program 5.4 </li></ul><ul><li>//passing individual elements </li...
5.2.4  Passing Array to Function (cont..) <ul><li>(ii)  pass the whole elements (call by reference) </li></ul><ul><li>Pass...
5.2.4  Passing Array to Function (cont..) <ul><li>Example Program 5.5 </li></ul><ul><li>//Passing the whole array </li></u...
5.2.4  Passing Array to Function (cont..)   <ul><li>Another example : </li></ul><ul><li>#include<stdio.h> </li></ul><ul><l...
5.3  Two Dimensional Array <ul><ul><ul><li>Array of array. </li></ul></ul></ul><ul><ul><ul><li>2-D array have 2 subscripts...
5.3.1 Two Dimensional Array Declaration(cont..) <ul><li>Example  : </li></ul><ul><li>(i) int x[10][5]; /*allocate 50 space...
5.3.1 Two Dimensional Array Declaration(cont..) <ul><li>Example  : </li></ul><ul><li>int exam_score[3][4];  </li></ul>row1...
5.3.1 Two Dimensional Array Declaration(cont..) <ul><li>The right index of an array is increased first before the left ind...
5.3.2 Two Dimensional Array Initialization <ul><li>We can initialize 2-D array at compile time and run time. </li></ul><ul...
5.3.2 Two Dimensional Array Initialization(cont…) <ul><li>also can declare the far left dimension size implicitly. </li></...
5.3.2 Two Dimensional Array Initialization(cont..) <ul><li>: Run-Time Initialization </li></ul><ul><li>Example  : </li></u...
5.3.3 Two Dimensional Array Referencing <ul><li>by specifying  array name & its row&column index.  </li></ul><ul><li>Can a...
5.3.3 Two Dimensional Array Referencing (cont…) Example Program 5.8 #include<stdio.h> void main( ) { int exam_score[3][4]=...
Upcoming SlideShare
Loading in...5
×

Ch5 array nota

1,222

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,222
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
73
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ch5 array nota

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

    Clipping is a handy way to collect important slides you want to go back to later.

×