searching a number in an int array

Hi,

I am relatively new to programming in general and C++ in particular and starting to learn, using the Malik book. Currently I am doing an exercise using a sequential search algorithm (Chapter 9).

Basically I initialize an int array of size 10 with zeros. Then element 7 is changed to 1. Via cin I collect a user input (integer) which is the number to be searched.

Now the problem is that the search function from the book is somehow not working. I added a function to see how the array looks like and there is definitely one element with value 1 that should be found, but it simply isn't. The function always returns -1 (element not found).

The program compiles, but doesn't work as it is supposed to.
I cannot find the mistake, please help :)


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
//********************************************************
// search algorithm for arrays
//********************************************************

#include <iostream>

using namespace std;

void initializeArray(int list[], int listSize);				
int seqSearch(int list[], int listLength, int searchItem);		
void printArray(int list[], int listSize);								
	
int main()
{
	int maximum = 10;
	int array[maximum];
	int result;
	int input;
	
	cout << "Enter number to search in array!" << endl;
	cin >> input;
	
	// set all array elements to 1;
	initializeArray(array, maximum);
	cout << "before change" << endl;
	printArray(array, maximum);
	
	// change array element No. 7 to 0
	array[7] = 1;
	cout << "after change" << endl;
	printArray(array, maximum);
	
	// search user input in array
	result = seqSearch(array, maximum, input);
	
	if (result = -1)
		cout << "Item not found!" << endl;
	else
		cout << "Item found at position: " << result << "!" << endl;
	
	return 0;
}

int seqSearch(int list[], int listLength, int searchItem)
{
	int loc;
	bool found = false;
	
	loc = 0;
	
	while (loc < listLength && !found)
		if (list[loc] == searchItem)
			found = true;
		else
			loc++;
	
	if (found)
		return loc;
	else
		return -1;
}

void initializeArray(int list[], int listSize)
{
	int index;
	for (index = 0; index < listSize; index++)
		list[index] = 0;
}

void printArray(int list[], int listSize)
{
	int index;
	for (index = 0; index < listSize; index++)
		cout << list[index] << endl;
}




Last edited on
Line 36 must be:
if(result == -1)

Secondly, result must be modified with 1.
Last edited on
Hi,
thanks a lot Condor, I fixed the error in line 36 and now it works! :)
Great!

But what do you mean with "secondly, result must be modified with 1"?

Best
razr
Output sample:
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
Enter number to search in array!
1
before change
0
0
0
0
0
0
0
0
0
0
after change
0
0
0
0
0
0
0
1
0
0
Item found at position: 7!

May be cout to be something like this: Item found at array[7] (although that 1 is the 8th element of the array). That's all.
Yes, I see. I changed the initialization to 0 and array[7] to 1 without changing the comments in the code, my bad.

Also thanks for the hint on the array position, I forgot that arrays start at 0.

So, problem solved, thanks :)
Ok razr, you're welcome! Please mark this thread as Solved. Happy coding!
Topic archived. No new replies allowed.