C++ program to find the sum of the largest number and the smallest number in the array using class

Given an array, we have to find the sum of the largest number and the smallest number in the array using the class and object approach.
Submitted by Shubh Pachori, on September 06, 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
Sum of the largest and smallest number is 11

C++ code to find the sum of the largest number and the smallest number 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];
    }
  }

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

    // 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 sum of the Largest
    // and Smallest number in the array
    sum = array[9] + array[0];

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

    // returning the sum
    return sum;
  }
};

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

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

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

  // calling sumArray() function to calculate sum
  // of the largest and smallest number in the array
  sum = A.sumArray();

  cout << "Sum of the largest and smallest number is " << sum;

  return 0;
}

Output:

RUN 1:
array[0]: 1
array[1]: 2
array[2]: 3
array[3]: 4
array[4]: 5
array[5]: 66
array[6]: 77
array[7]: 88
array[8]: 10
array[9]: 15
Smallest Number: 1 Largest Number: 88
Sum of the largest and smallest number is 89

RUN 2:
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
Sum of the largest and smallest number is 24

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 sumArray() to store the given values in an array and to find the sum of the 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 sumArray() member function to find the sum of the largest and smallest number in the given integer numbers in the given array. The sumArray() function contains the logic to find the sum of 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.