0% found this document useful (0 votes)
26 views

Insertion

The document is a C++ program that implements insertion of a number into an array. It takes input of 10 numbers into an array, then takes input of a number to insert. It checks if the number is less than the first element, greater than the last element, or between two existing elements, and inserts it appropriately by shifting the other elements. It then prints the array with the inserted number.

Uploaded by

Swapnil Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Insertion

The document is a C++ program that implements insertion of a number into an array. It takes input of 10 numbers into an array, then takes input of a number to insert. It checks if the number is less than the first element, greater than the last element, or between two existing elements, and inserts it appropriately by shifting the other elements. It then prints the array with the inserted number.

Uploaded by

Swapnil Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Q 7. WAP to impliment insertion of a no in an array .

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int x[10],i,n,p;
cout<<" Enter no in ascending order :\n";
for(i=0;i<9;i++)
{
cin>>x[i];
}
cout<<" Enter the element to be inserted : ";
cin>>n;
if(n<x[0])
{
for(i=9;i<=1;i++)
{
x[i]=x[i-1];
}
x[0]=n;
}
else if(n>x[8])
{
x[9]=n;
}
else
{
for(i=0;i<8;i++)
{
if(n>x[i]&&n<=x[i+1])
{
p=i;
break;
}
}
for(i=9;i>p;i--)
{
x[i]=x[i-1];
}
x[p+1]=n;
}
cout<<"After insertion : \n";
for(i=0;i<=9;i++)
{
cout<<x[i]<<endl;
}
getch();
}

OUTPUT 7 :

Enter no in ascending order :


1
4
6
9
12
16
19
22
25
Enter the element to be inserted : 18
After insertion :
1
4
6
9
12
16
18
19
22
25

You might also like