Search function causes program to hang

My code works until the isInArray function finds the value its looking for. If the value is present in the array, the program hangs. Por que?

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
90
91
92
93
94
95
96
97
98
99
/* 
 * File:   main.cpp
 * Author: Roark
 *
 * Created on October 5, 2014, 5:42 PM
 */

#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <ctime>
using namespace std;

const int ArraySize = 100;
void print(int array[]);
int * populate(int array[]);
int * sort(int array[]);
bool isInArray(int array[],int i);
int main()
{
    int array[ArraySize];

    // Assign random numbers between 1 and 100 to each element of the array
    populate(array);

    print(array);
    cout << endl;

    sort(array);
    
    cout << endl << endl << endl;

    print(array);
    cout << endl;

    // check the array for values 0, 25, 50, 75, 100
    for (int i = 0; i <= 100; i += 25)
    {
        cout << i << " is ";
        if (!isInArray(array,i)) cout << "not ";
        cout << "in the array" << endl;
    }
}

void print(int array[]) //print array to terminal
{
    int counter = 0;
    while (counter < ArraySize)
    {
    cout << array[counter] << "  ";
    counter++;
    }
}
int * populate(int array[]) //assign a random value to array elements
{
    int counter = 0;
    while (counter < ArraySize)
    {
        array[counter] = rand() %100;
        counter++;
    }
    return array;
}
int * sort(int array[]) //organize the array with a sort function
{
    int hold;
    int count = 0;
    int bookmark = 0;
    while (count < 100)
    {
        hold = array[count];  //hold the first unsorted element of the array
        while (bookmark < 100)
        {
            if (array[bookmark] > hold) //if element is greater than held element, swap them
            {
                array[count] = array[bookmark];  
                array[bookmark] = hold;
                hold = array[count];
            }
            bookmark++; //advance to next unsorted element
        }
        count++; //keep track of how far sort has progressed
        bookmark = 0; //reset from where sort begins
    }
    return array;
}
bool isInArray(int array[],int i) //search array for integer
{
    bool found = false;
    int count;
    while (count < 100)
    {
        if (i == array[count])
        {found = true;}
        else
        {count++;}
    }
    return found;
}
I think you'll need to assign some value to counter
int counter=0//or something
It hangs because you are not increasing the index of the array when the function finds the integer. You might want to consider terminating the function once the integer is found by returning true immediately and setting the function to return false by default.
Also since the array is sorted, you can use binary search to look for the number; this is faster but requires more code
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isInArray(int array[],int i) //search array for integer
{
    bool found = false;
    int count;
    while (count < 100)
    {
        if (i == array[count])
        {found = true;}
        else
        {count++;}
    }
    return found;
}

Look at what happens when i == array[count]. It sets found to true, but then just keep repeating this since it will never again reach that else statement. count will never be incremented once i == array[count], so you'll never get out of the while loop.
Last edited on
@Smac89 and @Ganado

That worked!

1
2
3
4
5
6
7
8
9
10
11
12
{
    bool found = false;
    int count;
    while (count < 100)
    {
        if (i == array[count])
        {found = true;
        count++;}
        else
        {count++;}
    }
    return found;

I set count to increment every cycle, and now it works. Thanks!
Topic archived. No new replies allowed.