Life Hacks


      

Write a program of the insertion in an array.



#include<iostream.h>

#include<conio.h>

void main( ) /* main start */

{

clrscr( );

int a[20], j, n, k, b, c; /* local variable */

cout << " \n Enter the number of elements in an array is: " << " \n ";

cin >> n;

cout << " \n Enter the value of elements of array is: " << " \n ";

for(c = 1; c <= n; c++)

{

cin >> a[c];

}

j = n; /* End of for loop */

cout << " \n Enter the position of element in which is inserted: " << " \n ";

cin >> k;

while(j >= k)

{

a[j + 1] = a[j];

j = j – 1;

} /* End of while loop */

cout << " \n Enter the elements is: " << " \n ";

cin >> b;

a[k] = b;

n = n + 1;

cout << " \n Now new array is: " << " \n ";

for(c = 1; c <= n; c++)

{

cout << " \n " << a[c];

} /* End of for loop */

getch( );

}

Output:-

clip_image002

Leave a Reply