Write a C++ program using the concept of OOP to find the second largest element in an array


 Write a C++ program using the concept of OOP to find the second-largest element in an array


Algorithm:

  1. Create a class called "Array"
  2. Define variables for size and a pointer to an array to store the elements
  3. Create a constructor to initialize the size and allocate memory for the array
  4. Create a method called "input" to take input from the user and store it in the array
  5. Create a method called "sort" to sort the array in descending order
  6. Create a method called "secondLargest" to find the second largest element in the array
  7. Create an object of the Array class and use the methods to find the second largest element

C++ Program:

#include <iostream>
using namespace std;
 
class Array {
    int size;
    int *arr;
  public:
    Array(int s) {
        size = s;
        arr = new int[size];
    }
    void input() {
        cout << "Enter elements of array: " << endl;
        for(int i = 0; i < size; i++)
            cin >> arr[i];
    }
    void sort() {
        for(int i = 0; i < size; i++) {
            for(int j = i+1; j < size; j++) {
                if(arr[i] < arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
    }
    void secondLargest() {
        cout << "The second largest element is: " << arr[1] << endl;
    }
    ~Array() {
        delete[] arr;
    }
};
 
int main() {
    int s;
    cout << "Enter the size of the array: ";
    cin >> s;
    Array a(s);
    a.input();
    a.sort();
    a.secondLargest();
    return 0;
}
 
 
/*
======Input/Output=====
Enter the size of the array: 5
Enter elements of array: 
10 20 30 40 50
 
 
The second largest element is: 40
*/

Explanation:

  • The user enters 5 for the size of the array, indicating that the array has 5 elements.
  • The user then enters the elements of the array: 10 20 30 40 50
  • The program creates an Array object, sorts the array in descending order, and finds the second largest element which is 40 and prints the result.

Another way to find the second largest element:

#include <iostream>
#include <climits>
using namespace std;
 
class Array {
    int size;
    int *arr;
  public:
    Array(int s) {
        size = s;
        arr = new int[size];
    }
    void input() {
        cout << "Enter elements of array: " << endl;
        for(int i = 0; i < size; i++)
            cin >> arr[i];
    }
    void findSecondLargest() {
        int firstLargest = INT_MIN, secondLargest = INT_MIN;
        for(int i = 0; i < size; i++) {
            if(arr[i] > firstLargest) {
                secondLargest = firstLargest;
                firstLargest = arr[i];
            } else if(arr[i] > secondLargest && arr[i] < firstLargest) {
                secondLargest = arr[i];
            }
        }
        cout << "The second largest element is: " << secondLargest << endl;
    }
    ~Array() {
        delete[] arr;
    }
};
 
int main() {
    int s;
    cout << "Enter the size of the array: ";
    cin >> s;
    Array a(s);
    a.input();
    a.findSecondLargest();
    return 0;
}
 
/*
======Input/Output=====
 
Enter the size of the array: 5
Enter elements of array: 
10 20 30 40 50
 
The second largest element is: 40
*/

#OOP
#HappyProgramming
#HappyCoding

No comments:

Post a Comment