WAP to
create 2 array class using friend function and find out which array is smallest.
#include<iostream.h>
#include<conio.h>
int i=0;
class array1
{
public:
void getdata(int a[],int n);
void displaydata(int a[],int n);
friend int min(int a[],int n); //friend function declare
};
void array1::getdata(int a[],int n)
{
for(i=0;i<n;i++)
{
cout<<i;
cout<<"\t\t";
cin>>a[i];
}
}
void array1 :: displaydata(int a[],int n)
{
cout<<"\nelemnt of first array is\n";
for(i=0;i<n;i++)
{
cout<<i;
cout<<"\t\t";
cout<<a[i];
cout<<"\n";
}
}
int min(int a[],int n)
//friend function call
{ //int i;
int smallest;
smallest = a[0];
for (i=0; i<n; i++)
{
if (a[i]<smallest)
{
smallest=a[i];
}
}
return(smallest);
//return value to main function
}
// class two
start
/*-----------------------------------------------------------------*/
class array2
{
public:
void getdata2(int b[],int m);
void displaydata2(int b[],int m);
friend int min2(int b[],int m);//second friend function
declare
};
void array2::getdata2(int b[],int m)
{
for(i=0;i<m;i++)
{
cout<<i;
cout<<"\t\t";
cin>>b[i];
}
}
void array2 :: displaydata2(int b[],int m)
{
cout<<"\nelemnt of first array is\n";
for(i=0;i<m;i++)
{
cout<<i;
cout<<"\t\t";
cout<<b[i];
cout<<"\n";
}
}
int min2(int b[],int m)
//second friend function define
{ //int i;
int smallest2;
smallest2 = b[0];
for (i=0; i<m; i++)
{
if (b[i]<smallest2)
{
smallest2=b[i];
}
}
return(smallest2); //return value to main function
}
/*--------------------MAIN FUNCTION----------------------*/
void main()
{
int *a;
//pointer declare
int *b;
//pointer declare
int n,m;
a=new int [n];
//pointer define
b=new int [m];
//pointer define
array1 x;
array2 y;
clrscr();
cout<<"\n enter how many element you want to
enter in first array \n";
cin>>n;
x.getdata(a,n);
x.displaydata(a,n);
//x.min(a,n);
cout<<"min number in an first array
is"<<min(a,n); //friend function call
cout<<"\n enter how many element you want to
enter in second array\n";
cin>>m;
y.getdata2(b,m);
y.displaydata2(b,m);
//y.min2(b,m);
cout<<"\nsmallest number in second array is
\t"<<min2(b,m);//friend function call
delete [] a; //delete memory
delete [] b; //delete memory
getch();
}