Finding a number in an array

Im trying to find the number 197 in the array but even when its there, it says that it is not. I can't find what is wrong with my code. What do you guys think?

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  

#include <iostream>
#include <ctime>


using namespace std;

void SelectionSortA(int RandomNums[], int size)
{
    int startscan, maxIndex, maxValue;
    for (startscan = 0; startscan < (size - 1); startscan++)
    {
        maxIndex = startscan;
        maxValue = RandomNums[startscan];
        for (int index = startscan + 1; index < size; index++)
        {
            if (RandomNums[index] > maxValue)
            {
                maxValue = RandomNums[index];
                maxIndex = index;
            }
        }
        RandomNums[maxIndex] = RandomNums[startscan];
        RandomNums[startscan] = maxValue;
    }
    
    for(int count = 0; count < size; count++)
    {
        cout << RandomNums[count] << endl;
    }
}

int binarySearch (int RandomNums[], int size, int value)
{
    int first = 0, last = size - 1, middle, position = -1;
    bool found = false;
    
    while (!found && first <= last)
    {
        middle = (first + last) / 2;
        if (RandomNums[middle] == value)
        {
            found = true;
            position = middle;
        }
        else if (RandomNums[middle] > value)
        {
            last = middle - 1;
        }
        else
        {
            first = middle + 1;
        }
    }
    
    return position;
}


int main()
{
    
    int size = 100, RandomNums[size], results;
    
    srand((unsigned)time(0));
    
    cout << "Here is the RandomNums Array: " << endl;
    for(int count = 0; count < size; count++)
    {
        RandomNums[count] = (rand() % 1000);
        cout << RandomNums[count] << endl;
    }
    
    cout << endl << "Here is the RandomNums Array in decreasing order: " << endl;
    SelectionSortA(RandomNums, 100);
    
    cout << endl << "The program will search for the integer 197 in the RandomNums Array. " << endl << endl;
    int value = 197;
    results = binarySearch(RandomNums, 100, value);
    
    if (results == -1)
        cout << "The integer was not found in the RandomNums Array. " << endl;
    else
        cout << "The integer was found in element " << results << " of the RandomNums Array. " << endl;
    
    
    return 0;
}
Two problems:

1) You're calling srand(). This will cause the RNG to return a different sequence of random numbers each time the program is run. You have no guarantee that 197 will be returned by the RNG.

2) You're sorting the array into descending order, however, your binary search is expecting the array in ascending order. Let's assume the middle number is 501. Your binary search is going to assume that 197 is in the bottom half the array, while in reality (if it exists in the array at all), it would be in the upper half.
Thank you! I fixed it already.
Topic archived. No new replies allowed.