Advertisement
Maco153

Array Occurrence

Nov 13th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int getOccurrence(int arr[], int size, int num) {
  4.     int count(0);
  5.     for (int i = 0; i < size; i++) {
  6.         if (num == arr[i])
  7.             count++;
  8.     }
  9.     return count;
  10. }
  11.  
  12.  
  13.  
  14. int main() {
  15.  
  16.     int array[100], size(0), num(0); int times = 0;
  17.     cout << "Enter the size of the array!\n";
  18.     cin >> size;
  19.     cout << "Enter your array\n";
  20.     for (int i = 0; i < size; i++) {
  21.         cin >> array[i];
  22.     }
  23.     cout << "Enter the number you want to calculate how many times it occurred in the array\n";
  24.     cin >> num;
  25.  
  26.     times= getOccurrence(array, size, num);
  27.    
  28.     cout << "The number of times the number " << num << " occurred is " << times << endl;
  29.     return 0;
  30.  
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement