Tuesday, 30 January 2018

Arrays in C

Arrays in C

 Introduction to Arrays
 
           Suppose you want to store the marks of 100 students of a class and then find the average. Would you declare 100 variables for that ? Definitely not. So, here comes a data structure called array which provides an utility to store a collection of elements of same data type in contiguous memory location.

Consider the declaration below :



            In the above statement, we have declared an array named marks which can store 5 values of type int. Each of the values can be accessed using an index in the array.

Suppose there are 5 values stored in the array marks as show below : 

 

             Value stored at index 0 of array marks is 75 i.e marks [ 0 ] = 75. Remember array index starts from 0. Similarly, marks [ 2 ] = 93.

The array marks can be initialized with 5 values in a single statement : 



             We see that size of the array is not specified in the above statement. Since, we have initialized the array with 5 values, an array of size 5 is automatically created. We can explicitly specify the size of the array. In that case, if we specify the size as 5, we can initialize with 5 elements only. Consider the following program which takes marks of 5 students in an array and then find the sum and average of all marks :

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------



Passing array to function

           Arrays are by default passed by reference. We can pass an array to a function either by using the array name ( arr ) or the address of the first element of the array ( &arr [ 0 ] ) .
 Similarly, we can receive the passed array either using an array ( int arr[ ] )
 [ see the insertElement( ) , deleteElement( ) , and searchElement( ) functions below ] or a pointer to an array ( int *arr ) [ see the displayArray( ) function below ] .

Consider the following program in which we implement four functions ( insert, delete, search and display ) :

----------------------------------------------------------------------------------------------------------------------------

 

----------------------------------------------------------------------------------------------------------------------------

Output :
 
Original Array : 56 43 12 9 39
After Deletion : 56 43 9 39
After Insertion: 56 43 99 9 39
Element 43 is found at position 1

----------------------------------------------------------------------------------------------------------------------------

         Sorting is another popular operation on array elements. Please refer to the sorting algorithm section to understand how different sort techniques work.

Pointer to an array

          We have seen that a pointer variable stores the address of another variable. A pointer variable can point to an array as well. Consider the following statements :

 

 We declared a pointer variable ptr which points to the first element of the array. Since array elements are stored in contiguous memory locations, all the elements can be accessed using ptr. Moreover, a pointer variable can point to any index of the array. Following program demonstrates the use of pointer to an array :
----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :
 
Value at index 0 is 4
Value at index 1 is 7
Value at index 2 is 6
Value at index 3 is 9
Value at 'i' is 9
Now Value at 'i' is 3

----------------------------------------------------------------------------------------------------------------------------

 Two Dimensional ( 2D ) Arrays

         So, far we have seen one dimensional arrays. Now we will see two dimensional array also known as matrix, which is another useful data structure.

Consider the declaration below :

 

 We have declared a two dimensional array or matrix with 3 rows and 4 columns.
 We can store 3 x 4 = 12 elements in this matrix.

Each element is stored in a cell which can be accessed using the combination of  row index and column index.

Initialiation of a 2D array is shown below :



       We can see how the above initialization statement in the left builds a matrix shown in the right. Every element is stored in a cell which has a index shown as subscript of each element. For e.g, 52 is stored in a cell with index [ 1, 2 ] where 1 denotes the row number ( index ) and 2 denotes the column number ( index ). Please note that both row and column index starts with 0.

Following program inputs a matrix from user and displays it :

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------


 Passing 2D Array to Function

         2D Arrays (matrices) are passed similar to 1D arrays but the no. of columns must be specified in the argument of the function to which a matrix has been passed. In the following program we implement four functions which takes a matrix as an argument and perform the following functions :
1) Compute sum of left diagonal elements
2) Compute sum of right diagonal elements
3) Print the lower diagonal matrix
4) Print the upper diagonal matrix

----------------------------------------------------------------------------------------------------------------------------



----------------------------------------------------------------------------------------------------------------------------

Output :
 
Sum of Left Diagonal elements : 16
Sum of Right Diagonal elements : 14

Lower Diagonal Elements :-
5
9 2
1 4 5

Upper Diagonal Elements :-
3 8 4
7 3
8

----------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment