Sunday, December 12, 2010

Merge two arrays

//Program to merge two arrays

#include
#include

#define SIZE 20
class array
{
int a[SIZE];
int n;

public:
array();
void read();
void sort(array,array);
void merge(array,array);
void display();
};

array::array()
{
int i;
n=0;
for(i=0;i<<"\nEnter the number of elements:"; cin>>n;
if(n>SIZE)
{
cout<<"The array cannot hold "<<<" elements...please Re-enter size"; goto start; } else { cout<<"\nEnter the "<<<" elements of array:"; for(i=0;i>a[i];
}
}

void array::sort(array a1,array a2)
{
int i,j,ch,temp;

for(i=0;ia1.a[j])
{
temp=a1.a[i];
a1.a[i]=a1.a[j];
a1.a[j]=temp;
}
}
}
for(i=0;i2*SIZE)
{
cout<<"\nCannot perform merging...array cannot hold elements!!!";
}
else
{
for(i=0,j=0;i<(arr1.n+arr2.n);i++,j++)
{
a[i]=arr2.a[j];
}
}
}

void array::display()
{
int i;
for(i=0;i
{
cout<<" "<
}
}

void main()
{
array a1,a2,a3;

a1.read();
a2.read();

cout<<"\nThe first array is \n";
a1.display();

cout<<"\nThe Second array is \n";
a2.display();

a3.merge(a1,a2);

cout<<"\nThe merged array is\n";
a3.display();
getch();
}

No comments:

Post a Comment