< BACK TO 1D Arrays

Introduction To One Dimensional Arrays

Published 24 October 2022

What is an array ? So to understand this concept let's take an example of a chocolate box. So you must have seen a chocolate box , which is having these chocolates seperated by the partitions.So when we the chocolate box a number of things comes into our mind like.

  • It is a collection of (box) chocolates
  • All the partitions which are holding this chocolates are adjacent or we can say that they are contiguous, because they are very next to each other.
  • Here each partitions has two neighbours except the first and the last one. For example, the second partition has one neighbour to the left and one neighbour to the right. But the first and the last one, they have only one neighbour.
  • Now when we receive the pack of chocolates, the size of the box is fixed and cannot be changed or modified. So any chocolate making company will first create this box and then based on how many chocolates they want to add they will create that many partitions into the box. And once this box is created it cannot be modified.
  • Also these partitions are very adjacent to each other. Therefore they are also indexed and can be determined by its position. So simply by looking at the box you can say which indexed number chocolate you want.

So simillar kind of analogy can be applied for understanding the concept of the 1D arrays.

So here you can see that this is an array having four elements. So we can say that it is a collection of data elements of a particular type. And here you can see that all the partitions which are holding this data have contiguous memory locations so they are very next to each other. you can also see that those partitions have two neighbours except the first and the last one.

Now for creating an array we need to provide the size of the array. And once the array is created it cannot be modified or change further. So if you want a dynamic array, there is some complex logic behind it, which we will see in further blog-post.

Now here each partition has indexed because they are in the contiguous memory. So for an array of four elements, usually the index starts with zero and ends with the number of elements inside the array -1. (lenght of array - 1.).

So here we are having 4 elements in an array so here the length of the array is 4. And the indexing starts with zero so here the last indexed will have the number 3 as lenght of array - 1 i.e (4 - 1) = 3.

This indexing of array helps us in accessing the values that is inside the container. So we can simple do like arr[3] for getting the element at the third index of an array.


< Previous Post

Blog title

Next Post >

Blog title