Programming In C Arrays Objective of these slides

Programming In C Arrays Objective of these slides: n to introduce arrays Programming In C 1

. 1 What is an Array? Collection of same data type elements. n An array is a data structure which can hold a sequence of values. n The array c[] holds 3 integer values: n c[0[ 45 - c[1[ 6 c[2[ 72 Programming In C 2
![. 2 Declaring an Array n An array is declared as. Data-type array-name [size]; . 2 Declaring an Array n An array is declared as. Data-type array-name [size];](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-3.jpg)
. 2 Declaring an Array n An array is declared as. Data-type array-name [size]; n E. g. int marks[10]; Data type Array-name Array size enclosed in brackets • This statement defines an int array named marks that can store marks of 10 students. • Since only a single subscript is used so it represents one-dimensional array Programming In C 3
![int b[100], x[27]; char sent[125; [ n n n b[] holds 100 integers (in int b[100], x[27]; char sent[125; [ n n n b[] holds 100 integers (in](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-4.jpg)
int b[100], x[27]; char sent[125; [ n n n b[] holds 100 integers (in b[0] to b[99([ x[] holds 27 integers (in x[0] to x[26([ sent[] holds 125 characters (in sent[0] to sent[124([ Programming In C 4

Memory allocation to an array The storage space allocated to any array is equal to the maximum no. of elements multiplied by the type of data type used. n All arrays are stored in contiguous 2 bytes memory locations. a[0] 10 n § e. g. int a[5] ; a[1] 21 a[2] 12 a[3] 14 a[4] 11 10 bytes Representation of a array Programming In C 5

3. Initialization of an array n n Definition of an array doesn’t automatically results in initialization of array elements. It only allocates contiguous memory to an array with each element assigned some garbage value. An array can be initialized using an equal to (=) sign. E. g. int a[3]={21, 19, 5}; a[0 [ a[1 ] a[2 ] Programming In C 6
![n other valid statements : n int a[ ] ={21 , 19 , 5}; n other valid statements : n int a[ ] ={21 , 19 , 5};](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-7.jpg)
n other valid statements : n int a[ ] ={21 , 19 , 5}; n int a[4] = { 21 , 3 }; [ a[0] =21, a[1]=3, a[2]=0, a[3]=0] n Invalid statements : n int a[3] = {2 , 4 , 3 , 1}; n int a[4] = { , 2 , 4 , 1}; Programming In C 7

. 4 An Array Program #include <stdio. h> notice the range int main() { int n[10], i; for (i = 0; i < 10; i++) n[i] = 0; printf("%s%13 sn", "Element", "Value"); for(i = 0; i < 10; i++) printf("%7 d%13 dn", i, n[i]); return 0; { Programming In C 8

Output: Element 0 1 2 : 9 Value 0 0 0 : 0 Programming In C 9

Types of array n Two types. Linear array n Non- linear array n n Linear array One dimensional arrays are called linear array. n Only one subscript is used. n Syntax: data-type array-name [size]; where data-types can be integer, float, double, char. n e. g. int x[5]; float salary[20]; Programming In C n 10

Non-linear arrays n Non – linear arrays are further subdivided into following categories: n n Two dimensional array Three dimensional array N dimensional array 2 D arrays n n Two subscripted arrays. These arrays are in row and column form. Syntax: data-type array-name [row-size] [columnsize]; E. g. int a[5][2]; float b[3][3]; Programming In C 11

3 D arrays Also called space array. n It has three subscripts. n Syntax: n data-type array-name [space size] [row size] [column size]; e. g. int s[5][2][3]; Programming In C 12

Two-dimensional Arrays. 1 Declaring 2 D Arrays 2 D Array Initialisation. 2. 3 Using a 2 D Array Programming In C 13
![. 1 Declaring 2 D Arrays int b[3][4[ n The first subscript is the . 1 Declaring 2 D Arrays int b[3][4[ n The first subscript is the](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-14.jpg)
. 1 Declaring 2 D Arrays int b[3][4[ n The first subscript is the number of rows (3), the second subscript is the number of columns (4. ( Col 0 Col 1 Col 2 Col 3 Row 0 b[0][0[ b[0][1[ b[0][2[ b[0][3[ Row 1 b[1][0[ b[1][1[ b[1][2[ b[1][3[ Row 2 b[2][0[ b[2][1[ b[2][2[ b[2][3[ Programming In C 14
![2 D Array Initialisation. 2 n Many alternatives: int a[2][3] = {{1, 2, 3}, 2 D Array Initialisation. 2 n Many alternatives: int a[2][3] = {{1, 2, 3},](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-15.jpg)
2 D Array Initialisation. 2 n Many alternatives: int a[2][3] = {{1, 2, 3}, {4, 5, 6; {{ n Indexing is by rows, so a[0] row initialised with {1, 2, 3. { int a[2][3] = {1, 2, 3, 4, 5, 6; { int a[][3] = {{1, 2, 3}, {4, 5, 6; {{ Programming In C 15
![Initialisation of parts int b[2][2] = {{1}, {3, 4; {{ n b[0][1] is assigned Initialisation of parts int b[2][2] = {{1}, {3, 4; {{ n b[0][1] is assigned](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-16.jpg)
Initialisation of parts int b[2][2] = {{1}, {3, 4; {{ n b[0][1] is assigned 0 b[0][0] = 1 b[1][0] = 3, b[1][1] = 4 Programming In C 16
![Initialisation with code int a[3][4]; : for (row = 0; row < 3; row++) Initialisation with code int a[3][4]; : for (row = 0; row < 3; row++)](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-17.jpg)
Initialisation with code int a[3][4]; : for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) a[row][col] = row * col; Programming In C 17
![3. Using a 2 D Array #include <stdio. h> int sum(int a[][]); int main() 3. Using a 2 D Array #include <stdio. h> int sum(int a[][]); int main()](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-18.jpg)
3. Using a 2 D Array #include <stdio. h> int sum(int a[][]); int main() { int scores[3][5] = {0}; int tot; : /* scores gets some values */ tot = sum(scores); printf("Total score is %dn", tot); return 0; { Programming In C continued 18
![int sum(int a[][5]) /* sum all the elements of the array */ { int int sum(int a[][5]) /* sum all the elements of the array */ { int](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-19.jpg)
int sum(int a[][5]) /* sum all the elements of the array */ { int i, j, sum = 0; for (i = 0; i < 3; ++i) for (j = 0; j < 5; ++j) sum += a[i][j]; return sum; { Programming In C 19

Three-dimensional Arrays. 1 Declaring 3 D Arrays 3 D Array Initialisation. 2. 3 Using a 3 D Array Programming In C 20
![. 1 Declaraing 3 D Arrays int a[2][2][3] = {{1, 1, 0}, {{3, 0, . 1 Declaraing 3 D Arrays int a[2][2][3] = {{1, 1, 0}, {{3, 0,](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-21.jpg)
. 1 Declaraing 3 D Arrays int a[2][2][3] = {{1, 1, 0}, {{3, 0, 0}, n n n { {2, 0, 0}}, {4, 4, 0; {{{ The first inner brackets refer to the first subscript. The next inner brackets refer to the second subscript. The three values inside each of the inner brackets refer to final subscript. Programming In C 21
![a[0][1][2] = 2 2 0 1 X Z 1 0 Y Programming In C a[0][1][2] = 2 2 0 1 X Z 1 0 Y Programming In C](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-22.jpg)
a[0][1][2] = 2 2 0 1 X Z 1 0 Y Programming In C 22
![3 D Array Initialisation. 2 int a[2][2][3] = {0}; /* everything to 0 */ 3 D Array Initialisation. 2 int a[2][2][3] = {0}; /* everything to 0 */](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-23.jpg)
3 D Array Initialisation. 2 int a[2][2][3] = {0}; /* everything to 0 */ Using code: int a[3][4][5]; : for (row = 0; row < 3; row++) for (col = 0; col < 4; col++) for (dep = 0; dep < 5; dep++) a[row][col][dep] = row*col*dep; Programming In C 23
![. 3 Using a 3 D Array #include <stdio. h> int sum_coords(int a[][][]); int . 3 Using a 3 D Array #include <stdio. h> int sum_coords(int a[][][]); int](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-24.jpg)
. 3 Using a 3 D Array #include <stdio. h> int sum_coords(int a[][][]); int main() { int orbit[10][20][30] = {0}; int points; : points = printf(" return 0; /* initialise orbit array */ sum_coords(orbit); Orbit points sum: %dn", points); { Programming In C continued 24
![int sum_coords(int a[][20][30]) /* sum the number of points in the array */ { int sum_coords(int a[][20][30]) /* sum the number of points in the array */ {](http://slidetodoc.com/presentation_image/408ad7520737b361d94709012d5e0663/image-25.jpg)
int sum_coords(int a[][20][30]) /* sum the number of points in the array */ { int i, j, k, sum = 0; for (i = 0; i < 10; ++i) for (j = 0; j < 20; ++j) for (k = 0; k < 30; ++k) sum += a[i][j][k]; return sum; { Programming In C 25
- Slides: 25