. 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.
Answer:-
#include <iostream>
using namespace std;
class Array{
int arr[100];
int n;
public:
Array(int n, int arr[100]){
this->n = n;
for(int i = 0; i < this->n; i++){
this->arr[i] = arr[i];
}
}
int get_max(){
int max = INT_MIN;
for(int i = 0; i < this->n; i++){
if(this->arr[i] > max) max = this->arr[i];
}
return max;
}
int get_min(){
int min = INT_MAX;
for(int i = 0; i < this->n; i++){
if(this->arr[i] < min) min = this->arr[i];
}
return min;
}
};
int main(){
int n, arr[100];
cout<<"Input number of elements: ";
cin>>n;
cout<<"Input array elements: \n";
for(int i = 0; i < n; i++){
cin>>arr[i];
}
Array *array = new Array(n, arr);
cout<<"\nThe maximum value in the array is ";
cout<<array->get_max();
cout<<"\nThe minimum value in the array is ";
cout<<array->get_min();
return 0;
}
Comments
Leave a comment