Arrays in C/C++ | Declaration and Operations

20 min read

An array is a collection of homogeneous elements stored at contiguous memory locations. Know more about arrays and types of arrays. Now let us look at Arrays in C/C++.

 

arrays-introduction

  • Array Index: The location of an element in an array has an index, which identifies the element. Array index starts from 0.
  • Array element: Items stored in an array is called an element. The elements can be accessed via its index.
  • Array Length: The length of an array is defined based on the number of elements an array can store. In the above example, array length is 6 which means that it can store 6 elements.

Normal variables (a, b, c, d….) can be used when we have a small number of elements, but if we want to store a large number of elements, it becomes difficult to manage them with normal variables. Representing many elements with one variable name is the basic idea of arrays.

 

Array Declaration in C/C++

• Declaration of an array by specifying its size

// Array declaration by specifying size 
int face[ 7 ]; 
// we can also declare an array of size specified by user 
int n = 7; 
int face[ n ];

• Declaration of an array by initializing array elements

// Array declaration by initializing the elements 
int face[ ] = { 2, 3, 5, 7, 11 }
/* An array of size 5 is created by the compiler.
It is same as "int face[4] = {10, 20, 30, 40}"*/

• Declaration of an array by specifying its size and initializing the elements

// Array declaration by specifying size and initializing the elements
int face[ 7 ] = { 2, 3, 5, 7, 11 }
/* Compiler creates an array of size 7, initializes it first
5 elements as specified as input by user and rest two elements as 0. */

 

Array Operation in C/C++

Traversal, insertion, deletion, search, and updation are the major types of operations that can be performed on arrays. Now let us look at each of these operations one by one.

Traversal

Traversal in an array means printing the elements of the array one by one. This means you are traversing through the elements of the array one after the other. A simple program for traversal in an array is shown below.

// A sample C program for traversing
# include<stdio.h>
int main()
{
int face[10] = {1,5,7,9,2,4,6,8,10,15};
int i;
printf("The array elements are \n");
for(i = 0 ; i < 10 ; i++)
{
printf("%d ",face[ i ]);
}
return 0;
}

 

OUTPUT
Traversal of arrays in C/C++

 

Insertion

Insertion operation is used to insert an element into the array. Insertion of an element can happen at the beginning, middle or at the end of the array.

// A sample C program for insertion
#include <stdio.h>
int main()
{
int face[ ] = {7,4,5,9,8,15};
int item = 10, k = 3, n = 6;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0 ; i < n ; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
n = n + 1;
while( j >= k)
{
face[ j + 1 ] = face[ j ];
j = j - 1;
}
face[k] = item;
printf("The array elements after insertion :\n");
for(i = 0; i < n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
return 0;
}

 

OUTPUT
Insertion of element in arrays in C/C++

Deletion

Deletion operation is used to delete an element from the array. A simple program to perform deletion in an array is shown below.

// A sample C program for deletion
#include <stdio.h>
int main()
{
int face[ ] = {7,4,5,9,8,15};
int k = 3, n = 6;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
j = k;
while( j < n)
{
face[ j-1 ] = face[ j ];
j = j + 1;
}
n = n -1;
printf("The array elements after deletion :\n");
for(i = 0; i<n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
return 0;
}

OUTPUT
Deletion of element in arrays in C/C++

Searching

A search operation is used to search for an element in the array. It can also be used to search for an element greater than or less than the specified element. A simple program to perform this operation is shown below.

// A sample C program for searching
#include <stdio.h>
int main()
{
int face[ ] = {7,4,5,9,8,15};
int item = 9, n = 6;
int i = 0, j = 0;
printf("The original array elements are :\n");
for(i = 0; i < n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
while( j < n)
{
if( face[ j ] == item )
{
break;
}
j = j + 1;
}
printf("Found element %d at index %d\n", item, j);
return 0;
}

 

OUTPUT

Searching of element in arrays in C/C++

 

Updating

Updating refers to updating an existing array element at a particular index value to a new element.

// A sample C program for updating
#include <stdio.h>
int main()
{
int face[ ] = {7,4,5,9,8,15};
int k = 3, n = 6, item = 17;
int i, j;
printf("The original array elements are :\n");
for(i = 0; i<n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
face[ k-1 ] = item;
printf("The array elements after updation :\n");
for(i = 0; i<n; i++)
{
printf("face[%d] = %d \n", i, face[ i ]);
}
return 0;
}

 

OUTPUT
updating-element-arrays

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error