I have the following program, the programs purpose is to display how many times each value in the list vector occurred.
if the tuple 2:3 occurs 3 times in the vector, then the program displays this to the user.
Expected output
- 0:8 occurred 1 time %x
- 2:3 occurred 3 time %x
- 9:5 occurred 2 time %x
- 8:9 occurred 1 time %x
Actual Output:
- 2:3 occurred 3 time %42
- 8:9 occurred 1 time %14
- 9:5 occurred 3 time %42
Any idea what I'm doing incorrectly? Here's a complete and verifiable working version of the code I'm using
#include <vector>
#include <iostream>
#include <tuple>
using namespace std;
int counter = 0;
double percentage;
int val = 0;
vector<tuple<int, int>> list = { make_tuple(2, 3), make_tuple(0, 8), make_tuple(2, 3), make_tuple(8, 9), make_tuple(9, 5), make_tuple(9, 5), make_tuple(2, 3) };
int binarysearch(vector<tuple<int, int>> list, int low, int high, tuple<int, int> number)
{
int index = low;
int mid = 0;
// loop till the condition is true
while (low <= high) {
// divide the array for search
mid = (low + high) / 2;
if (list.at(mid) > number) {
high = mid - 1;
}
else {
low = mid + 1;
}
}return (high - index + 1);
}
int main()
{
while (counter <= list.size() - 1) {
val = binarysearch(list, counter, list.size() - 1, list.at(counter));
percentage = val * 100 / list.size();
cout << "Value: " << get<0>(list.at(counter)) << ":" << get<1>(list.at(counter)) << " Occurs: " << val << " Time(s)" << " %" << percentage << endl;
counter += val;
}
return 0;
}