Answer to Question #187805 in C++ for Archana

Question #187805

Create a class: “Question 1” with data members: 1D integer array of maximum size: 100, n (int). Create a dynamic constructor which takes input of n and n no. of array elements. Apart from taking input this class also displays the maximum and minimum elements from the given array elements.


1
Expert's answer
2021-05-01T15:23:52-0400
#include <iostream>
using namespace std;
 
class Question1
{
public:
	Question1(int n);
	~Question1();
 
private:
	const int MAX = 100;
	int size;
	int* arr;
};
 
Question1::Question1(int n)
{
	if (n > MAX) n = 100;
	size = n;
	arr = new int[size];
	int min = 0;
	int max = 0;
	cout << "Enter " << size << " values use space (1 5 10): ";
	for (int i = 0; i < size; i++)
	{
		cin >> arr[i];
		if (i == 0)
		{
			min = arr[i];
			max = arr[i];
		}
		if (arr[i] > max) max = arr[i];
		if (arr[i] < min) min = arr[i];
	}
	cout << "Max element in array is: " << max << endl;
	cout << "Min element in array is: " << min << endl;
	
}
 
Question1::~Question1()
{
	delete[] arr;
}
 
int main()
{
	int size = 0;
	cout << "Enter number of elements: ";
	cin >> size;
	Question1 Q1(size);
	return 0;
}

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