-2

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;
         }
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mitch89
  • 59
  • 7
  • 1
    Any observations when stepping through your code with the debugger? – πάντα ῥεῖ Apr 20 '16 at 19:39
  • nothing that I can see, looked a few times now. – Mitch89 Apr 20 '16 at 19:45
  • I am voting to close this as asking general debugging help AND not doing it yourself, instead asking on SO then deleting the Q to ask the next Q till you get it done (assignment?). – Khalil Khalaf Apr 20 '16 at 19:57
  • Ah, I think I need to organise the array first. – Mitch89 Apr 20 '16 at 19:57
  • tbh, I removed those questions myself because I worked out the issue -- I got one piece of beneficial advice about the percentage, but nothing else. – Mitch89 Apr 20 '16 at 19:58
  • Which was the answer I posted to your question. Then you deleted that question to post this question which has no effort shown except pasting that "beneficial advice". This is not proper on SO and now here you ONLY have to debug it line by line – Khalil Khalaf Apr 20 '16 at 20:05
  • I just figured out the issue, I need to organise it, just no idea how to organise a tuple array. – Mitch89 Apr 20 '16 at 20:09

1 Answers1

1

You cannot run a binary search on an unsorted container. A binary search relies on the fact that if the midpoint is not the element you want then the element you want will be in the top half if it is more than the midpoint and the bottom half if it is less. You cannot guarantee that with an unsorted container.

Now instead of writing your own functions to get the number of each occurrence you can use a std::map to do that for you like

std::vector<std::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) };
std::map<std::tuple<int, int>, int> occurrences;
for (const auto& e : list) // go though the vector and add to the map.  increment the value on duplication
    ++occurrences[e];

for (const auto& e : occurrences)
{
    double percentage = e.second * 100 / list.size();
    cout << "Value: " << get<0>(e.first) << ":" << get<1>(e.first) << " Occurs: " << e.second << " Time(s)" << " %" << percentage << endl;
}

Which outputs:

Value: 0:8 Occurs: 1 Time(s) %14
Value: 2:3 Occurs: 3 Time(s) %42
Value: 8:9 Occurs: 1 Time(s) %14
Value: 9:5 Occurs: 2 Time(s) %28
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • I used a bubble sort to sort the array, then I used my binary search and it worked. I'll take maps into consideraiton next time though. – Mitch89 Apr 20 '16 at 20:54