fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int MAX_SIZE = 10;
  6.  
  7. class Array {
  8. private:
  9. int elements[MAX_SIZE];
  10. int size;
  11.  
  12. public:
  13.  
  14. Array() : size(0) {}
  15. void addElement(int value) {
  16. if (size < MAX_SIZE) {
  17. elements[size++] = value;
  18. cout << "Element added successfully." << endl;
  19. } else {
  20. cout << "Array is full. Cannot add more elements." << endl;
  21. }
  22. }
  23.  
  24. void display() {
  25. if (size == 0) {
  26. cout << "Array is empty." << endl;
  27. } else {
  28. cout << "Array elements:" << endl;
  29. for (int i = 0; i < size; ++i) {
  30. cout << elements[i] << " ";
  31. }
  32. cout << endl;
  33. }
  34. }
  35.  
  36. int findMax() {
  37. if (size == 0) {
  38. cout << "Array is empty. No maximum element." << endl;
  39. return -1;
  40. }
  41.  
  42. int maxElement = elements[0];
  43. for (int i = 1; i < size; ++i) {
  44. if (elements[i] > maxElement) {
  45. maxElement = elements[i];
  46. }
  47. }
  48. return maxElement;
  49. }
  50. };
  51.  
  52. int main() {
  53. Array arr;
  54. arr.addElement(10);
  55. arr.addElement(5);
  56. arr.addElement(20);
  57. arr.addElement(8);
  58.  
  59. arr.display();
  60. int maxElement = arr.findMax();
  61. if (maxElement != -1) {
  62. cout << "Maximum element in the array: " << maxElement << endl;
  63. }
  64.  
  65. return 0;
  66. }
Success #stdin #stdout 0s 5304KB
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