arrays-in-data-structure-and-algorithms

Array Operations In Data Structure And Algorithms Using C Programming

Hello guys, welcome back to my blog. In this article, I will discuss array operations in data structure and algorithms using C programming, what is an array, why an array is used, programs on arrays, etc.

If you need an article on some other topics then comment us below in the comment box. You can also catch me @ Instagram – Chetan Shidling.

Also, read:

  1. Sorting In Data Structure And Algorithms, Code, Working, Types Of Sorting.
  2. Trees In Data Structure And Algorithm Using C, Code, Explanation, Types.
  3. Queue In Data Structure And Algorithm Using C, Code, Explanation.

Array Operations In Data Structure And Algorithms

An array is a group of similar data items. The elements of an array are of the identical type and each element can be accessed using the same name, but with different index values. The elements of the array are collected in consecutive memory places and are referenced by an index (also recognized as the subscript). A subscript is an ordinal number which is used to recognize an element of the array.

arrays-in-data-structure-and-algorithms

Example: Array for character

char name[25];
strcpy(name,"Chetan");

array
| C | h | e | t | a | n |………….|
Total memory occupied is 1 byte * 25 = 25 byte.

We have previously seen that all variable must be declared before it is used. The same idea is true for array variables. An array need be declared before being used. Declaring an array means defining the following:

  1. Data type—the kind of values it can store, for example, int, char, float, double.
  2. Name—to identify the array.
  3. Size—the maximum number of values that the array can hold.

Arrays are declared using the following syntax:

type name[size];

The type can be int, float, double, char, or any other valid data type. The number in brackets shows the size of the array, i.e., the maximum number of elements that can be collected in the array. For example, if we write, int marks[10]; then the statement declares marks to be an array holding 10 elements. In C, the array index begins from zero. The first element will be saved in marks[0], the second element in marks[1], and so on. Therefore, the last element, which is the 10th element, will be saved in marks[9].

Operations Performed In Array

  1. Traversing an array
  2. Inserting an element in an array
  3. Searching an element in an array
  4. Deleting an element from an array
  5. Merging two arrays
  6. Sorting an array in ascending or descending sequence

a. Traversing an Array

Traversing an array means accessing each and all elements of the array for a particular purpose. Traversing the data elements of an array can include print every element, calculating the total number of elements, or conducting any process on these elements.

Program to read and display numbers:

#include <stdio.h>
int main()
{
int i, n, arr[20];
clrscr();
printf("\n Enter the number of elements to store in the array : ");
scanf("%d", & n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",& arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}

Output:

Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
The array elements are 1 2 3 4 5

b. Inserting an element in an array

If an element has to be inserted at the end of an existing array, then the task of insertion is quite simple. We just have to add 1 to the upper_bound and assign the value. Algorithm to insert an element in the middle of an array.

Step 1: [INITIALIZATION] SETI=N
Step 2: Repeat Steps 3 and 4 whileI>= POS
Step 3: SET A[I + 1] = A[I]
Step 4: SETI=I–1
[END OF LOOP]
Step 5: SETN=N+1
Step 6: SET A[POS] = VAL
Step 7: EXIT

Program to insert element in between of the array

#include <conio.h>
int main()
{
int i, n, num, pos, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", & n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", & arr[i]);
}
printf("\n Enter the number to be inserted : ");
scanf("%d", & num);
printf("\n Enter the position at which the number has to be added :");
scanf("%d", & pos);
for(i=n–1;i>=pos;i––)
arr[i+1] = arr[i];
arr[pos] = num;
n = n+1;
printf("\n The array after insertion of %d is : ", num);
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}

Output:

Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be inserted : 0
Enter the position at which the number has to be added : 3
The array after insertion of 0 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 0
arr[4] = 4
arr[5] = 5

c. Deleting an element from an array

#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, pos, arr[10];
clrscr();
printf("\n Enter the number of elements in the array : ");
scanf("%d", & n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d", & arr[i]);
}
printf("\nEnter the position from which the number has to be deleted : ");
scanf("%d", & pos);
for(i=pos; i<n–1;i++)
arr[i] = arr[i+1];
n––;
printf("\n The array after deletion is : ");
for(i=0;i<n;i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}

Output:

Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the position from which the number has to be deleted : 3
The array after deletion is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 5

d. Merging two arrays

Merging two arrays in a third array means first copying the elements of the first array into the third array and then copying the elements of the second array in the third array. Therefore, the merged array contains the elements of the first array followed by the elements of the second array.

Program to merge two arrays?

#include <stdio.h>
#include <conio.h>
int main()
{
int arr1[10], arr2[10], arr3[20];
int i, n1, n2, m, index=0;
clrscr();
printf("\n Enter the number of elements in array1 : ");
scanf("%d", & n1);
printf("\n\n Enter the elements of the first array");
for(i=0;i<n1;i++)
{
printf("\n arr1[%d] = ", i);
scanf("%d", & arr1[i]);
}
printf("\n Enter the number of elements in array2 : ");
scanf("%d", & n2);
printf("\n\n Enter the elements of the second array");
for(i=0;i<n2;i++)
{
printf("\n arr2[%d] = ", i);
scanf("%d", & arr2[i]);
}
m = n1+n2;
for(i=0;i<n1;i++)
{
arr3[index] = arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index] = arr2[i];
index++;
}
printf("\n\n The merged array is");
for(i=0;i<m;i++)
printf("\n arr[%d] = %d", i, arr3[i]);
getch();
return 0;
}

Output:

Enter the number of elements in array1 : 3
Enter the elements of the first array
arr1[0] = 1
arr1[1] = 2
arr1[2] = 3
Enter the number of elements in array2 : 3
Enter the elements of the second array
arr2[0] = 4
arr2[1] = 5
arr2[2] = 6
The merged array is
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6

Types of arrays

01. One dimensional array    ex: int a[10]

02. Two-dimensional array     ex: int a[10][10]

03. Multi-dimensional array     ex: int a[10][10][10]

Also, read:

About The Author

Share Now