C Programming and Data Structures (45 Blogs)

All you Need to Know about Arrays In C Programming

Published on Aug 13,2019 77 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

What is an array? Why do we need it? In this article, you will learn about the data structure array and its types and why is it a widely used data structure. Let’s get into it.

An Array is a sequential collection of elements, of the same data type. They are stored sequentially in memory. An Array is a data structure that holds a similar type of elements. The array elements are not treated as objects in c like they are in java.

Imagine you are at a musical instrument store and I tell you to arrange all the keyboards under the brand Casio at one place one above the other. This sequential collection of records is called an Array. An array is a sequential collection of elements of the same data type. In our example above,  Casio is the data type and all the keyboards you collected are of the brand Casio. All the elements in an array are addressed by a common name.

There are two types of arrays:

  1. Single dimensional array
  2. Array Initialization
  3. Accessing Array at any Point
  4. Multidimensional array

 

SINGLE DIMENSIONAL ARRAY

The syntax for declaring a Single Dimensional Array:

Datatype arrayName[arraySize];

We have a data type that can be any of the basic data types like int, float or double. Array Name is the name of the array and we declare the size of the array.

In our above example, the array will be,

Casio keyboardArray[100];

Let’s consider another example:

int test[30];

The array test will hold the elements of type int and will have a size 30.

 

ARRAY SIZE:

Array size is given at the time of declaration of the array. Once the size of the array is given it cannot be changed. The compiler then allocates that much memory space to the array.

Consider the Example:

int test[20];

In the example above, we have an array test, of type int. We have given the array size to be 20. This means that 20 consecutive memory locations will be left free for the array in the memory.

 

ARRAY INDEX AND INITIALIZATION

A number associated with each position in an array and this number is called the array index. Its starts from 0 and to the last element, that is the size of the array minus one. The minus one is there because we start counting from zero and not one. Array indices always begin from zero.

Consider this example, this is the age array.

Here the array contains the values 12,41,3,13,7 and the indices are 0,1,2,3,4. If we want to represent an element at index 4 it is represented as age[4] and the value 7 will be displayed.

By default, the array contains all zero values.

Array initialization is done at the time of declaration. This can also be carried out later if the user enters the array value as and when needed.

INITIALIZATION DURING DECLARATION:

An array can be initialized during declaration. This is done by specifying the array elements at the time of declaration. Here the array size is also fixed and it is decided by us.

 

Consider the code,

#include<stdio.h>

int main()

{

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

return 0;

}

EXPLANATION:

In the above example, we create an array of type int and with the name arr. We directly specify the array elements. The size of the array is decided by counting the number of elements in our array. In this case, the size is 4.

 

INITIALIZATION BY USER:

In this method, we let the user decide the size of the array. In this case, we need a variable to hold the size of the array and a for loop to accept the elements of the array. We assign a random size at the time of declaration and use only as needed. The size at the start is usually at the higher side. We have a variable i to control the for loop.

Consider the example,

#include<stdio.h>

int main()

{

int arr[50],n,i ;

printf("Enter the size of array:n");

scanf(“%d ”,n);

printf("Enter the elements of array:n");

for(i=0;i<n;i++)

{

            Scanf(“%d ”,arr[i]);

}

 

return 0;

}

OUTPUT:
EXPLANATION:

In the above program, we declare an array of size 50. We then ask the user to enter the number of elements he wishes to enter in this array. We then accept the array elements entered by the user.

 

DISPLAYING THE ARRAY:

Displaying the array also requires the for-loop. We traverse to the entire array and display the elements of the array.

Here is an example,

#include<stdio.h>

int main()

{

int arr[50],n,i ;

printf("Enter the size of array:n");

scanf(“%d ”,n);

printf("Enter the elements of array:n");

for(i=0;i<n;i++)

{

            Scanf(“%d ”,arr[i]);

}

printf("Array Elements are:n");

for(i=0;i<n;i++)

{

            Printf(“%d  ”,arr[i]);

}

return 0;

}

OUTPUT:
Arrays in C
EXPLANATION:

In the above program, we declare an array of size 50. We then ask the user to enter the number of elements he wishes to enter in this array. We then accept the array elements entered by the user. We then use a for loop again to display the array elements.

 

ACCESSING THE ARRAY AT ANY POINT:

Accessing array elements is simple and is done by using the array index. Have a look at the code below.

#include<stdio.h>

int main()

{

int arr[5],i ;

arr[4]=2;

arr[2]=17;

arr[0]=17;

printf("Array elements are: n");

for(i=0;i<5;i++)

{

            printf(“%d  ”,arr[i]);

}

return 0;

}

OUTPUT:
output- Arrays in C
EXPLANATION:

In the program above, we have an array of size 5. We enter elements at different locations using array index. We print the array to get the above output.

By Default, all the array elements are zero. What happens if we cross the array size?

In c, if we try to access the elements out of bound, error may not be shown by the compiler but we will not get proper output.

 

MULTI-DIMENSIONAL ARRAY:

Arrays of arrays are multi-dimensional arrays. This is because each element in a multi-dimensional array has an array of its own. We need n for loops to iterate through a multidimensional array depending on the dimensions.

 SYNTAX FOR DECLARING MULTI_DIMENSIONAL ARRAYS:

Datatype arrayname[size1][size2]…..[size n];

Consider the example,

int a[10][20];

The size of the above array will be 10*20 that is 200 elements.

Similarly, we can have two or three or even more dimensional arrays.

Each dimension requires one for loop. So, the two-dimensional array requires two- and three-dimensional array requires three.

Consider the code:

#include<stdio.h>

int main()

{

             

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

 

           

            for (int i = 0; i < 3; i++)

            {

                        for (int j = 0; j < 2; j++)

                        {

                                    printf( "Element:  %d n”,arr[i][j]);

                        }

            }

 

            return 0;

}

OUTPUT:
Arrays in C
EXPLANATION:

In the above code, we display a 3*2 matrix. This array has 3 rows and 2 columns. We have 2 for loops. Each responsible for one dimension of the array. The outer for loop takes care of rows and inner of columns.

Similarly, we can write a code for three-dimensional array and there will be three for loops and each dimension will be controlled by one for loop.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.