write a c program to insert an element at desired position in an array
C Program: Insert Element at Desired Position in Array
A C program to insert an element at a desired position in an array means a program that takes an array, its size, a desired position, and a new value as inputs from the user, and inserts the new value at the desired position in the array.
The program needs to check whether the desired position is within the bounds of the array, and if so, it needs to shift all the elements to the right of the desired position to make room for the new value, and then insert the new value at the desired position.
After inserting the new value, the program should print the updated array to the console. If the desired position is out of bounds, the program should print an error message and not modify the array.
C-Program that inserts an element at a desired position in an array
if '10'
is to be stored at the ' 2 '
position then,
#include<stdio.h>
#define MAX_SIZE 100 // Maximum array size
int main() {
int arr[MAX_SIZE], size, i, pos, value;
// Input the size of the array
printf("Enter the size of the array: ");
scanf("%d", &size);
// Input array elements
printf("Enter the array elements:\n");
for (i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
// Input the position and value to insert
printf("Enter the position to insert: ");
scanf("%d", &pos);
printf("Enter the value to insert: ");
scanf("%d", &value);
// Check if position is valid
if (pos < 1 || pos > size + 1) {
printf("Invalid position!\n");
} else {
// Shift elements to the right
for (i = size; i >= pos; i--) {
arr[i] = arr[i - 1];
}
// Insert element at the desired position
arr[pos - 1] = value;
// Increment size of the array
size++;
// Print the updated array
printf("Array after inserting %d at position %d:\n", value, pos);
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
return 0;
}
Enter the size of the array: 5
Enter the array elements: 10 30 40 50 60
Enter the position to insert: 2
Enter the value to insert: 20
Array after inserting 20 at position: 2
10 20 30 40 50
Attention:
In this program, the user inputs the size and elements of an array, as well as the position and value to insert. The program then checks if the position is valid (i.e. between 1 and size+1), shifts all elements to the right starting from the position to insert, inserts the element at the desired position, increments the size of the array, and prints the updated array. If the position is not valid, the program prints an error message.
Note that this program assumes that the array is not full and has space to insert the new element. If the array is already full, you'll need to modify the program to create a larger array or use dynamic memory allocation
Further Reading:
write a program to find the largest elements of a given array with an index.
write a c program to find the second largest elements of a given array.
write a program to find the first largest and second-largest elements of a given array.
Write a c program find largest elements in this array number or index
write a program to find the smallest elements of a given array with an index.
write a program to find the first largest and first smallest elements of a given array.
write a program to find the second smallest elements of a given array.
write a program to find the second largest and second smallest elements of a given array.
write a program to find the first smallest and second smallest elements of a given array.
Enroll Now:
[ C-Programming From Scratch to Advanced 2023-2024] "Start Supercharging Your Productivity!"
Contact Us:
- For any inquiries, please email us at [[email protected]].
- Follow us on insta [ electro4u_offical_ ] for updates and tips.
Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!