×

C++ Programs

C++ Most popular & Searched Programs

C++ Basic I/O Programs

C++ Constructor & Destructor Programs

C++ Manipulators Programs

C++ Inheritance Programs

C++ Operator Overloading Programs

C++ File Handling Programs

C++ Bit Manipulation Programs

C++ Classes & Object Programs

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

Submitted by Shubh Pachori, on August 26, 2022

Problem statement

Given an array, we have to find the mode of the array using the class and object approach.

Example:

Input:
array[0]: 1
array[1]: 2
array[2]: 33
array[3]: 2
array[4]: 44
array[5]: 1
array[6]: 55
array[7]: 2
array[8]: 66
array[9]: 2

Output:
Mode of Array is 2

C++ code to find the mode 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[10]; // public functions public: // getArray() function to get the // values of the array void getArray() { for (int index = 0; index <= 9; index++) { cout << "array[" << index << "]:"; cin >> array[index]; } } // sortAscending() function to sort the array // in ascending order int mode() { // int type variables for indexing int index_1, index_2; // initialising int type variables to // perform operations int Mode = 0, count = 0; // for loop to traverse the whole array for (index_1 = 0; index_1 <= 9; index_1++) { // reinitialising the count variable count = 0; // for loop to traverse the whole array for (index_2 = 0; index_2 <= 9; index_2++) { // if condition to check if the digit at // index_1 and index_2 of array is equal if (array[index_1] == array[index_2]) { // increment of 1 in count at every iteration count++; } } // if value of count is greater than 0 if (count > 0) { // store the value at index_1 of array in Mode Mode = array[index_1]; } } // returning Mode return Mode; } }; int main() { // create an object Array A; // float type variable to // store mode of the array float mod; // function is called by the object // to store the array A.getArray(); // mode() function is called by the object to // find out the mode of the array mod = A.mode(); cout << "Mode of Array is " << mod; return 0; }

Output

array[0]:1
array[1]:2
array[2]:3
array[3]:4
array[4]:5
array[5]:6
array[6]:7
array[7]:8
array[8]:9
array[9]:1
Mode of Array is 1

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 mode() to store the given values in an array and to find out the mode 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 mode() member function to find out the mode of the array. The mode() function contains the logic to find out the mode of the array and printing the result.

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

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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