Write a program in C to insert New value in the array at a given positionNote: First read array size then elements and then position and its valueFor example:Test Input Result151 2 3 4 53 8New Array is 1 2 8 3 4 5
Question
Write a program in C to insert New value in the array at a given positionNote: First read array size then elements and then position and its valueFor example:Test Input Result151 2 3 4 53 8New Array is 1 2 8 3 4 5
Solution
Here is a simple C program that inserts a new value in an array at a given position:
#include <stdio.h>
int main() {
int array[100], position, i, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (i = 0; i < n; i++)
scanf("%d", &array[i]);
printf("Enter the location where you wish to insert an element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (i = n - 1; i >= position - 1; i--)
array[i+1] = array[i];
array[position-1] = value;
printf("Resultant array is\n");
for (i = 0; i <= n; i++)
printf("%d ", array[i]);
return 0;
}
This program first reads the number of elements in the array, then the elements themselves. It then asks for the position at which to insert the new element and the value of the new element. It shifts all elements from that position to the right, then inserts the new element. Finally, it prints the new array.
Similar Questions
Write C program to insert the values at the specific index in the array.
Consider an array arr of size 5, initially filled with elements [2, 4, 6, 8, 10]. You want to insert the element 5 at position 2 in the array. What would be the array after the insertion?
Reni is working on a programming challenge where she needs to add an element 'x' into an existing array at a specified index 'k'. The task requires her to take an existing array and two integers, 'x' and 'k'. Her goal is to insert the element 'x' into the array at the index 'k', effectively shifting the subsequent elements to the right. Once the insertion is complete, Reni should display the updated array.Help her with the program.Input format :The first line of input is an integer 'n', representing the initial size of the array.The second line of input consists of 'n' space-separated integers, the elements of the array.The third line of input is an integer 'x,' the element to be inserted.The fourth line of input is an integer 'k', representing the position to insert 'x.'Output format :The output displays the array after inserting 'x' at position 'k', separated by a space.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:1 ≤ n ≤ 10Size of the array = n+11 ≤ Each element ≤ 10000 ≤ k ≤ n1 ≤ x ≤ 1000Sample test cases :Input 1 :510 20 30 40 50151Output 1 :10 15 20 30 40 50 Input 2 :102 5 3 9 8 12 20 97 37 10002310Output 2 :2 5 3 9 8 12 20 97 37 1000 23 Input 3 :3178 567 10008972
Write a program to create a list using Array List.After creation insert a new element at the required position and print the list123452(position)10(new element)output:1210345
How do you initialize a 2D array in C?
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.