Home »
C++ Programs
C++ program to find the product of the largest number and the smallest number in the array using class
Submitted by Shubh Pachori, on September 05, 2022
Problem statement
Given an array, we have to find the product of the largest number and the smallest number in the array using the class and object approach.
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
Product of the largest and smallest number is 18
C++ code to find the product 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];
}
}
// productArray() function to calculate the
// product of the largest and the
// smallest number in the array
int productArray() {
// initialising int type variables
// to perform operations
int index_1, index_2, temp, product;
// 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 product of the Largest
// and Smallest number in the array
product = array[9] * array[0];
cout << "Smallest Number: " << array[0] << " Largest Number: " << array[9]
<< endl;
// returning the product
return product;
}
};
int main() {
// create an object
Array A;
// int type variable to store
// the product
int product;
// calling getArray() function is called
// by the object to store the array
A.getArray();
// calling productArray() function to calculate product
// of the largest and smallest number in the array
product = A.productArray();
cout << "Product of the largest and smallest number is " << product;
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
Product of the largest and smallest number is 23
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 productArray() to store the given values in the array and to find the product 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 productArray() member function to find the product of the largest and smallest number in the given integer numbers in the given array. The productArray() function contains the logic to find the product of the largest and smallest number in the given numbers and printing the result.
C++ Class and Object Programs (Set 2) »