Monday, 14 July 2014

Inserting element in array at desired position , without overwriting


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


"CODE"

#include <stdio.h>
#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();
}

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: TechyBuz

3 comments:

  1. 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
    we inserts 45 in place 3
    RESULT:
    1
    2
    45
    3
    4
    5
    RESULT SHOULD BE:
    1
    2
    45
    4
    5

    ReplyDelete
    Replies
    1. In your case, it would replace the array. And that is easy.
      We are inserting an element in an array, in which the element size increases by 1, and the array moves forward.
      Regards

      Delete
  2. Only a programmer KnOwS its impOrtAnce

    ReplyDelete