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>
#include <string>
using namespace std;
class Question1{
private:
//1D integer array of maximum size: 100, n (int).
int integers[100];
int n;
public:
Question1(int n){
this->n=n;
}
void input(){
for(int i=0;i<n;i++){
cout<<"Enter value "<<(i+1)<<": ";
cin>>this->integers[i];
}
}
void display(){
int max=this->integers[0];
int min=this->integers[0];
for(int i=0;i<n;i++){
if(max<this->integers[i]){
max=this->integers[i];
}
if(min>this->integers[i]){
min=this->integers[i];
}
}
cout<<"The minimum element is: "<<min<<"\n";
cout<<"The maximum element is: "<<max<<"\n";
}
};
int main(){
Question1 question1(5);
question1.input();
question1.display();
system("pause");
return 0;
}
Comments
Leave a comment