fork download
#include <iostream>

using namespace std;

const int MAX_SIZE = 10;

class Array {
private:
    int elements[MAX_SIZE];
    int size;

public:

    Array() : size(0) {}
    void addElement(int value) {
        if (size < MAX_SIZE) {
            elements[size++] = value;
            cout << "Element added successfully." << endl;
        } else {
            cout << "Array is full. Cannot add more elements." << endl;
        }
    }

    void display() {
        if (size == 0) {
            cout << "Array is empty." << endl;
        } else {
            cout << "Array elements:" << endl;
            for (int i = 0; i < size; ++i) {
                cout << elements[i] << " ";
            }
            cout << endl;
        }
    }

    int findMax() {
        if (size == 0) {
            cout << "Array is empty. No maximum element." << endl;
            return -1;
        }

        int maxElement = elements[0];
        for (int i = 1; i < size; ++i) {
            if (elements[i] > maxElement) {
                maxElement = elements[i];
            }
        }
        return maxElement;
    }
};

int main() {
    Array arr;
    arr.addElement(10);
    arr.addElement(5);
    arr.addElement(20);
    arr.addElement(8);

    arr.display();
    int maxElement = arr.findMax();
    if (maxElement != -1) {
        cout << "Maximum element in the array: " << maxElement << endl;
    }

    return 0;
}
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
Element added successfully.
Element added successfully.
Element added successfully.
Element added successfully.
Array elements:
10 5 20 8 
Maximum element in the array: 20