The Ultimate One Dimensional Array Tutorial for Beginners

Array:

An array is a variable to store a group of elements of the same datatype. An array can hold only one type of element. We can store only integer numbers into an array. We instore only characters into an array. In array we cannot store different types of elements into the same array. For example, it is not possible to store an integer, a float type number,r and a character together in the same way. An array is generally written using square braces after its name, as nav[]. In the square braces, we write the position number of the element starting from 0,1,2,3…etc.

Example: nav[3];

In above example, nav[0] represents first element, nav[1] represents second element, nav[3] represents third element. Here o,1,2.. are called subscript or index. That is the reason an array is also called ‘subscripted variable’ or ‘indexed variable’.

One dimensional array:

A 1D array represents a single row or single column of elements. For examples marks obtained by students in 5 subjects. These marks can be written as a row or as a column. So,it becomes an example for 1D array.A row represents horizontal arrangement of elements and a column represents vertical arrangements of elements.

1D array can be created in two ways. The first way is that we can declare 1D array and then store elements into that array. A 1D array can be declared with a pair of square braces after the array name as show below:

int marks[5];

Here, the array type is ‘int’. So, we can store integer type elements into this array. One pair of square braces represents that it is a 1D array. In the square braces the number 5 represents the size of the array. This means we can store up to 5 elements into the array.Currently there are no elements stored in this array. To store 5 integer type elements into this array, we can use a loop as.

for(i=0; i<5; i++)
scanf("%d",&marks[i]);

The second way of creating 1D array is to declare as well as initialize the array using a single statemtent as:

int marks[]={10,20,30,40,50} ;

Here, the size of the array is not needed. Since there are only 5 elements, the array will assume the size of 5. If we want, we can also write the size of the array as:

int marks[5]={10,20,30,40,50};

In above example, the compiler will allot 5 continuous blocks of memory and stores the elements as shown in below.

 Storage of elements in a 1D array

In above figure, marks[0] represents the first element 10, marks[1] represent the second element 60, and so on.The element position number in the array is represented by 0,1,2,3 and 4. This is called index of the array. A 1D array will have only one index that represents the element position number.

Example:char name[]= { 'Z','O','N','E','\0'};
               or
char name[5]={'Z','O','N','E','\0'};

Here, we are declaring name[] as 1D array of char type and storing the characters ZONE. While storing th characters, we stored \0 as the last character. ” represents NULL character, which represents the end of a string. Because of \0, the compiler will be able to understand where to stop when retrieving the characters from memory.

fig: Storage of elements in a character type 1D array

Note:

Array index always starts with zero.

Naveed Tawargeri
 

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.