C++ program to find the occurrence of the number in the array using class

Given an array, we have to find the occurrence of the number in the array using the class and object approach.
Submitted by Shubh Pachori, on September 13, 2022

Example:

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

Output: 
Enter Number: 8
Occurrence of the number in array is 2

C++ code to find the occurrence of the 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 arr[10];

  // public member functions
 public:
  // getArray() function to insert 
  // array elements
  void getArray() {
    for (int index = 0; index < 10; index++) {
      cout << "[" << index << "]: ";
      cin >> arr[index];
    }
  }

  // countNumber() function to count the
  // Occurrence of the number
  int countNumber() {
    // initialising int type variable to 
    // perform operations
    int index, number, count = 0;

    // inputting number
    cout << "Enter Number: ";
    cin >> number;

    // for loop to traverse the whole array
    for (index = 0; index <= 9; index++) {
      // if condition to check the occurrence
      // of the number inserted
      if (number == arr[index]) {
        // increment of 1 in count
        // at every iteration
        count++;
      }
    }

    // returning counter
    return count;
  }
};

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

  // int type variable to
  // store the counter
  int count;

  // calling getArray() function to 
  // insert the array
  A.getArray();

  // calling countNumber() function 
  // to find the occurrence of the number
  count = A.countNumber();

  cout << "Occurrence of the number in array is " << count << endl;

  return 0;
}

Output:

[0]: 1
[1]: 2
[2]: 3
[3]: 4
[4]: 5
[5]: 6
[6]: 7
[7]: 1
[8]: 8
[9]: 9
Enter Number: 1
Occurrence of the number in array is 2

Explanation:

In the above code, we have created a class Array, one int type array data members arr[10] to store the elements of the array, and public member functions getArray() and countNumber() to store the array elements and to find the occurance of the number in the array.

In the main() function, we are creating an object A of class Array, reading the inputted array by the user using getArray() function, and finally calling the countNumber() member function to find the occurance of the number in the array. The countNumber() function contains the logic to find the occurance of the number in the array and printing the result.

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


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.