Everything I do or study, I would like to be able to explain to my 8 or 6 year old children. While dusting my bookshelf, I found an old book that spoke about different data structures in a lot of detail including code snippets from the 90’s. As I read through it, it made complete sense to me until my 6 year old came and asked me what I was reading. I tried explaining in layman terms but realized that what I spoke might not have made sense to her.

Arrays in simple terms

Parking LotOn a sunny afternoon, I reached the parking lot in the mall. To my dismay, I went round and round in circles searching for a spot to free up. Suddenly, I found myself smiling and to my surprise, I also found a parking for my electric car. As we walked towards the mall from the parking lot, I explained the concept of arrays to my young kids.

 

 

Arrays like “parking lots” have fixed sizes. The size is defined during creation. Early in the morning, the parking lot is empty. As cars come to the parking lot, they occupy space and fill up available spaces.
2-Parking Lot Array

Each car that comes in to occupy a space is known as insertion of an element in an array. There is a maximum limit to the number of cars that can be parked in the parking lot which is defined during creation.

3-Array Insertion

Whenever a car leaves its location, the space is freed up. This is known as deletion of an element from an array.

4-Array Deletion

Syntax

Creation:
Datatype ArrayName[size]

Insertion
For temp = 1 to size

Insert[temp] = SomeCoolValue

Deletion
Delete[position]

 

Multi-dimensional arrays

Usually the parking lots in an airport are available on many different floors, and locations. An array which has many rows and columns is called a multi-dimensional array.

5-MultiDimensional Array

Syntax:
Datatype ArrayName[row, column]

int[,] MultidimensionalArray =
    new int[2, 4]{ 
        {1, 2, 3, 4}, {5, 6, 7, 8} 
    };

 

Jagged Arrays or Array of arrays

A jagged array is an array whose elements are arrays. The elements of a jagged array can be different dimensions and sizes.

Syntax: Datatype[,..] ArrayName[,..]

int[, ,] JaggedArray = new int[5, 4, 2];

jaggedArray[0] = new int[] { 1, 3, 5, 7, 9 };
jaggedArray[1] = new int[] { 0, 2, 4, 6 };
jaggedArray[2] = new int[] { 11, 22 };

 

Problem on an array

In an array, find the largest and smallest number

Input: InputArray(5) = {5, 4, 1, 6, 3}

Logic:

//Find the largest and smallest element in an array
static void PrintLargestSmallest(int[] inArray)
{
    int small = inArray[0];     //define variable for smallest #
    int large = inArray[0];    //define variable for largest #
    int len = inArray.Length;   //length of array
 
    //Loop through all the values
    for (int i = 1; i < len; i++)
    {
        if (inArray[i] > large)  //Check if the array value is > large
            large = inArray[i];  //update the large variable
        else if (inArray[i] < small) //Check if the array value is < small
            small = inArray[i];   //update the small variable              
    }
 
    Console.WriteLine("Largest # is: " + large);
    Console.WriteLine("Smallest # is: " + small);
}

 

Output iterating through sample values:

  1. Initially,                  Smallest=5; Largest=5
  2. First iteration,      Smallest=4; Largest=5
  3. Second iteration, Smallest=1; Largest=5
  4. Third iteration,    Smallest=1; Largest=6
  5. Fourth iteration,  Smallest=1; Largest=6

 

Now dear readers, this was a simple problem with 5 values. What happens if you have 1000 values or a million values? Do you think arrays would be a good choice for it?

The interviewer always wants to know which data structure is most suited for what type of data set. What are the places that arrays make best sense to use versus other data structures?

Ready to improve your online presence?

Contact us today to start the process to design your online presence

Contact Us