In this post, you are provided a source code that how to insert an element in a list of arrays at a desired
position with the mechanism performed to do it .
In output instead of overwriting its looking that the whole list move one step forward and the new element
is inserting at its desired position ..
Mechanism
#include <conio.h>
void main (void)
{
static int a[6];
int x,c;
for(int i=0; i<5; i++)
{
a[i] = i+1;
printf("\n %d",a[i]);
}
printf("\n Enter number to add in the list and position where to add");
scanf("%d %d",&x,&c);
c = c - 1;
for(int y=5; y>c; y--)
a[y] = a[y-1];
a[c] = x;
for (i=0; i<=5; i++)
printf("\n %d",a[i]);
getch();
}





number is inserting at the actuall place as we desired but the default array list just move one place forward making room for the desired element,not replacing the pace of previous element actually.....for e.g
ReplyDeletewe inserts 45 in place 3
RESULT:
1
2
45
3
4
5
RESULT SHOULD BE:
1
2
45
4
5
In your case, it would replace the array. And that is easy.
DeleteWe are inserting an element in an array, in which the element size increases by 1, and the array moves forward.
Regards
Only a programmer KnOwS its impOrtAnce
ReplyDelete