# Lottery Checker - Logic behind it?

Page 1 of 1

## 6 Replies - 4480 Views - Last Post: 23 January 2010 - 11:08 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=151473&amp;s=58bab80830e2bad252f41032801807e2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 chastepe

Reputation: 0
• Posts: 3
• Joined: 19-January 10

# Lottery Checker - Logic behind it?

Posted 22 January 2010 - 09:55 PM

Hello everyone, I must create a syndicate manager where you insert six numbers from 1 to 49 and then by doing a manual draw, I must check if there are any jackpot winners (6 numbers matching). I am doing this using C# and ASP.NET 3.5. The numbers I insert in the syndicate manager are stored in a database.

I going to check if any of the six numbers into text boxes (that I use for the manual draw) match the numbers stored in the database. This must be done individually for every text box. This means that each text box will be searching in each row (num1, num2, num3, num4, num5, num6). I have one table that has as primary key the name of the person and then I have six columns for each number (name, number1, number2, number3, number4, number5, number6).

At the moment I using an IF statement to check if any numbers match in the database. I do not understand how am I going to tell it to move on to the second text box supposing it finds any number matching. In addition, if that happens it should somehow add those numbers so that it knows whether the numbers that match are six in order to be a winner.

Maybe the IF statement is not the right way to build it, but if I use a loop I do not understand how it is going to work.

I am a beginner and I am not asking you to give me the code, and yes this is an assignment . The only thing I would be happy to know is the logic behind it. Please let me know what the right path is....whether I should use IF statements or LOOPS. Any help is welcome. Thank you very much.

Here is a small piece of code I wrote:

protected void Button1_Click(object sender, EventArgs e)
{
numbers.lottoDataTable numtable = num.GetNumbers();
Label1.Text = "";
foreach (DataRow row in numtable.Rows)
if (TextBox1.Text == System.Convert.ToString(row["number1"]) || (TextBox1.Text == System.Convert.ToString(row["number2"]) || (TextBox1.Text == System.Convert.ToString(row["number3"]) || (TextBox1.Text == System.Convert.ToString(row["number4"]) || (TextBox1.Text == System.Convert.ToString(row["number5"]) || (TextBox1.Text == System.Convert.ToString(row["number6"])))))))
{
Label1.Text = " Mr " + System.Convert.ToString(row["name"])
+ ", you have won";
}

Is This A Good Question/Topic? 0

## Replies To: Lottery Checker - Logic behind it?

### #2 knichols

Reputation: 10
• Posts: 21
• Joined: 09-November 09

## Re: Lottery Checker - Logic behind it?

Posted 22 January 2010 - 10:15 PM

Just my opinion, and I won't write any code since you seem to want to figure this out on your own, but I would use a collection such as "List" to store the numbers from the database.

After you have it in the list you can use the "contains" or "binarysearch" methods to look for the individual numbers.

Your loop would loop through the numbers coming from the textboxes which should probably be stored in some sort of collection as well to iterate over them.

### #3 chastepe

Reputation: 0
• Posts: 3
• Joined: 19-January 10

## Re: Lottery Checker - Logic behind it?

Posted 22 January 2010 - 10:26 PM

knichols, on 22 Jan, 2010 - 09:15 PM, said:

Just my opinion, and I won't write any code since you seem to want to figure this out on your own, but I would use a collection such as "List" to store the numbers from the database.

After you have it in the list you can use the "contains" or "binarysearch" methods to look for the individual numbers.

Your loop would loop through the numbers coming from the textboxes which should probably be stored in some sort of collection as well to iterate over them.

Yes I am using a List View function from Visual Studio 2008. I have also created a Table Adapter so that I can search through the database.

However, should I use loops for the whole process? If you check my code, I am using IF statements...obviously it is incomplete. I have read about arrays but I do not know how I would use them to do the loop inside the database.
I do not mind if you would like to give general examples of code! Any help is appreciated.
Thanks again

### #4 knichols

Reputation: 10
• Posts: 21
• Joined: 09-November 09

## Re: Lottery Checker - Logic behind it?

Posted 22 January 2010 - 10:43 PM

I meant the List class which is a generic collection. Here's a bit of code but it would really only work if you have one distinct set of winning numbers. Looking at your code it seems like you're thinking of having multiple rows of winning number sets.

Also that foreach loop pretty much depends on having some numbers in the textbox list. If it was empty, you would have a fake winner.

List<int> DatabaseNumbers = new List<int>();
DatabaseNumbers.AddRange(new int[] { 1, 2, 3, 4, 5, 6 }); // Write Logic to Insert Database Numbers

List<int> TextBoxNumbers = new List<int>();
TextBoxNumbers.AddRange(new int[] { 1, 2, 3, 4, 5, 6 }); // Write Logic to Insert TextBox Numbers

bool Winner = true;
foreach (int num in TextBoxNumbers)
{
if (!DatabaseNumbers.Contains(num)) Winner = false;
}

richTextBox1.Text = Winner.ToString();

### #5 dawmail333

Reputation: 27
• Posts: 174
• Joined: 02-July 07

## Re: Lottery Checker - Logic behind it?

Posted 23 January 2010 - 03:48 AM

Sorry, a) is order important, and is it possible to have duplicate numbers? Don't forget those.

### #6 baavgai

• Dreaming Coder

Reputation: 6053
• Posts: 13,106
• Joined: 16-October 07

## Re: Lottery Checker - Logic behind it?

Posted 23 January 2010 - 04:42 AM

chastepe, on 22 Jan, 2010 - 10:55 PM, said:

This must be done individually for every text box. This means that each text box will be searching in each row (num1, num2, num3, num4, num5, num6). I have one table that has as primary key the name of the person and then I have six columns for each number (name, number1, number2, number3, number4, number5, number6).

This sounds like a wonky thing to do, but if I had to...

The database should look like:
Table: Draw
DrawId (PK)
When
Who

Table: DrawNumbers
DrawId (PK, FK)
Sequence (PK)
Number

So,
Data: Draw
DrawId  When	   Who
1		   1/1/2010  Larry
2		   1/8/2010  Moe

Data: DrawNumbers
DrawId Sequence Number
1		  1			 23
1		  2			 12
1		  3			 19
1		  4			 6
1		  5			 11
1		  6			 25
2		  1			 16
2		  2			 12
2		  3			 5
2		  4			 9
2		  5			 22
2		  6			 3

Now you can have a simple query applicable to any of your six numbers. You also have more freedom of analysis, like "how many times has X come up" which isn't easy with a separate column for each number.

### #7 chastepe

Reputation: 0
• Posts: 3
• Joined: 19-January 10

## Re: Lottery Checker - Logic behind it?

Posted 23 January 2010 - 11:08 AM

dawmail333, on 23 Jan, 2010 - 02:48 AM, said:

Sorry, a) is order important, and is it possible to have duplicate numbers? Don't forget those.

No order is not important. If you have the numbers in a different order in your syndicate than the ones in the draw you should still win, as long as you have them. No you should not have duplicates.