C++ program to find the median of the array using class

Given an array, we have to find the median of the array using the class and object approach.
Submitted by Shubh Pachori, on August 26, 2022

Example:

Input:
array[0]: 1
array[1]: 2
array[2]: 3
array[3]: 4
array[4]: 5

Output:
Median of the array is 3 

C++ code to find the median of the array using the class and object approach

#include <iostream>
using namespace std;

// create a class
class Array {
  // private data member
  private:
    int array[5];

  // public functions
  public:
    // getArray() function to get 
    // the value of the array
    void getArray() {
      for (int index = 0; index <= 4; index++) {
        cout << "array[" << index << "]: ";
        cin >> array[index];
      }
    }

  // median() function to find out the 
  // median of the array
  double median() {
    // initialising int type variables to 
    // perform operations
    int index_1, index_2, temp, med;

    // float type variable to store sum
    float sum = 0;

    // for loop to read the whole array
    for (index_1 = 0; index_1 <= 4; index_1++) {
      // for loop to compare numbers of array
      for (index_2 = 0; index_2 < 4 - index_1; index_2++) {
        // if condition to check if the next term is smaller than
        // this then swapping takes place
        if (array[index_2] > array[index_2 + 1]) {
          //  swapping numbers if numbers
          //  are not in the order
          temp = array[index_2];
          array[index_2] = array[index_2 + 1];
          array[index_2 + 1] = temp;
        }
      }
    }

    // if condition to check number of elements of the array
    // if it is odd then it will comes in this condition
    if (index_1 % 2 != 0) {
      // divided index_1 by 2 and store it in med
      med = index_1 / 2;

      // returning the digit at array[med]
      return array[med];
    }
    // else it will comes in else part
    else {
      // divided index_1 by 2 and store it in med
      med = index_1 / 2;

      // add digit at med and last digit from med
      sum = array[med] + array[med - 1];

      // divide sum by 2
      sum = sum / 2;

      // returning the sum
      return sum;
    }
  }
};

int main() {
  // create an object
  Array A;

  // float type variable to store the median
  float med;

  // function is called by the object 
  // to store the array
  A.getArray();

  // median() function is called by the object to 
  // find out the median of the array
  med = A.median();

  cout << "Median of Array is " << med;

  return 0;
}

Output:

RUN 1:
array[0]: 1 
array[1]: 22
array[2]: 333
array[3]: 4444
array[4]: 55555
Median of the array is 333

RUN 2:
array[0]: 10
array[1]: 20
array[2]: 30
array[3]: 40
array[4]: 50
Median of Array is 30

Explanation:

In the above code, we have created a class Array, one int type array data member array[5] to store the values, and public member functions putArray() and median() to store the given values in an array and to find out the median of the array.

In the main() function, we are creating an object A of class Array, reading integer values by the user of the array using the putArray() function, and finally calling the median() member function to find out the median of the array. The median() function contains the logic to find out the median of the array and printing the result.

C++ Class and Object Programs (Set 2) »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.