Inserting elements in an array using C Language
How does an array's insertion operation work?
Adding one or more items at a particular index or indexes into the existing components of an array is known as an "insert operation" or "inserting elements".
Below are the steps to insert an element into an array using the algorithm:
Step 1: Start
Step 2: Set len = len + 1(size of an array)
Step 3: Set s = len – 1(counter variable)
Step 4: Repeat Step 5 and 6 for s = len - 1 to s >= loc - 1
Step 5: Set array[s+1] = array[s](iterative forward)
Step 6: Set s = s – 1 (Decreasing counter variable)
Step 7: End of step 4 loop.
Step 8: Insert element to set array[loc-1]
Step 9: Stop
Techniques for adding elements to the array
Detailed logic for adding an element to an array step by step.
- The size of the input and the array's elements. It should be stored in a variable like size and arr.
- Add a new element to the array and specify its location. Please put it in a variable, such as pos or num.
- Inserting an array
- Move the components from the specified insert point one position to the right to add a new element to the array. To insert, run a loop from size to pos in decreasing order. A loop structure like for(i=size; i>=pos; i--) would be appropriate.
- A loop uses arr[i] = arr[i - 1] to copy a previous component to the present component.
- Ultimately, upon the completion of the shift. Copy the new element, arr[pos - 1] = num, to the specified location.
Code to insert a number in an array
/**
* Code to insert a number in an array
*/
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE];
int s, len, data, loc;
/* Input dimension of an array */
printf("Enter size of an array : ");
scanf("%d", &len);
/* Input elements in array */
printf("Enter elements in array : ");
for(s=0; s<len; s++)
{
scanf("%d", &array[s]);
}
/* Input new element and position to add */
printf("Enter elements to add : ");
scanf("%d", &data);
printf("Enter the element location : ");
scanf("%d", &loc);
/* If the position of the element is not valid */
if(loc > len+1 || loc <= 0)
{
printf("Please enter location between 1 to %d", len);
}
else
{
/* Make room for a new array element by shifting to the right */
for(s=len; s>=loc; s--)
{
array[s] = array[s-1];
}
/* Insert new element at given position and increment size */
array[loc-1] = data;
len++;
/* Print array after insert operation */
printf("Array elements after insertion : ");
for(s=0; s<len; s++)
{
printf("%d\t", array[s]);
}
}
return 0;
}
Output:
Another example of inserting an element
/**
* Code to insert a number in an array
*/
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE];
int s, len, data, loc;
/* Input dimension of an array */
printf("Enter size of an array : ");
scanf("%d", &len);
/* Input elements in array */
printf("Enter elements in array : ");
for(s=0; s<len; s++)
{
scanf("%d", &array[s]);
}
/* Input new element and position to add */
printf("Enter elements to add : ");
scanf("%d", &data);
printf("Enter the element location : ");
scanf("%d", &loc);
/* If the position of the element is not valid */
if(loc > len+1 || loc <= 0)
{
printf("Please enter location between 1 to %d", len);
}
else
{
/* Make room for a new array element by shifting to the right */
for(s=len; s>=loc; s--)
{
array[s] = array[s-1];
}
/* Insert new element at given position and increment size */
array[loc-1] = data;
len++;
/* Print array after insert operation */
printf("Array elements after insertion : ");
for(s=0; s<len; s++)
{
printf("%d\t", array[s]);
}
}
return 0;
}
Output: