In: Computer Science
# please answer question asap please
Write a program in C to insert new value in the array (unsorted list).
Use pointer notation for the array elements
.Test Data:Input the size of array: 4 Input 4 elements in the array in ascending order:
element -0: 2
element -1: 9
element -2: 7
element -3: 12
Input the value to be inserted: 5
Input the Position, where the value to be inserted: 2
Expected Output:The current list of the array:2 9 7 12
After Insert the element the new list is:2 5 9 7 12
C code:
#include <stdio.h>
int main()
{
int n;
printf("Input size of array: ");
scanf("%d",&n);
int arr[n],ele,pos;
int *p=arr;
for(int i=0;i<n;i++)
{
printf("Enter element - %d: ",i);
scanf("%d",(p+i));
}
printf("Input the value to be inserted: ");
scanf("%d",&ele);
printf("Input the Position, where the value to be inserted:
");
scanf("%d",&pos);
printf("Expected Output:The current list of the array: ");
for(int j=0;j<n;j++)
{
printf("%d ",*(p+j));
}
//main code
for(int i=n-1;i>=0;i--)
{
*(p+i+1)=*(p+i);
if(i==pos-1)
{
*(p+i)=ele;
break;
}
}
printf("\nAfter Insert the element the new list is: ");
for(int j=0;j<=n;j++)
{
printf("%d ",*(p+j));
}
}
Output:
