arrays and bools

so i'm supposed to create a program where a user enters a lottery number and it searches the array of lottery numbers to see if they're a match.
i want to know how to display the results of the search in the way my teacher wants, which is that lottery number validation is to be performed in a function that takes two parameters: the winning lotto numbers array and input lottery number. she wants the function to return a boolean value for true if its a winner, and false if it isn't. help? here's what i've got to work with so far.


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
//Include Files
#include <iostream>
#include <iomanip>
using namespace std;

//Function Prototypes
int getlottoNum();
int binarySearch(int [], int, int);
bool isWinner(int);
void displayNum();

//Main Line

int main()
{
	const int SIZE = 10;
	int win[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};

	int lottoNum;
	int results;
	int index;

	do
	{
		//Ask user for lottery number
		lottoNum = getlottoNum();

		//Search for lottery number
		index = int binarySearch(int list[], int numElems, int value);
		
		//Display search results

	}

	//Validate - BOOL
	isWinner(win[], lottoNum);


	//Sort Numbers & print

	return 0;

}

//Function Declarations

int getlottoNum()
{
	int lottoNum;
	cout << "What is your choice for this weeks winning lottery number?" << endl;
	cin >> lottoNum;
	return lottoNum;
}

int binarySearch(int array[], int numElems, int value)
{
	int first = 0,
		last = numElems - 1,
		middle,
		position = -1;
	bool found = false;

	while (!found && first <= last)
	{
		middle = (first + last) / 2;
		if (array[middle] == value)
		{
			found = true;
			position = middle;
		}
		else if (array[middle] > value)
			last = middle - 1;
		else
			first = middle + 1;
	}
	return position;

bool isWinner(int win[], )
{
	bool status;

	if (lottoNum)
		status = true; //winner
	else
		status = false;
	return status;
}

//Program Output 
You're on the right track, but you're making things more complicated then they need to be. Personally, I don't think you need both binarySearch and isWinner- combine them into one function, and call it validateNumber. Pass the array and the player's input, check the number against the array, and return whether or not it's true or false. Then, display a message based on the returned boolean value.

Also, to clarify, what do you mean by
i want to know how to display the results of the search in the way my teacher wants, which is that lottery number validation is to be performed in a function that takes two parameters
The first part makes it sound like you want to do something like cout<<"Congrats! "<<lottoNum<< " is a winner!" (ie, a message to the screen). However, the second part makes it sound like you're concerned about the composition of your program.
Topic archived. No new replies allowed.