Problem Description
Write a program to insert an element in the array.
Example
For example consider an array having three elements in it initially and a[0] = 1, a[1] = 2 and a[2] = 3 and you want to insert a number 45 at location 1 i.e. a[0] = 45, so we have to move elements one step below so after insertion a[1] = 1 , and a[2] = 2 and a[3] = 3.
Input and Output Format:
Assume that the maximum number of elements in the array is 20.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.CODING ARENA
#include <stdio.h>
int main()
{
int array[100], position,i,temp, c, n, value;
scanf("%d", &n);
for (c = 0; c < n; c++)
{
scanf("%d", &array[c]);
}
scanf("%d", &position);
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position] = value;
printf("Before sorting\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
printf("After sorting\n");
for(c=0;c<n+1;c++)
{
for(i=c;i<n+1;i++)
{
if(array[c]>array[i])
{
temp=array[c];
array[c]=array[i];
array[i]=temp;
}
}
}
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
return 0;
}
Test Case 1
Input (stdin)5 9 6 3 2 7 1 10
Expected OutputBefore sorting 9 10 6 3 2 7 After sorting 2 3 6 7 9 10
Test Case 2
Input (stdin)7 7 6 5 4 3 2 1 0 9
Expected OutputBefore sorting 9 7 6 5 4 3 2 1 After sorting 1 2 3 4 5 6 7 9
No comments:
Post a Comment