This is a C Program to insert an element in a specified position in a given array.
This program implements a one dimentional array, sorts it and then takes a user input and inserts the desired element in the specified position of a one dimentional array and print all the elements of the array with a +1 increment in the array size.
1. Declare a one dimentional array of some fixed capacity.
2. Take size of the array as input from users, which must be at least one less than array’s capacity.
3. Define array elements, taking each element as input from users.
4. Sort the array elements.
5. Now, take a value from users, which needs to be inserted inside this array.
6. Select a suitable position for the new element by comparing the new element to the array elements and insert it in that position.
7. Print all the elements. Now, the array size would be previous array size+1.
8. Exit
Here is source code of the C program to insert an element in a specified position in a given array. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C program to insert a particular element in a specified position
* in a given array
*/
#include <stdio.h>
void main()
{
int array[10];
int i, j, n, m, temp, key, pos;
printf("Enter how many elements \n");
scanf("%d", &n);
printf("Enter the elements \n");
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements are \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
// Sorting the elements of the array
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
printf("Sorted list is \n");
for (i = 0; i < n; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = 0; i < n; i++)
{
if (key < array[i])
{
pos = i;
break;
}
if (key > array[n-1])
{
pos = n;
break;
}
}
if (pos != n)
{
m = n - pos + 1 ;
for (i = 0; i <= m; i++)
{
array[n - i + 2] = array[n - i + 1] ;
}
}
array[pos] = key;
printf("Final list is \n");
for (i = 0; i < n + 1; i++)
{
printf("%d\n", array[i]);
}
}
1. Declare a one dimentional array, a of some fixed capacity, 10.
2. Take an input for size of the array, n. The size of the array must be at least one smaller than array’s capacity, 10.
3. Using for loop, fill the array with elements to its size.
4. Print all the elements of the array.
5. Sort the elements of the array in ascending order. In this case, we have used Insertion sort.
6. Now, take a value from users, which needs to be inserted inside array.
7. Run a for loop from 0 to arraySize-1, checking and comparing each element of array
i) If new element is smaller than array element, then the position is array element is saved.
ii) If new element is bigger than the last element of array (biggest element of array), then the size of array is saved.
8. Position saved is where the new element will be inserted.
9. All the elements at that position in the array and after that are shifted backwards by 1.
10. Print all the elements of the new array.
Enter how many elements 5 Enter the elements 76 90 56 78 12 Input array elements are 76 90 56 78 12 Sorted list is 12 56 76 78 90 Enter the element to be inserted 61 Final list is 12 56 61 76 78 90
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Check C Books
- Apply for C Internship
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Practice BCA MCQs