Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include<vector>
  2. #include<iostream>
  3.  
  4. using namespace std;
  5.  
  6. vector<int> modes(vector<int>);
  7. int largest(int*,int);
  8.  
  9. int main() {
  10.     vector<int> nums;
  11.     while(true) {
  12.         int num;
  13.         cout << "Enter a number between 1-20. (Input -1 to stop.)\n> ";
  14.         cin >> num;
  15.         if (num == -1) {
  16.             break;
  17.         } else {
  18.             nums.push_back(num);
  19.         }
  20.     }
  21.     vector<int> mosts = modes(nums);
  22.     for (int i = 0;i < mosts.size();i++) {
  23.         cout << "Found a mode: " << mosts[i]+1 << endl;
  24.     }
  25. }
  26.  
  27. vector<int> modes(vector<int> nums) {
  28.     int length = nums.size();
  29.     int amounts[20] = {};
  30.     for (int i = 0;i < 20;i++) {
  31.         amounts[i] = 0;
  32.     }
  33.     for (int j = 0;j < nums.size();j++) {
  34.         int num = nums[j];
  35.         amounts[num] += 1;
  36.     }
  37.     for (int i = 0;i < 20;i++) {
  38.         int amount = amounts[i];
  39.         cout << "amount for " << i+1 << ": " << amount << endl;
  40.     }
  41.     int most = largest(amounts,20);
  42.     cout << most << endl;
  43.     vector<int> mosts;
  44.     for (int i = 0;i < 20;i++) {
  45.         int amount = amounts[i];
  46.         if (amount == most) {
  47.                 cout << "found one" << endl;
  48.             mosts.push_back(i);
  49.         }
  50.     }
  51.     return mosts;
  52. }
  53.  
  54. int largest(int arr[], int n)
  55. {
  56.     int i;
  57.  
  58.     // Initialize maximum element
  59.     int max = arr[0];
  60.  
  61.     // Traverse array elements
  62.     // from second and compare
  63.     // every element with current max
  64.     for (i = 1; i < n; i++)
  65.         if (arr[i] > max)
  66.             max = arr[i];
  67.  
  68.     return max;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement