Advertisement
Lawnknome

findMode.cpp

Nov 23rd, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <vector>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. vector<int> modeVect(vector<int>& vect);
  8.  
  9. int main()
  10. {
  11.     vector<int> values;             //vector of ints generated by user input
  12.     int num;                        //number added to vector
  13.     char again;                     //continue entering values to the vector
  14.  
  15.     cout << "\nThis program will find the mode(s) of a list given by you." << endl;
  16.    
  17.     do
  18.     {
  19.         cout << "Please enter an integer to add to the list: ";
  20.        
  21.         while(!(cin >> num))
  22.         {
  23.             cin.clear();
  24.             cin.ignore(256, '\n');
  25.             cout << "ERROR\n";
  26.             cout << "Please enter a valid integer to add to the list: ";
  27.         }  
  28.  
  29.         values.push_back(num); 
  30.  
  31.         cout << "Would you like to add another integer to the list? (Y/N): ";
  32.         cin >> again;
  33.  
  34.     }while (again == 'Y' || again == 'y'); 
  35.  
  36.     vector<int> mode(modeVect(values));
  37.  
  38.     cout << "\n\nThe mode(s) calculated: ";
  39.  
  40.     for(int i =0; i < mode.size(); i++)
  41.     {
  42.         cout << mode[i] << " ";
  43.     }  
  44.  
  45.     cout << "\n\n";
  46.  
  47.     return 0;
  48. }
  49.  
  50. vector<int> modeVect (vector<int>& vect)
  51. {
  52.     sort(vect.begin(), vect.end());
  53.  
  54.     int current;                //current number being tallied
  55.     int count = 0;              //iteration
  56.     int old_freq = 0;           //compare to older mode
  57.     vector<int> mode;
  58.  
  59.     do{
  60.         int frequency = 0;      //frequency a number appears
  61.         current = vect[count];
  62.  
  63.         for(int i = 0; i < vect.size(); i++)
  64.         {
  65.             if(vect[i] > current)
  66.                 break;
  67.  
  68.             if(vect[i] == current)
  69.             {
  70.                 frequency++;
  71.             }  
  72.         }
  73.  
  74.         if(frequency == old_freq)
  75.         {
  76.             for(int j = 0; j < mode.size(); j++)
  77.             {
  78.                 if(mode[j] == current)
  79.                     goto same;
  80.             }  
  81.             mode.push_back(current);
  82.         }
  83.        
  84.         if(frequency > old_freq)
  85.         {
  86.             mode.clear();
  87.             mode.push_back(current);
  88.             old_freq = frequency;
  89.         }
  90.        
  91.         same:
  92.         count++;
  93.  
  94.     }while (count < vect.size());      
  95.  
  96.     return mode;
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement