Computer Fundamentals with C and Unix
(Nested for, Searching, Sorting)
Lecture 06: Jan. 05, 2023 Prof. K.R. Chowdhary : Professor of CS

Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.

6.1 Nested for...loops

A nested loop has one loop inside of another. These are typically used for working with two dimensions arrays and two dimensions printing. When a loop is nested inside another loop, the inner loop runs many times inside the outer loop. In each iteration of the outer loop, the inner loop will be re-started. The inner loop must finish all of its iterations before the outer loop can continue to its next iteration.

The nesting can be up to any level.


Figure 6.1: Nested for loops

6.2 Two dimensional Arrays

The two-dimensional array can be defined as an array of arrays. The 2D array is organized as matrices which can be represented as the collection of rows and columns. However, 2D arrays are created to implement a relational database look like data structure. It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required.

We declare two-D array of rows and columns by:

data_type array_name[rows][columns];

where data_type is any data type, like, int, char, float, long, etc, so that all the elements are of the same type. For example, we can declare a two dimensional array of 4 rows and there are 3 columns in each row, by the statement:

int twodimen[4][3];

This array can be initialized at the time of declaration by assigning the values directly into the array elements, by statement:

int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

The C language allows arrays of any dimensions, i.e., 3-D, 4-D, ..., n-D.

6.2.1 Printing Two Dimensional Arrays

The Fig. 6.2 shows a 4 x 3 two dimensional array declared as will as initialized with integer data sets. The nested for ... loop prints the array, and after each row is printed, it gives a line feed by printing newline character. Note that outer for loop counts rows as 0,1,2,3 and for each row the inner loop counts columns as 0, 1, 2.


Figure 6.2: Two dimensional array

6.2.2 Summing two 2-dimensional arrays

Let us consider that we are interested to initialize two 2-dimensional arrays, then sum these arrays and then print all three 2-D arrays, i.e., two original and the result obtained after summing the two arrays. The sum operation is some what like shown in Fig. 6.3.


Figure 6.3: Principle of summing Two 2-dimensional arrays

The program in Fig. 6.4 declares and initializes two arrays twod1[3][3] and twod2[3][3], each of them 3 row and 3 columns. Third array sumarr[3][3] is declared but not initialize. The first for loop in this program sums element by element of the two array, i.e., twod1[0][0] is added with twod2[0][0] and result is stored in sumarr[0][0]. Similalry for all the elements.

Next, three nested for loops prints the twod1, twod2, and sumarr arrays. The printed results are shown in Fig. 6.5.


Figure 6.4: Program to sum Two 2-dimensional arrays


Figure 6.5: Two dimensional array run output

6.3 Searching arrays

An array is made of continuous locations in the memory, hence its elements can be searched, by advancing the array index by 1, till the element of the array is found. For example, in the array,

int empno[10];

there are 10 elements, empno[0] to emp[9]. Suppose their values are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29. We are interested to search if 11 is in the array? So we check it like this:

if (empno[0] == 11)
if (empno[1] == 11)
if (empno[2] == 11)
if (empno[9] == 11)

and if condition not met in any of these then 11 is not in the array. And, if the condition is found to be true in any one of the cases, then data 11 exists in the array. This can be checked using a for loop as follows:

int found = 0; // initially not found
for(i=0; i<10; i++){
  if (empno[i] == 11){
     printf("data is found in the array\n");
 if (found == 0)
 printf("Data is not found\n");

Based on the idea discussed above, we present a program that searches in a single dimensional array, as given in the Fig. 6.6.


Figure 6.6: Searching an array