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.
#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;
}
Comments
Leave a comment