Insertion in array
#include<iostream.h>
#include<process.h>
int findpos(int [], int, int);
void main()
{
int
ar[50],item,n,index;
cout<<"
how many elements do you want to create array with?...\n";
cin>>n;
cout<<"\n
enter array elements(must be sorted in asc order)\n";
for(int
i=0; i<n; i++)
cin>>ar[i];
char
ch='y';
while(ch=='y'||ch=='Y')
{
cout<<"\n enter element to be
inserted...";
cin>>item;
if(n==50)
{ cout<<"overflow!!!\n";
exit(1);
}
index=
findpos(ar,n,item);
for(i=n; i>index; i--)
{ ar[i]=ar[i-1];}
ar[index]=item;
n+=1;
cout<<"\n
want to insert more elements? (y/n)...";
cin>>ch;
}
cout<<"the array now is as shown
below...\n";
for(i=0;
i<n; i++)
cout<<ar[i]<<" ";
cout<<endl;
}
}
int findpos(int
ar[], int size, int item)
{
int pos;
if(item<ar[0])
pos=0;
else
{
for(int i=0; i<size-1; i++)
pos=0;
}
}
}
}
0 Comments