3. /* Program for insertion of an element in an array*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define S 7
main()
{
int arr[10];
int i, pos, num, j;
clrscr();
for( i = 0; i < S; i++ )
{
printf("\n Enter arr[%d] element: ", i);
scanf(" %d ", &arr[i] );
}
clrscr();
printf(" \n\nArray is \n\n ");
for( i = 0; i < S; i++ )
printf(" %2d ", arr[i] );
printf(" \nEnter the element which you want to insert\n ");
scanf(" %d ", &num );
printf(" \nEnter the position (first is 0)\n " );
scanf("%d", &pos);
if( pos > = 0 && pos <= 9)
{
for( j = S;j > pos; j-- )
arr[j] = arr[j-1];
arr[j] = num;
}
else
{
printf(" Wrong position selected\n ");
getch();
exit(1);
}
printf(" Array after insertion\n ");
for(i = 0; i < S; i++ )
printf(" %d ", arr[i]);
getch();
}
OUTPUT:
Enter arr[0] element: 4
Enter arr[1] element: 7
Enter arr[2] element: 3
Enter arr[3] element: 6
Enter arr[4] element: 8
Enter arr[5] element: 4
Array is
4 7 3 6 8 4
Enter the element which you want to insert
3
Enter the position (first is 0)
4
Array after insertion
4 7 3 6 8 4 4
out put is wrong
ReplyDeleteit should be 47363844