1. 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{
int array[100], n;
public:
Question1(){}
Question1(int x, int *arr){
n = x;
for(int i = 0; i < n; i++){
array[i] = arr[i];
}
}
int max(){
int maxv = INT_MIN;
for(int i = 0; i < n; i++){
if(array[i] > maxv) maxv = array[i];
}
return maxv;
}
int min(){
int minv = INT_MAX;
for(int i = 0; i < n; i++){
if(array[i] < minv) minv = array[i];
}
return minv;
}
};
int main(){
Question1 q;
int array[100], n;
cout<<"Input number of elements: ";
cin>>n;
cout<<"Input elements;\n";
for(int i = 0; i < n; i++) cin>>array[i];
q = Question1(n, array);
cout<<"Maximum element is "<<q.max()<<endl;
cout<<"Minimum element is "<<q.min()<<endl;
return 0;
}
Comments
Leave a comment