Answer to Question #188732 in C++ for Shubham kumar jha

Question #188732

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.


1
Expert's answer
2021-05-03T13:44:57-0400
#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;
}

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