Write C++ program to Create two classes Array1 and Array2 which contains data members as Integer array of a specified size. Write necessary member functions to accept and display array elements of both the classes. Find and display smallest number from both the array.(use friend function,manipulators)
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
class arr2;
class arr1
{
public:
int n1,i,*a; //n1=no , *a=pointer a
void accept()
{
cout<<"Enter how many element in an array: ";
cin>>n1;
a=new int[n1]; //allocate memory dynamically
for(i=0;i<n1;i++)
{
cout<<"Enter an element :- ";
cin>>a[i];
}
}
void disp()
{
for(i=0;i<n1;i++)
{
cout<<a[i]<<" ";
}
}
friend void min(arr1 a1,arr2 a2); // declare friend function
};
class arr2
{
public:
int n2,i,*b; // n2=no , *b=pointer b
void accept()
{
cout<<"\nEnter how many element in an array: ";
cin>>n2;
b=new int[n2]; //allocate memory dynamically
for(i=0;i<n2;i++)
{
cout<<"Enter an element :- ";
cin>>b[i];
}
}
void disp()
{
for(i=0;i<n2;i++)
{
cout<<b[i]<<" ";
}
}
friend void mini(arr1 a1,arr2 a2); // declare friend function
};
void min(arr1 a1,arr2 a2)
{
int s,i;// s=smallest value in array 1
s=a1.a[0];
for(i=0;i<a1.n1;i++)
{
if (s>a1.a[i])
s=a1.a[i];
}
int s1; // s1=smallest value in array 2
s1=a2.b[0];
for(i=0;i<a2.n2;i++)
{
if (s1>a2.b[i])
s1=a2.b[i];
}
if(s<s1)
cout<<"\nMINIMUM from 2 array is: "<<setw(2)<<s;
else
cout<<"\nMINIMUM from 2 array is: "<<setw(2)<<s1;
}
int main()
{
arr1 a1; //create instance of class arr1
arr2 a2; //create instance of class arr2
a1.accept();
a1.disp();
a2.accept();
a2.disp();
min(a1,a2);
getch();
return(0);
}
0 Comments