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

Question #188728

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-03T19:01:17-0400
#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;
}

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