Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- std::vector<std::vector<int>> multiplicity(std::vector<int> arr)
- {
- // declare the return vector which will be used store the processed information (number, count)
- std::vector<int> returnVector(2);
- // declare output which will contain vectors
- std::vector<vector<int>> output;
- // track working number and count
- int working_n, working_c;
- // initialise working number and count
- working_n = arr[0];
- working_c = 1;
- // iterate through rest of arr and calculate counts
- for (int i = 1; i < arr.size(); i++)
- {
- cout << "arr[" << i << "]: " << arr[i] << endl;
- if (arr[i] == working_n)
- {
- working_c++;
- cout << "working_c++: " << working_c << endl;
- }
- else
- {
- // add results to returnVector
- returnVector[0] = working_n;
- returnVector[1] = working_c;
- // reset working variables
- working_n = arr[i];
- working_c = 1;
- cout << "working_n :" << working_n << " · ";
- cout << "working_c :" << working_c << endl;
- }
- // push back results of returnVector to output
- output.push_back(returnVector);
- }
- return output;
- }
- int main(int argc, char const *argv[])
- {
- vector<int> array = {5,5,5,1,1,5,5,3};
- for (const auto &vector : multiplicity(array))
- {
- cout << vector[0] << ", " << vector[1] << "|";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement