Sunday, December 12, 2010

Array operations

/*program to perform array operations
1_Insertion
2_Deletion
3_Searching
4_Sorting
5_Merging
6_Traversal*/

#include
#include
#include

#define SIZE 10

class array
{
int a[SIZE],n;

public:
array();
void read();
void insert();
void move_ins(int);
void delet();
void move_del(int);
void search();
void sort();
void merge(array,array);
void display();
};

array::array()
{
int i;
for(i=0;i a[i]=-1;
};

void array::read()
{
int i;
cout<<"\nEnter the number of elements:";
cin>>n;
if(n>SIZE)
{
cout<<"Array Limit Exceeded!!!Please Re-try";
return;
}
cout<<"\nEnter the "< for(i=0;i cin>>a[i];
}

void array::insert()
{
int pos=0,ch,ele;

do
{
cout<<"\n------Insertion------";
cout<<"\n1_At Begin";
cout<<"\n2_At End";
cout<<"\n3_Given Position";
//cout<<"\n4_Next Position";
cout<<"\n4_Return to Main Menu";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

if(n+1>SIZE)
{
cout<<"\nArray Overflow!!! Cannot insert element";
}
else
{
switch(ch)
{
case 1:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
pos=0;
move_ins(pos);
a[pos]=ele;
break;

case 2:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
pos=n;
move_ins(pos);
a[pos]=ele;
break;

case 3:
cout<<"\nEnter the element to be inserted(non -ve integer):";
cin>>ele;
cout<<"\nEnter the position of the element:";
cin>>pos;
pos--;
if(pos>SIZE-1||pos>n)
cout<<"\nInvalid position!!!cannot insert element";
else
{
move_ins(pos);
a[pos]=ele;
}
break;

/*case 4:
cout<<"\nEnter the element to be inserted:";
cin>>ele;
pos=pos+1;
if(pos>SIZE-1)
cout<<"\nInvalid position!!!cannot insert element";
else
{
move_ins(pos);
a[pos]=ele;
}
break;*/

case 4:
return;
//break;

default:
cout<<"\nInvalid choice";
break;
}
display();
}
}while(ch!=5);
}

void array::move_ins(int p)
{
int i;
for(i=n;i>p;i--)
{
a[i]=a[i-1];
}
n++;
}

void array::delet()
{
int ch,pos;



do
{
if(n==0)
{
cout<<"\nInsert first!!!!";
return;
}
cout<<"\n-------Deletion----------";
cout<<"\n1_Begin";
cout<<"\n2_End";
cout<<"\n3_Given Position";
cout<<"\n4_Return to Main Menu";
cout<<"\nEnter choice<1-4>:";
cin>>ch;

switch(ch)
{
case 1:
pos=0;
move_del(pos);
break;

case 2:
pos=n-1;
cout<<"\nThe element deleted is "< a[pos]=-1;
n=n-1;
break;

case 3:
cout<<"\nEnter the position of the element to be deleted:";
cin>>pos;
pos--;
if(pos>n)
{
cout<<"\nCannot Insert !!!!!Invalid Position";
return;
}
move_del(pos);
break;

case 4:
return;
//break;

default:
cout<<"\nInvalid Choice!!!!";
break;
}
display();
}while(ch!=4);

}


void array::move_del(int p)
{
int i;
cout<<"\nThe element deleted is "< for(i=p;i {
a[i]=a[i+1];
}
n--;
}

void array::search()
{
int ele,flag=0,i,pos;

if(n==0)
{
cout<<"\nThe array is empty!!!!";
return;
}

cout<<"\nEnter the element to be searched:";
cin>>ele;
for(i=0;i {
if(a[i]==ele)
{
pos=i;
flag=1;
break;
}
}
if(flag==0)
cout<<"\nThe element "< else
cout<<"\nThe element "< }
void array::sort()
{
int i,j,ch,temp;

if(n==0)
{
cout<<"\nThe array is empty";
return;
}

do
{
cout<<"\n------Sorting-------";
cout<<"\n1_Ascending Sort";
cout<<"\n2_Descending Sort";
cout<<"\n3_Main Menu";
cout<<"\nEnter choice<1-3>:";

cin>>ch;

switch(ch)
{
case 1:
for(i=0;i {
for(j=i+1;j {

while(a[i]==-1)
i++;

while(a[j]==-1)
j++;

if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
display();
break;
case 2:
for(i=0;i {
for(j=i+1;j {

while(a[i]==-1)
i++;
while(a[j]==-1)
j++;

if(a[i] {
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
display();
break;

case 3:
return;

default:
cout<<"\nInvalid Choice!!!";
break;
}
}while(ch!=3);
}

void array::merge(array arr1,array arr2)
{
int i,j;
if((arr1.n+arr2.n)>2*SIZE)
{
cout<<"\nCannot perform merging...array cannot hold elements!!!";
}
else
{
for(i=0;i {
a[i]=arr1.a[i];
}
for(i=arr1.n,j=0;i<(arr1.n+arr2.n);i++,j++)
{
a[i]=arr2.a[j];
}
}
}

void array::display()
{
int i;

if(n==0)
{
cout<<"\nThe array is empty!!!!";
return;
}
cout<<"\nArray is now ";
for(i=0;i {
if(a[i]!=-1)
cout<<"\na["< }
}

void main()
{
int ch;
array a1,a2,a3,a4;
clrscr();
do
{
cout<<"\n----------Array operations----------";
cout<<"\n1_Read";
cout<<"\n2_Insert";
cout<<"\n3_Delete";
cout<<"\n4_Search";
cout<<"\n5_Sort";
cout<<"\n6_Display";
cout<<"\n7_Merge";
cout<<"\n8_Exit";
cout<<"\nEnter choice<1-8>:";
cin>>ch;

switch(ch)
{
case 1:
a1.read();
break;
case 2:
a1.insert();
break;
case 3:
a1.delet();
break;
case 4:
a1.search();
break;
case 5:
a1.sort();
break;
case 6:
a1.display();
break;
case 7:
cout<<"\nEnter the first array\n";
a2.read();
cout<<"\nEnter the second array\n";
a3.read();
cout<<"\nThe first ";
a2.display();
cout<<"\nThe second ";
a3.display();
a4.merge(a2,a3);
cout<<"\nmerged array\n";
a4.display();
break;
case 8:
exit(0);
break;
default:
cout<<"\nInvalid choice!!!!";
break;

}
}while(ch!=8);
getch();
}

No comments:

Post a Comment