Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. std::vector<std::vector<int>> multiplicity(std::vector<int> arr)
  7. {
  8. // declare the return vector which will be used store the processed information (number, count)
  9. std::vector<int> returnVector(2);
  10.  
  11. // declare output which will contain vectors
  12. std::vector<vector<int>> output;
  13.  
  14. // track working number and count
  15. int working_n, working_c;
  16.  
  17. // initialise working number and count
  18. working_n = arr[0];
  19. working_c = 1;
  20.  
  21. // iterate through rest of arr and calculate counts
  22. for (int i = 1; i < arr.size(); i++)
  23. {
  24. cout << "arr[" << i << "]: " << arr[i] << endl;
  25.  
  26. if (arr[i] == working_n)
  27. {
  28. working_c++;
  29. cout << "working_c++: " << working_c << endl;
  30.  
  31. }
  32. else
  33. {
  34. // add results to returnVector
  35. returnVector[0] = working_n;
  36. returnVector[1] = working_c;
  37. // reset working variables
  38. working_n = arr[i];
  39. working_c = 1;
  40.  
  41. cout << "working_n :" << working_n << " · ";
  42. cout << "working_c :" << working_c << endl;
  43.  
  44. }
  45. // push back results of returnVector to output
  46. output.push_back(returnVector);
  47. }
  48. return output;
  49. }
  50.  
  51. int main(int argc, char const *argv[])
  52. {
  53. vector<int> array = {5,5,5,1,1,5,5,3};
  54. for (const auto &vector : multiplicity(array))
  55. {
  56. cout << vector[0] << ", " << vector[1] << "|";
  57. }
  58. cout << endl;
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement