Binary Search (Int)

Jan 6, 2021 at 4:06am
I'm writing a code about getting to know if the customer cards are inside the sorted list of cards of my cards.txt. The issue is that I do not find an error on my binarySearch function but it does not return me the "isFound". I tried a linear search it works perfectly, so here my binarySearch is the issue. What's wrong? What did I miss? Or maybe, is my version of C++, not the right one to use in that case? (I'm using C++98, on VSC)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream> // preprocessor directive //
#include <fstream>  // library for file (create/open/read)
#include <string>
using namespace std;

// linear Search which check if the name of frauder (customer) is inside the list of frauder
void search(int array[], int n, int x)
{
    for (int i = 0; i < n; i++)
    {
        if (array[i] == x)
        {
            cout << "This credit card is inside the list" << endl;
        }
    }
}

//returns true if value is in list.  assumes list sorted.
bool binarySearch(const int list[], int size, int value)
{
    int first = 0, last = size - 1, middle;
    bool isFound = false;
    while (!isFound && first <= last)
    {
        middle = (first + last) / 2;
        if (list[middle] == value)
            isFound = true;
        else if (list[middle] > value) // lower half
            last = middle - 1;
        else
            first = middle + 1; // upper half
    }
    return isFound;
}

int main()
{
    const int numCards = 20;
    ifstream readFile;
    int cards[numCards], customerCard;

    readFile.open("cards.txt");

    // check if file exist
    if (!readFile)
    {
        cout << "ERROR: Invalid filename\n";
        return 1;
    }

    for (int i = 0; i < numCards && readFile >> cards[i]; i++)
    {
        cout << cards[i] << '\n';
    }

    readFile.close();

    cout << "What credit card, would you like to use ? " << endl;
    cin >> customerCard;

    // search(cards, numCards, customerCard);
    binarySearch(cards, numCards, customerCard);

    return 0;
}


Here the cards.txt file:

128904
212323
250202
279711
323435
352347
390121
399191
424090
425011
439023
444444
489201
598264
623109
799090
811424
902123
920943
955010
Last edited on Jan 6, 2021 at 4:19am
Jan 6, 2021 at 5:19am
It does return the found status.

You're just not using the result.
1
2
3
    if ( binarySearch(cards, numCards, customerCard) ) {
      cout << "Yeah baby!" << endl;
    }


$ g++ foo.cpp
$ ./a.out 
128904
212323
250202
279711
323435
352347
390121
399191
424090
425011
439023
444444
489201
598264
623109
799090
811424
902123
920943
955010
What credit card, would you like to use ? 
444444
Yeah baby!

Jan 6, 2021 at 5:35am
What should I change to make use of that?

I put the return outside the while bracket so if it finds it should show me the status right? since I call the function correctly in the main
Last edited on Jan 6, 2021 at 5:35am
Jan 6, 2021 at 5:41am
return statements don't "show" you anything.

cout statements show you things.

Compare your two functions.
- linear search has a cout, and returns void
- binary search has no cout and returns bool

> since I call the function correctly in the main
That's just it - you don't.
Well the call is correct, you're just not making any use of the bool result that comes back.


1
2
3
4
5
6
7
8
9
10
11
12
13
// linear Search which check if the name of frauder (customer) is inside the list of frauder
bool search(int array[], int n, int x)
{
    for (int i = 0; i < n; i++)
    {
        if (array[i] == x)
        {
            // cout << "This credit card is inside the list" << endl;
            return true;
        }
    }
    return false;
}


In main
1
2
3
4
5
6
    if ( search(cards, numCards, customerCard) ) {
        cout << "Yeah linear baby!" << endl;
    }
    if ( binarySearch(cards, numCards, customerCard) ) {
        cout << "Yeah binary baby!" << endl;
    }

Jan 6, 2021 at 5:55am
Understood, thanks
Topic archived. No new replies allowed.