compiled on turbocpp 4.5.....error is that the final merged array is much bigger with garbage values...please help to find the mistake in this program...
Code:
//cpp program to concatenate 2 arrays
#include<iostream.h>
#include<conio.h>
//the class
class array
{
private:
int a[40];
int n;
public:
void getdata();
void concatenate(array &a1,array &a2);
void showdata(array &a);
};
void array::getdata()
{
cout<<"\n How many?";
cin>>n;
cout<<"\n Enter the elements:";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
}
void array::showdata(array &a)
{
for(int i=0;i<a.n;i++)
{
cout<<"\n"<<a.a[i];
}
}
//concatenation or merge function for the two arrays
void array::concatenate(array &a1,array &a2)
{
array a3;
for(int i=0,k=0;i<a1.n;i++,k++)
{
a3.a[k]=a1.a[i];
}
for(int j=0;j<a2.n;j++,k++)
{
a3.a[k]=a2.a[j];
}
}
void main()
{
array a1,a2,a3;
a1.getdata();
a1.showdata(a1);
a2.getdata();
a2.showdata(a2);
a3.concatenate(a1,a2);
a3.showdata(a3);
getch();
}