Trouble with Boolean ( Linear and binary search )

I am working on a program that finds a value within a list through either binary or linear search. I based it from the algorithm in a book. The algorithm worked fine until I had this problem:

Whether the value is in the list or not, it always prints "The integer x was not found in the list".

In my code, when the item has already been found, I set the boolean value found to true. Then after a loop, I have the if-condition statement that prints the index where the item was found ( if true ) and prints otherwise that it was not found.

Here are the outputs I get:

Initial List:
        1 2 3 4 5 6 7 8 9 10

COMMAND: L
|Linear Search|

        Enter a value to search: 3
1 ->2 3 4 5 6 7 8 9 10

1 2 ->3 4 5 6 7 8 9 10

The integer 3 was not found in the list.
COMMAND: L
|Linear Search|

        Enter a value to search: 12
1 ->2 3 4 5 6 7 8 9 10

1 2 ->3 4 5 6 7 8 9 10

1 2 3 ->4 5 6 7 8 9 10

1 2 3 4 ->5 6 7 8 9 10

1 2 3 4 5 ->6 7 8 9 10

1 2 3 4 5 6 ->7 8 9 10

1 2 3 4 5 6 7 ->8 9 10

1 2 3 4 5 6 7 8 ->9 10

1 2 3 4 5 6 7 8 9 ->10

1 2 3 4 5 6 7 8 9 10

The integer 12 was not found in the list.


If you notice at the first case, 3 was obviously in the list, and the printing ended when it was found. But then it printed that it was not.

Here's another one for the binary search:

Initial List:
        1 2 3 4 5 6 7 8 9 10

COMMAND: B
|Binary Search|

        Enter a value to search: 9
1 2 3 4 ->5 6 7 8 9 10

6 7 ->8 9 10

The integer 9 was not found in the list.
COMMAND: B
|Binary Search|

        Enter a value to search: 12
1 2 3 4 ->5 6 7 8 9 10

6 7 ->8 9 10

->9 10

->10

The integer 12 was not found in the list.


Same thing happens.

Here are the code snippets I used to execute the search:

Linear.
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
void Linear( int item ) {
		bool found = false;
		loc = 0;
		
		while ( loc < n && found == false ) {
			if ( item == list[ loc ] ) {
				found == true;
				break;
			}
			else {
				loc++;
			
				for ( int l = 0; l < n; l++ ) {
					if ( l == loc ) {
						cout << "->";
						outf << "->";
					}
					cout << list[ l ] << " ";
					outf << list[ l ] << " ";
				}
				cout << "\n\n";
				outf << "\n\n";
			}
		}
		
		if ( !found ) {
			cout << "The integer " << item << " was not found in the list.\n"; 
			outf << "The integer " << item << " was not found in the list.\n"; 
		}
		else {
			cout << "The integer " << item << " was found in index " << loc << " of the list.\n"; 
			outf << "The integer " << item << " was found in index " << loc << " of the list.\n"; 
			for ( int l = 0; l < n; l++ ) {
				if ( l == loc ) {
					cout << "[";
					outf << "[";
				}
				cout << list[ l ] << " ";
				outf << list[ l ] << " ";
				
				if ( l == loc ) {
					cout << "]";
					outf << "]";
				}
			}
			cout << "\n\n";
			outf << "\n\n";
		}	
	}


Binary.

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
void Binary( int item ) {
		bool found = false;
		int first = 0;
		int last = n - 1;
		loc = 0;
		
		while ( first <= last && found == false ) {
			loc = ( first + last ) / 2;
			
			if ( item != list[ loc ] ) {
				for ( int b = first; b <= last; b++ ) {
					if ( b == loc ) {
						cout << "->";
						outf << "->";
					}
					cout << list[ b ] << " ";
					outf << list[ b ] << " ";	
				}
				cout << "\n\n";
				outf << "\n\n";
				if ( item < list[ loc ] )
					last = loc - 1;
				else if ( list[ loc ] < item )
					first = loc + 1;
			}
			else {
				found == true;
				break;
			}
		}
		if ( !found ) {
			cout << "The integer " << item << " was not found in the list.\n"; 
			outf << "The integer " << item << " was not found in the list.\n"; 
		}
		else {
			cout << "The integer " << item << " was found in index " << loc << " of the list.\n"; 
			outf << "The integer " << item << " was found in index " << loc << " of the list.\n"; 
			
			for ( int b = 0; b < n; b++ ) {
				if ( b == loc ) {
					cout << "[";
					outf << "[";
				}
				cout << list[ b ] << " ";
				outf << list[ b ] << " ";
				
				if ( b == loc ) {
					cout << "]";
					outf << "]";
				}
			}
			cout << "\n\n";
			outf << "\n\n";
		}
	}


This are just fragments of the entire program so if some variables seem undeclared, it's either it is a parameter of the function or it was declared in other parts of the program.

It seems that the else part after the while-loop in each of the functions is being ignored always. The else part also includes a final look at the list and where the value is found.

So what do you guys think went wrong? Why does it always display that the item was not found even if it was found? Thanks a lot for all the help!
Last edited on
1
2
3
4
5
while ( loc < n && found == false ) {
			if ( item == list[ loc ] ) {
				found == true;
				break;
			}


should be :

1
2
3
4
5
while ( loc < n && found == false ) {
			if ( item == list[ loc ] ) {
				found = true; //you set found to true
				break;
			}


same for :
1
2
3
4
else {
				found = true;
				break;
			}

1
2
3
4
5
while ( loc < n && found == false ) {
if ( item == list[ loc ] ) {
found == true;
break;
}


should be :

1
2
3
4
5
while ( loc < n && found == false ) {
if ( item == list[ loc ] ) {
found = true; //you set found to true
break;
}


same for :
1
2
3
4
else {
found = true;
break;
}


Thanks a lot! Didn't notice that.
Topic archived. No new replies allowed.