Insertion in an array involves adding a new element to a specific position in the array. Here's an example implementation of inserting an element into an array in C:
```
#include <stdio.h>
void insert_element(int arr[], int n, int pos, int value) {
// Shift elements to make space for new element
for (int i = n-1; i >= pos; i--) {
arr[i+1] = arr[i];
}
// Insert new element
arr[pos] = value;
}
int main() {
int arr[10] = {1, 3, 5, 7, 9};
int n = 5; // number of elements in array
int pos = 2; // position to insert new element
int value = 4; // value of new element
insert_element(arr, n, pos, value);
n++; // increment number of elements in array
// Print array after insertion
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
In this implementation, the `insert_element()` function takes an array, its length, the position to insert the new element, and the value of the new element as input. The function shifts all elements after the insert position to the right to make space for the new element and then inserts the new element at the specified position. The `main()` function initializes an array and then inserts a new element into it at position 2 with a value of 4. Finally, it prints the array after insertion.
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc