Some help with sequential search algorithm

The details of the assignment is within the code. Basically its a simulated radio station holding a number guessing contest. 20 numbers were "randomly selected" and stored in a text file. The sequential search algorithm is to find the number (obviously), but I am stuck at this point... could someone give me a nudge to get me rolling again please?

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
#include<iostream>
#include<fstream>

using namespace std;

int sequenSrch(int&, int&);
int guess();

int main()
{
	cout << "This program simulates a radio station that asks the caller to guess a number.\n";
	cout << "The number is compared against a list of 20 numbers between 1 and 500 inclusive.\n";
	cout << "The contest is held until a number has been matched or a value of - 1 is entered.\n";
	cout << "A message is displayed containing the winning number, the location in the list of numbers,";
	cout << " the number of calls made, and the amount of the prize.";

	int num = 20;
	int size = 0;
	int first, x;
	int* prizeArray = new int[num];
	int location;
	
	location = sequenSrch(); // Not sure what to pass here...?

	ifstream prizeList;
	
	prizeList.open("prizeList.txt");
	while (prizeList >> x)
	{
		if (size == num)
		{
			int* newPrizeArray = new int[2 * num];

			for (first = 0; first < size; first++)
			{
				newPrizeArray[first] = prizeArray[first];
			}
			delete[] prizeArray;
			prizeArray = newPrizeArray;
			num *= 2;
		}
		prizeArray[size] = x;
		size++;
	}
	prizeList.close();

	guess();
	;

	return 0;
}

int sequenSrch(int newPrizeArray[20], int value)
{
	int i = 1, results = 0;
	bool found = false;
	
	while (!found && i < newPrizeArray[20])
	{
		if (newPrizeArray[i] == value)
		{
			found = true;
			results = i;
		}
		cout << guess() << " is not in the list. Call again.\n";
		i++;
	}
	return results;
}

int guess()
{
	int guess;

	cout << "Hello caller. What number between 1 and 500 are you guessing?\n";
	cin >> guess;

	return guess;
}
If the size of the array can only be one value, what is the point of using dynamic memory?

Valid indices for an array are 0 to size-1, where size is the number of elements in the array, so when you access newPrizeArray[20] on line 58, you're accessing a non-existent element. Why, btw, are you comparing the array index i to an element of the array?

Line 65 executes whether a value is find or not.
i revamped the code a bit... this is what I changed it to...
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
	const int x = 20;
	int y;
	int prizeArray[x];
	
	ifstream prizeList;
	
	prizeList.open("prizeList.txt");
	for (y = 0; y < x && prizeList >> prizeArray[y]; y++);
	prizeList.close();

	guess();
	sequenSrch(); //"+3 overloads"... not sure what to pass (still)

	return 0;
}

int sequenSrch(const int prizeArray[], int arrayLength, int searchedItem)
{
	int location;
	bool found = false;
	location = 0;
	
	while (location < arrayLength && !found)
	if (prizeArray[location] == searchedItem)
		found = true;
	else
		cout << guess() << " is not in the list. Call Again.\n";
		location++;

	if (found)
		return location;
	else
		return -1;
}
The is what I have now....

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
#include<iostream>
#include<fstream>

using namespace std;

int sequenSrch(const int prizeArray[], int arrayLength, int searchedItem);
int guess();

int main()
{
	cout << "This program simulates a radio station that asks the caller to guess a number.\n";
	cout << "The number is compared against a list of 20 numbers between 1 and 500 inclusive.\n";
	cout << "The contest is held until a number has been matched or a value of - 1 is entered.\n";
	cout << "A message is displayed containing the winning number, the location in the list of numbers,";
	cout << " the number of calls made, and the amount of the prize.";

	const int x = 20;
	int fillArray;
	int prizeArray[x];
	int callerNum = 1;

	ifstream prizeList;
	
	prizeList.open("prizeList.txt");
	for (fillArray = 0; fillArray < x && prizeList >> prizeArray[fillArray]; fillArray++);
	prizeList.close();

	int guessNum = guess();
	int loc = sequenSrch(prizeArray, fillArray, guessNum);
	
	
	while (guessNum != loc)
	{
		guess();
		callerNum++;
	}
		cout << "Congratulations. Your number " << guessNum << " was found at location " << loc;
		cout << " of the list.\n";
		cout << "Counting you, there were " << callerNum << " callers.You win $10, 000!\n";

	return 0;
}

int sequenSrch(const int prizeArray[], int arrayLength, int searchedItem)
{
	int location;
	bool found = false;
	location = 0;
	
	while (location < arrayLength && !found)
	if (prizeArray[location] == searchedItem)
		found = true;
	else
		location++;

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

int guess()
{
	int guess;

	cout << "\nHello caller. What number between 1 and 500 are you guessing?\n";
	cin >> guess;

	return guess;
}


I keep getting an infinite loop that keeps asking for the guess or an infinite scrolls the console until I exit. My head is about to explode from looking at this simple program that I just cannot seem to figure out.
Look at the loop on line 32 to 36. You loop on the value of guessNum. Where does that value change anywhere in the loop? What do you do with the value returned by guess?
Well... the returned value of guess is supposed to stay the same to check the list of numbers... then ask again for another number if it does not match with the list... compare to the list... ask again... until either -1 is entered or guess returns a value = to a number within the list, but I want to increment the value of callerNum each time to include that in the output to the "winner"

My problem is how to compare the number guessed to the list from the file... well the array that was created from the file. Then loop if the number is not matched to ask for another number and increment callerNum
My problem is how to compare the number guessed to the list

Then it would behoove you to stop throwing the guessed value away. Do something with it.
sequenSrch() returns the location of the element or -1, so to tell if the item was found, you just need to compare the result of sequenSrch() with -1. Then inside the loop you need to do the whole "get the guess, do the search" part again. So it would look like this:
1
2
3
4
5
    while (loc < 0) {
        guessNum = guess();
        loc = sequenSrch(prizeArray, fillArray, guessNum);
        callerNum++;
    }


If you do something once and then do it again inside a while() loop, it's a good indication that you should consider a do/while loop instead. In this case, not only does it make the code easier, but it lets you handle the user quitting too:
1
2
3
4
5
6
7
8
    int guessNum;
    int loc;
    int callerNum = 0;
    do {
        guessNum = guess();
        loc = sequenSrch(prizeArray, fillArray, guessNum);
        ++callerNum;
    } while (loc < 0 && guessNum != -1);


The while() condition is "while the number hasn't been found (loc<0) and they didn't quit (guessNum != -1)"
After the loop, you need to check whether it exited because the user found the answer or because they quit. Print a different message depending on which it was.
A few other tips:

"abc" "def" is the same as "abcdef" so you can print out the opening message like this:
1
2
3
4
5
6
    cout <<
        "This program simulates a radio station that asks the caller to guess a number.\n"
        "The number is compared against a list of 20 numbers between 1 and 500 inclusive.\n"
        "The contest is held until a number has been matched or a value of - 1 is entered.\n"
        "A message is displayed containing the winning number, the location in the list of numbers,"
        "the number of calls made, and the amount of the prize.\n";

Try to choose variable names that are meaningful. Use "size" or some variation for the size of a container (because that's what the STL does). Use "max" in the name of a variable that represents the maximum:
1
2
3
4
5
6
7
8
9
10
11
12
    const int maxSize = 20;
    int size;
    int prizeArray[maxSize];

    ifstream prizeList;

    prizeList.open("prizeList.txt");
    for (size = 0;
         size < x && prizeList >> prizeArray[size];
         size++)
        ;
    prizeList.close();

Use for loops when counting, and don't be afraid to return from multiple places in a function if it makes things easier:
1
2
3
4
5
6
7
8
9
10
int
sequenSrch(const int prizeArray[], int arrayLength, int searchedItem)
{
    for (int i = 0; i < arrayLength; ++i) {
        if (prizeArray[i] == searchedItem) {
            return i;
        }
    }
    return -1;
}


Get in the habit of commenting your code. Each function should say what it does, what the parameters are, and what the return value is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Search for "searched Item" in "prizeArray" whose length is "arrayLength". Return
// the index if it's found, otherwise -1
int
sequenSrch(const int prizeArray[], int arrayLength, int searchedItem)
{
...
}

// Prompt for a guess and return it.
int
guess()
{
...
}

Alright... great input! I will take a look and finish this up today.
I have figured this whole thing out by changing the while loop to display if the guessed number is incorrect. Then I used if the number guessed is in the list.... display winner message, else the number entered was -1 so terminate. I really appreciate all the input. You guys have helped me out a bunch!
Topic archived. No new replies allowed.