Arrays in C with Examples.

Arrays in C with Examples.

Array is set of homogeneous elements stored in a contiguous memory location under a single variable name. Homogeneous means the data elements are of identical data types. For eg int arr[5]; Here arr is an array of 5 elements which are of integer data types. We will now unerstand entire concept of arrays in C with examples.

Note :- Array index always start at 0.

arrays in C with examples

Why do we need Arrays?

We can declare and use variables names (such as v1,v2,v3,etc) when we have very less data to store. But if we want to store a large data (for e.g. 100 elements) then declaring 100 variables will utilise large memory and become a hectic task to remember every element. In such cases, arrays makes it very easy to store and manage data efficiently.

Syntax

data_type name_of_array[array_size];

data type means whether the array stores integer, float, double, char,etc. type of data.
name of array can be anything except a name starting with a number.
array size means how many characters or numbers the array is going to store.

Eg.1. – Write a program to accept 5 numbers and display them using array.

//Array example
#include<stdio.h>

int main()
{
    int arr[5];   //array declaration
    int i;  

    printf("\n Enter 5 numbers:\n");  //Accepting array elements
    for(i = 0; i < 5; i++)
    {
        scanf("%d", &arr[i]);
    }

    printf("\n Entered numbers are:\n");
    for(i = 0; i < 5; i++)
    {
        printf("%d\t", arr[i]);     //displaying array elements (traversing)
    }

    return 0;
}

Output :-

arrays in C with examples

Explanation :-

As usual all the header files are checked. Then we enter main function. Here, we first declare an array of 5 elements. Then we ask user to enter 5 numbers. First for loop is to initialize the array. For displaying elements inside an array we use second for loop. And the program terminates.

Eg.2. – Write a program to show use of array.

//Array example
#include<stdio.h>

int main()
{
    int arr[] = {45, 67, 9, 81, 35, 12, 59};   //array declaration and initialization
    int i;  

    printf("\n Numbers inside array are:\n");
    for(i = 0; i < 7; i++)
    {
        printf("%d\t", arr[i]);     //displaying array elements (traversing)
    }

    return 0;
}

Output :-

arrays in C with examples

Explanation :-

As usual all the header files are checked. Then we enter main function. Here we declare an array but we also specify the elements for array. For displaying elements inside an array we use for loop. And the program terminates. This program completely describes the concept of arrays in C with examples.

Types of Arrays

Arrays are broadly classified into three types.

  1. One/Single dimensional arrays.
  2. Multi dimensional arrays.

1. One/Single Dimensional Arrays

A one dimensional array is a simple linear array which has only one index. For accessing elements of one dimensional array we need only oe subscript.

In this type of arrays, the starting index (i.e. index of first element) is always 0 and the index of last element is n-1 where n is the size of the array.

Syntax for declaring one dimensional array is same as normal array. E.g. int arr[10];

arrays in C with examples
Memory allocation for an array ‘arr’ of 10 integer elements.

2. Multi Dimensional Arrays

Multi dimensional arrays are used to store data that requires multiple references. For e.g. a matrix requires two references namely row number and column number. Hence, “matrix” is the best example of to dimensional arrays.

We can also have an array with more than two dimensions (i.e. three dimensional array,etc.).

Syntax for declaring a two dimensional array is

data_type name_of_array[m][n];

where m and n are integers specifying number of rows and columns

For e.g. int arr[3][3];

Arrangement of elements in 2D array.
Arrangement of elements in 2D array.

Here first bracket indicates number of rows and second bracket indicates number of column.

Memory allocation for an 2D array 'arr' of 10 integer elements.
Memory allocation for an 2D array ‘arr’ of 10 integer elements.

We can clearly see that the elements are stored in a sequential manner with all the elements of a row together one below the other, and then the next row and so on.

Eg. – Write a program to implement two dimensional array.

// Two-Dimensional array 
#include<stdio.h>
  
int main() 
{ 
    
// an array with 2 rows and 3 columns. 
    int arr[2][3] = {{0,1,2}, {3,4,5}};
  
    printf("\n Two dimensional array is: \n");
    for (int i = 0; i < 2; i++) 
    { 
        for (int j = 0; j < 3; j++) 
        { 
           printf("%d\t", arr[i][j]);
        } 
        printf("\n");
    } 
  
    return 0; 
} 

Output :-

arrays in C with examples

Explanation :-

Here we declare a two dimensional array arr[2][3]. The array consist of two rows and three columns. This two dimensional array is same as a matrix of 2 x 3. Here the first element will be stored at arr[0][0]. Second element at arr[0][1] and third at arr[0][2]. Next i.e. fourth element is stored at arr[1][0]. Fifth at arr[1][1] and sixth at arr[1][2]. To give it a proper matrix look we use nested for loop. First for loop is for number of rows and second for loop is for number of columns. And this is the way in which two dimensional arrays are stored in memory.

Advantages of Array

  • Multiple data elements are stored under a single variable name. This prevents declaration of multiple variable names for a identical data type elements.
  • Array elements can be accessed using single index number.
  • Memory is allocated in contiguous manner. This avoids memory shortage or memory management problems.
  • Two dimensional arrays are used for matrices.

Disadvantages of Array

  • We cannot change array size after declaration.
  • It stores only homogenuous data elements.
  • Insertion and Deletion operations are very difficult to perform in arrays.
  • Declaring a large array than required leads to the wastage of memory.

Applications

  • Arrays are used to implement vectors and matrices.
  • Arrays can help to implement data structures such as heap, stack, queue, etc.
  • It can be used to determine partial or complete control flow in programs.

Conclusion

This is the concept of arrays in C with examples. Same is the concept for C++. Implementing an array is very easy and it is very important concept for data structures. If you have any doubts regarding arrays just mail us at errorfreeprogram@gmail.com. And if you liked this post then do tell us in the comment section.

Also read: Pointer array/Array of pointers

For daily updates do follow us on Instagram. Visit Again!

ErrorFreeProgram.

3 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *