Answer to Question #213766 in C++ for Hemambar

Question #213766

Create two classes IntArray to store the set of integer numbers and FloatArray to store decimal

numbers. Add a member function read() for both classes for reading inputs. Create two objects ‘x’

for IntArray and ‘y’ for FloatArray. Read the inputs for x and y. Using a friend function maxmin(x,y),

display the maximum and minimum among the set of integers and decimal numbers.


1
Expert's answer
2021-07-05T05:12:59-0400
#include <iostream>//Input Output Stream use cin and cout function


using namespace std;
class FloatArray;//Prototype
//We implement class IntArray which store integer number 
class IntArray
{
private:
	//Fields
	int size;//Size array
	int* date;//set integer numbers
public:
	//Constructor parametriz
	IntArray(int _size=1)
	{
		this->size = _size;//Set the size array
		this->date = new int[this->size];//Alloc memory for (size) numbers
	}
	//Get size
	int getSize()const
	{
		return this->size;
	}
	//Read Date
	void read()
	{
		cout << "Please enter the numbers:\n";
		for (int i = 0; i < this->size; i++)
			cin >> this->date[i];
	}
	//Overload operator indexing []
	int& operator[](const int index)
	{
		return this->date[index];
	}
	friend void maxmin(IntArray x, FloatArray y);
	//Destructor
	~IntArray()
	{
		delete this->date;
	}
};
//class FloatArray
class FloatArray
{
private:
	//Fields
	int size;//Size array
	double* date;//set double numbers
public:
	//Constructor parametrized
	FloatArray(int _size = 1)
	{
		this->size = _size;//Set the size array
		this->date = new double[this->size];//Alloc memory for (size) numbers
	}
	//Read Date
	void read()
	{
		cout << "Please enter the numbers:\n";
		for (int i = 0; i < this->size; i++)
			cin >> this->date[i];
	}
	//Geting size array
	int getSize()const
	{
		return this->size;
	}
	//Overload operator indexing []
	double &operator[](const int index)
	{
		return this->date[index];
	}
	friend void maxmin(IntArray x, FloatArray y);
	~FloatArray()
	{
		delete[] date;
	}
};
void maxmin(IntArray x, FloatArray y)
{
	double max = x[0] * 1.0;
	double mn = max;
	for (int i = 0; i < x.getSize(); i++)
	{
		if (x[i] * 1.0 > max)
			max = x[i] * 1.0;
		if (x[i] * 1.0 < mn)
			mn = x[i] * 1.0;
	}
	for (int i = 0; i < y.getSize(); i++)
	{
		if (y[i] > max)
			max = y[i];
		if (y[i] < mn)
			mn = y[i];
	}
	cout << "Max: " << max << endl;
	cout << "Min: " << mn << endl;
}
int main()
{
	cout << "Please enter size of IntArray: ";
	int szInt;
	cin >> szInt;
    IntArray *x=new IntArray(szInt);
	x->read();
	cout << "Please enter size of FloatArray: ";
	int szFloat;
	cin >> szFloat;
	FloatArray *y=new FloatArray(szFloat);
	y->read();
	maxmin(*x, *y);
	return 0;
}
//Example test
/*
Input
4
1 2 3 4
2
1.25 2.666
Output
Max 4
Min 1


*/

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS