Advertisement
purxiz

code to find

Nov 23rd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. int main () {
  7.   ifstream f;
  8.   f.open ("message_revised.txt");
  9.   char b[9] = "";
  10.   vector<vector<int>> combo;
  11.   int count = 0;
  12.   while(f.read(b, sizeof(b) - 1)){
  13.     int found = 0;
  14.     //printf("%s\n", b);
  15.     for(int j = 0; j < combo.size(); j++){
  16.       //printf("checking for equality %d == %d\n", atoi(b), combo[j][0]);
  17.       if (atoi(b) == combo[j][0]) {
  18.           combo[j][1]++;
  19.           found = 1;
  20.       }
  21.     }
  22.     if(found == 0){
  23.       //printf("no match was found\n");
  24.       combo.push_back(vector<int> ());
  25.       combo[count].push_back(atoi(b));
  26.       combo[count++].push_back(1);
  27.     }
  28.     f.seekg(2, f.cur);
  29.   }
  30.   vector<int> max;
  31.   vector<int> pos;
  32.   for (int k = 0; k < combo.size(); k++) {
  33.     max.push_back(-5000000);
  34.     pos.push_back(0);
  35.     for(int i = 0; i < combo.size(); i++){
  36.       //printf("%d\n", combo[i][0]);
  37.       if(combo[i][1] > max[k]){
  38.         max[k] = combo[i][1];
  39.         pos[k] = i;
  40.       }
  41.     }
  42.     printf("%d occured %d times\n", combo[pos[k]][0], max[k]);
  43.     combo[pos[k]][1] = -1;
  44.   }
  45.   f.close();
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement