Advertisement
electricmaster

Vector homework c++

Jan 25th, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7. const int VECTOR_LENGTH = 10;
  8.  
  9. int min(vector<int> x){
  10.       sort(x.begin(), x.end());
  11.       return x[0];
  12. }
  13.  
  14. int max(vector<int> x){
  15.   sort(x.begin(), x.end());
  16.   return x[x.size() - 1];
  17. }
  18.  
  19. int mode(vector<int> x){
  20.   map<int, int> modemap;
  21.   for(unsigned int i = 0; i < x.size(); i++){
  22.     if(modemap.count(x[i]) == 0)
  23.       modemap.insert(pair<int, int>(x[i], 1));
  24.     else modemap.find(x[i])->second++;
  25.   }
  26.  
  27.   int number = 0;
  28.   int count = 0;
  29.   for(pair<int, int> p : modemap){
  30.     if(p.second > count){
  31.       count = p.second;
  32.       number = p.first;
  33.     }
  34.   }
  35.   return number;
  36. }
  37.  
  38. int main(){
  39.   vector<int> x;
  40.   for(int i = 0; i < VECTOR_LENGTH; i++ ){
  41.     cout << "Please enter an integer" << endl;
  42.     int point;
  43.     cin >> point;
  44.     x.push_back(point);
  45.   }
  46.  
  47.   cout << "The min of the vector is " << min(x) << endl;
  48.   cout << "The max of the vector is " << max(x) << endl;
  49.   cout << "The mode of the vector " << mode(x) << endl;
  50.   getchar();
  51.   return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement