C++ program to find the difference between the largest and smallest numbers in the array using class

Given an array, we have to find the difference between the largest number and the smallest number in the array using the class and object approach.
Submitted by Shubh Pachori, on September 01, 2022

Example:

Input:
array[0]: 2
array[1]: 4
array[2]: 6
array[3]: 7
array[4]: 5
array[5]: 9
array[6]: 8
array[7]: 4
array[8]: 3
array[9]: 2

Output:
Smallest Number: 2 Largest Number: 9
Difference between largest and smallest number is 7

C++ code to find the difference between the largest and smallest numbers in the array using the class and object approach

#include <iostream>
using namespace std;

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

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

  // differenceArray() function to calculate the
  // difference between the largest and the
  // smallest number in the array
  int differenceArray() {
    // initialising int type variables to perform operations
    int index_1, index_2, temp, difference;

    // for loop to read the whole array
    for (index_1 = 0; index_1 <= 9; index_1++) {
      // for loop to compare numbers of array
      for (index_2 = 0; index_2 < 9 - 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;
        }
      }
    }

    // calculating difference between Largest
    // and the Smallest number in the array
    difference = array[9] - array[0];

    cout << "Smallest Number: " << array[0] << " Largest Number: " << array[9]
         << endl;

    // returning the difference
    return difference;
  }
};

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

  // int type variable to store the difference
  int difference;

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

  // calling differenceArray() function to calculate difference
  // between largest and smallest number in the array
  difference = A.differenceArray();

  cout << "Difference between largest and smallest number is " << difference;

  return 0;
}

Output:

array[0]:1
array[1]:23
array[2]:4
array[3]:5
array[4]:6
array[5]:7
array[6]:8
array[7]:9
array[8]:10
array[9]:4
Smallest Number: 1 Largest Number: 23
Difference between largest and smallest number is 22

Explanation:

In the above code, we have created a class Array, one int type array data member array[10] to store the values, and public member functions putArray() and differenceArray() to store the given values in an array and to find the difference between largest and smallest number in 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 differenceArray() member function to find the difference between largest and smallest number in the given integer numbers in the given array. The differenceArray() function contains the logic to find the difference between the largest and smallest number in the given numbers and printing the result.

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




Comments and Discussions!

Load comments ↻





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