The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

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 :

int marks [5];

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 :

7562934988

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 :

int marks [ ] = { 75, 62, 93, 49, 88 };

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 :

#include <stdio.h>

int main() {
   int marks[5]; /* declare an array to store 5 values */
   int i; /* array index */
   int sum = 0;
   float average;
   printf("Enter the marks of 5 students :- \n");

   // read marks of 5 students
   for ( i = 0; i < 5 ; i++ ) {
      printf("Marks of student %d : ", i+1);
      scanf("%d", &marks[i]); /* input marks of student 'i' in index 'i' */
   }

   /* compute sum and average */
   for ( i = 0; i < 5 ; i++ ) {
      sum += marks[i]; /* add the value in each index of the array */
   }
   average = (float)sum / 5;

   printf("Sum : %d\n", sum);
   printf("Average : %f\n", average);
   return 0;
}
Run this program in your system to take input at run-time  


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 ) :

#include <stdio.h>

/* Display the array */
void displayArray(int *arr, int size) {
   int i;
   for ( i = 0; i < size; i++ ) {
      printf("%d  ", arr[i]);
   }
}

/* delete the element at index 'pos' 
 * Variable 'size' is passed by reference as the size of the array is changed
 * Array 'arr' is passed by reference by default   
 */
void deleteElement(int arr[], int *size, int pos) {
   int i;
   if ( *size == 0 ) { /* check for empty array */
      printf("\nArray is empty ... ");
      return;
   }
   if ( pos >= *size ) {
      printf("\nInvalid Position ...");
      return;
   }
   /* delete the element at index 'pos' */
   for ( i = pos; i < (*size)-1; i++ ) {
      /* shift all elements by 1 position towards left starting from 'pos' */
      arr[i] = arr[i+1];
   }
   (*size)--; /* update array size */
}

/* Insert the element 'k' at index 'pos' 
   Variable 'size' is passed by reference as the size of the array is changed
*/
void insertElement(int arr[], int *size, int k, int pos) {
   int i;
   if ( *size == 5 ) { /* Array has no space to insert more elements */
      printf("\nArray has no space ... ");
      return;
   }
   if ( pos >= *size ) {
      printf("\nInvalid Position ...");
      return;
   }
   /* insert the element 'k' at index 'pos' */
   for ( i = (*size)-1; i > pos; i-- ) {
      /* shift all elements by 1 position towards right till 'pos' */
      arr[i] = arr[i-1];
   }
   arr[pos] = k; /* insert the element */
   (*size)++; /* update size */
}

/* Search an element 'val' in the array
   Return the position (index) if element is found else return -1 
*/
int searchElement(int arr[], int size, int val) {
   int pos = -1, i;
   for ( i = 0; i < size; i++ ) {
      if (arr[i] == val) { /* compare each array element with 'var' */
         pos = i; /* element found */
         break; /* break out of loop */
      }
   }
   return pos;
}

int main() {
   int arr[5] = { 56, 43, 12, 9, 39 }; /* declare and initialize the array */
   int size = 5, pos;
   int element = 43;
   printf("\nOriginal Array : ");
   displayArray(arr, size);
   deleteElement(arr, &size, 2); /* delete the element at index 2  */
   printf("\nAfter Deletion : ");
   displayArray(arr, size);
   insertElement(arr, &size, 99, 2); /* insert element 99 at index 2 */
   printf("\nAfter Insertion: ");
   displayArray(&arr[0], size); /* Another way of passing array to a function */
   pos = searchElement(arr, size, element); /* search element '43' */
   if (pos != -1) {
      printf("\nElement %d is found at position %d\n", element, pos);
   }
   else {
      printf("\nElement not found ... \n");
   }
   return 0;
}

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 :

int marks [ ] = { 75, 62, 93, 49, 88 };
int *ptr = marks; // ptr points to first index of the array
int *ptr = &marks[0]; // This statement is same as the above one

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 :

#include <stdio.h>

int main() {
   int arr[5] = { 4, 7, 6, 9, 3 };
   int *ptr = arr; /* ptr holds the address of first element in arr[] */
   int *i;
   /* different ways of accessing array elements */
   printf("Value at index 0 is %d\n", arr[0]);
   printf("Value at index 1 is %d\n", 1[arr]);
   printf("Value at index 2 is %d\n", *(ptr + 2));
   printf("Value at index 3 is %d\n", *(3 + ptr));
   i = &arr[3]; /* 'i' points to index 3 of the array */
   printf("Value at 'i' is %d\n", *i);
   i++; /* 'i' now points to index 4 of the array */
   printf("Now Value at 'i' is %d", *i);
   return 0;
}


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 :

int mat [ 3 ][ 4 ];

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 :

InitializationIndexing
int mat[3][4] = { { 17, 23, 15, 19 },
                  { 44, 29, 52, 76 },
                  { 21, 63, 35, 57 } };

17 [ 0, 0 ]23 [ 0, 1 ]15 [ 0, 2 ]19 [ 0, 3 ]
44 [ 1, 0 ]29 [ 1, 1 ]52 [ 1, 2 ]76 [ 1, 3 ]
21 [ 2, 0 ]63 [ 2, 1 ]35 [ 2, 2 ]57 [ 2, 3 ]

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 :

#include <stdio.h>

int main() {
   int mat[3][3]; // matrix can have max 3 rows and 3 cols
   int i, j;
   printf("Enter the matrix elements row-wise :- \n");
   for ( i = 0; i < 3; i++ ) { // outer loop iterates over each row
      for ( j = 0; j < 3; j++ ) { // inter loop iteartes over each column
         printf("mat[ %d ][ %d ] : ", i, j);
         scanf("%d", &mat[i][j]); /* i -> row no. and j -> col no. */
      }
   }
   /* display the matrix */
   printf("You have entered the matrix :- \n");
   for ( i = 0; i < 3; i++ ) {
      for ( j = 0; j < 3; j++ ) {
         printf("%d  ", mat[i][j]);
      }
      printf("\n");
   }
   return 0;
}
Run this program in your system to take input at run-time  


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

/* 
 * This program does the following operations on a matrix 
 * 1) Calculate the sum of left and right diagonal matrix
 * 2) Print the lower and upper diagonal elements
 * For the matrix :  2 3 8 4
                     5 1 7 3
                     9 2 6 8
                     1 4 5 7
 * Sum of left diagonal elements = 2 + 1 + 6 + 7 = 16
 * Sum of right diagonal elements = 4 + 7 + 2 + 1 = 14
 * Lower diagonal matrix ( all elements below left diagonal ) : 
   5
   9 2
   1 4 5
 * Upper diagonal matrix ( all elements   above left diagonal ) : 
   3 8 4
     7 3
       8                               
*/

#include <stdio.h>

/* computes the sum of left diagonal elments */
int sumLeftDiagElements(int mat[][4], int rows, int cols) {
   int i, j, sum = 0;
   for ( i = 0; i < rows; i++ ) {
      for ( j = 0; j < cols; j++ ) {
         if ( i == j ) { /* we found a left diagonal element */
            sum += mat[i][j];
         }
      }
   }
   return sum;
}

/* computes the sum of right diagonal elments */
int sumRightDiagElements(int mat[][4], int rows, int cols) {
   int i, j, sum = 0;
   for ( i = 0; i < rows; i++ ) {
      for ( j = 0; j < cols; j++ ) {
         if ( (i + j) == (rows - 1) ) { /* we found a right diagonal element */
            sum += mat[i][j];
         }
      }
   }
   return sum;
}

/* Print the lower diagonal elements */
void lowerDiagMatrix(int mat[][4], int rows, int cols) {
   int i, j;
   for ( i = 0; i < rows; i++ ) {
      for ( j = 0; j < cols; j++ ) {
         if ( i > j ) { /* we found a lower diagonal element */
            printf("%d ", mat[i][j]);
         }
      }
      printf("\n");
   }
}

/* Print the upper diagonal elements */
void upperDiagMatrix(int mat[][4], int rows, int cols) {
   int i, j;
   for ( i = 0; i < rows; i++ ) {
      for ( j = 0; j < cols; j++ ) {
         if ( i < j ) { /* we found a upper diagonal element */
            printf("%d ", mat[i][j]);
         }
         else {
            printf("  ");
         }
      }
      printf("\n");
   }
}

int main() {
   int mat[4][4] = { { 2, 3, 8, 4 },
                     { 5, 1, 7, 3 },
                     { 9, 2, 6, 8 },
                     { 1, 4, 5, 7 } };
   int left_diag_sum, right_diag_sum;
   left_diag_sum = sumLeftDiagElements(mat, 4, 4);
   printf("Sum of Left Diagonal elements : %d\n", left_diag_sum);
   right_diag_sum = sumRightDiagElements(mat, 4, 4);
   printf("Sum of Right Diagonal elements : %d\n", right_diag_sum);
   printf("\nLower Diagonal Elements :- ");
   lowerDiagMatrix(mat, 4, 4);
   printf("\nUpper Diagonal Elements :- \n");
   upperDiagMatrix(mat, 4, 4);
   return 0;
}

Back | Next