Searching an Array

For my assignment I had to finish a program using 3 pre-set functions and the program had to tell you if the number you entered was in the array. We had a txt. file of 500 ints provided also.
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

#include <iostream>
#include <fstream>
using namespace std;

const int SIZE = 500;

void readFile (int a[], int size);
int getUserChoice ();
bool searchArray (int a[], int size, int value);

int main () {

	int integerArray[SIZE];
	int choice = -1;

	readFile (integerArray, SIZE);
	getUserChoice ();

    cout << choice;
	if (searchArray(integerArray, SIZE, choice))
		cout << choice << " is in the array." << endl;
	else
		cout << choice << "is not in the array." << endl;

return 0;
} 

//------------------------------------------------------------------------------------------
void readFile (int a[], int size){

	ifstream inFile;	
	inFile.open("ints.txt");

	if (inFile.fail()) {  
	  cout << "Unable to locate file." << endl;
	}
	else {
		for (int i=0; i<500; i++) {
	  	inFile >> a[SIZE];
	 
		}	
	inFile.close();
	}
}
//------------------------------------------------------------------------------------------
bool searchArray (int a[], int size, int value){

	int index = 0;
	bool found = false;
	
	while ((!found) && (index < SIZE))
		if (choice == a[index])
			found = true;
		else 
			index ++;	

	if (found)
		return index;
	else
		return -1;

}
//------------------------------------------------------------------------------------------
int getUserChoice (){
		
	int choice = -1;

	cout << "Enter a number to search for: " << endl;
	cin >> choice;

	return choice;
}


My problem is in the searchArray I believe, I am very new to programming but when I pass it choice, should it bring that value into the function?
Here, you are treating the search function as though it returned a true or false (boolean) value.
if (searchArray(integerArray, SIZE, choice))

Indeed, the function is defined with a bool return type. But inside the function what value does it actually attempt to return?
1
2
3
4
	if (found)
		return index;
	else
		return -1;

Answer, it tries to return the array index (or subscript) where the element was found. Or if the element was not found, then it returns -1.

Thus the value indicating "success" is an integer in the range 0 to SIZE-1. And "fail" is the integer -1. When converted to a bool, that means most of the time, the value returned will be true. The only case where it will return false is when the item is successfully found as the very first element of the array.

To summarise: it's broken.

How to fix it? Change the function so it really does return an integer.
int searchArray (int a[], int size, int value)
(change both lines 10 and 47).

In addition, change the way the result is tested.
1
2
3
4
	if (searchArray(integerArray, SIZE, choice) >= 0)
		cout << choice << " is in the array." << endl;
	else
		cout << choice << "is not in the array." << endl;


Or do it like this:
1
2
3
4
5
    int foundAt = searchArray(integerArray, SIZE, choice);
    if (foundAt >= 0)
		cout << choice << " is in the array at position "<< foundAt << endl;
	else
		cout << choice << "is not in the array." << endl;

I certainly see what you are saying with the way that function operates. No idea what I was thinking there. It is giving me a scope area now, saying that choice was not declared within the scope of the searchArray function?
Ah I missed that.
Line 53
if (choice == a[index])
should be
if (value == a[index])

The name of the parameter as defined in the function header is the one you need to use. It doesn't matter what name was used when the function was called - for example it could be called from main() like this, where a number is passed as the parameter value:
searchArray(integerArray, SIZE, 123);
Or equally, the function might be called several times, perhaps with different arrays, and a different variables to search for each time.
Last edited on
Thank you, I'm dropping the ball here with awareness. I with the math and developing psuedo-code it's just getting it in correct syntax and the logic working.

One last question, everything is compiling correctly, yet it always returns that the choice/value is always in the array. Is my logic inside the function correct?
Last edited on
It works ok for me. (terminology note: the actual code is inside the function definition, not the prototype).
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
#include <iostream>
#include <iomanip>

using namespace std;

int searchArray (int a[], int size, int value);

int main ()
{
    int array [] = { 1, 3, 5, 7, 11, 4, 2 };
    
    const int size = sizeof(array)/sizeof(array[0]);
    
    cout << "size = " << size << endl;
    
    for (int i=0; i<15; i++)
    {
        cout << setw(2) << i << " " << setw(2) << searchArray (array, size, i) << endl;
    }
       
    return 0;
}

int searchArray (int a[], int size, int value)
{
	int index = 0;
	bool found = false;
	
	while ((!found) && (index < size))
		if (value == a[index])
			found = true;
		else 
			index ++;	

	if (found)
		return index;
	else
		return -1;
}

Output:

size = 7
 0 -1
 1  0
 2  6
 3  1
 4  5
 5  2
 6 -1
 7  3
 8 -1
 9 -1
10 -1
11  4
12 -1
13 -1
14 -1
Much appreciated! May I ask any good references for learning c++?
well, I'm the wrong person to ask about that. I do think a good book (or several) is the best way to learn properly, but I'm not sure what to recommend.

It may be worthwhile looking at both the tutorial and reference sections on this site - and there are other online resources too.
http://www.cplusplus.com/doc/

Topic archived. No new replies allowed.