M
Miktor
I'm trying to write a lottery number generator for the uk national
lottery.
Any clues where I'm going wrong?
#include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{ // opening bracket of main() section
// declare the other variables we are going to use
int n=0;
int random_number=0;
int lottery_lines [10][7];
int number_bag [50];
int line_no=1;
int pos_no=1;
int selection;
int count=49;
int x=1;
// initialise contents of number_bag
// to contain numbers 1-49 in positions 1-49
for (n=1;n<=49;n++)
{
number_bag [n] = n;
cout << "Number_bag ";
cout << n;
cout << " = ";
cout << number_bag [n];
cout << "\n";
}
// BEGINNING OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++
+++++++++
for (line_no=1; (line_no != 9) and (pos_no != 2); line_no++)
// break out of the line loop if we are at line 9
{ // opening bracket for line loop
// inner program loop
// generates numbers in positions 1 to 6 for each line
// BEGINNING OF POSITION LOOP
==================================================
for (pos_no=1;pos_no<=6;pos_no++)
{ // opening bracket for position loop
// assign selection a random number between 1 and (49-count)
int selection = 1 + (rand () % count);
lottery_lines [line_no] [pos_no] = number_bag [selection];
cout << "Line ";
cout << line_no;
cout << " , number ";
cout << pos_no;
cout << " = ";
cout << lottery_lines [line_no] [pos_no];
cout << "\n";
count--;
// remove used numbers from number_bag array
// by moving all the numbers from number_bag [selection] down
one
for (x=1; x<=(count-selection);x++)
{
cout << "\n";
cout << "Moving number_bag ";
cout << (selection+x);
cout << " down one position";
cout << "\n";
number_bag [selection+x] = number_bag [selection+x+1];
}
} // closing bracket for position loop
// END OF POSITION LOOP
========================================================
} // END OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++
// at this stage, 49 numbers have been drawn,
// so we need to place another 5 random numbers from 1-49
// in number_bag [1] to number_bag [5]
for (n=1;n<=5;n++)
{
number_bag [n] = 1 + (rand () % 49);
// force repeat selection if number_bag [n] = number_bag [1]
if (number_bag [n] = number_bag [1])
{
n--;
}
}
// test that none of the numbers
// in number_bag [2] to [6] are repeats of each other
// CODE HERE
// assign the values of lottery_lines [9][1] to [9][6]
// to number_bag [1] to [6]
for (n=1;n<=6;n++)
{
lottery_lines [9][n] = number_bag [n];
}
// output the nine lines of lottery numbers
for (line_no=1;line_no<=9;line_no++)
{
cout << "\n";
cout << "LINE NO.";
cout << line_no;
cout << "\t";
for (pos_no=1;pos_no<=6;pos_no++)
{
cout << lottery_lines [line_no][pos_no];
cout << "\t";
}
}
system("PAUSE");
return 0;
} // closing bracket of main() section
lottery.
Any clues where I'm going wrong?
#include <cstdlib>
#include <iostream>
using namespace std;
int main ()
{ // opening bracket of main() section
// declare the other variables we are going to use
int n=0;
int random_number=0;
int lottery_lines [10][7];
int number_bag [50];
int line_no=1;
int pos_no=1;
int selection;
int count=49;
int x=1;
// initialise contents of number_bag
// to contain numbers 1-49 in positions 1-49
for (n=1;n<=49;n++)
{
number_bag [n] = n;
cout << "Number_bag ";
cout << n;
cout << " = ";
cout << number_bag [n];
cout << "\n";
}
// BEGINNING OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++
+++++++++
for (line_no=1; (line_no != 9) and (pos_no != 2); line_no++)
// break out of the line loop if we are at line 9
{ // opening bracket for line loop
// inner program loop
// generates numbers in positions 1 to 6 for each line
// BEGINNING OF POSITION LOOP
==================================================
for (pos_no=1;pos_no<=6;pos_no++)
{ // opening bracket for position loop
// assign selection a random number between 1 and (49-count)
int selection = 1 + (rand () % count);
lottery_lines [line_no] [pos_no] = number_bag [selection];
cout << "Line ";
cout << line_no;
cout << " , number ";
cout << pos_no;
cout << " = ";
cout << lottery_lines [line_no] [pos_no];
cout << "\n";
count--;
// remove used numbers from number_bag array
// by moving all the numbers from number_bag [selection] down
one
for (x=1; x<=(count-selection);x++)
{
cout << "\n";
cout << "Moving number_bag ";
cout << (selection+x);
cout << " down one position";
cout << "\n";
number_bag [selection+x] = number_bag [selection+x+1];
}
} // closing bracket for position loop
// END OF POSITION LOOP
========================================================
} // END OF LINE LOOP +++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++
// at this stage, 49 numbers have been drawn,
// so we need to place another 5 random numbers from 1-49
// in number_bag [1] to number_bag [5]
for (n=1;n<=5;n++)
{
number_bag [n] = 1 + (rand () % 49);
// force repeat selection if number_bag [n] = number_bag [1]
if (number_bag [n] = number_bag [1])
{
n--;
}
}
// test that none of the numbers
// in number_bag [2] to [6] are repeats of each other
// CODE HERE
// assign the values of lottery_lines [9][1] to [9][6]
// to number_bag [1] to [6]
for (n=1;n<=6;n++)
{
lottery_lines [9][n] = number_bag [n];
}
// output the nine lines of lottery numbers
for (line_no=1;line_no<=9;line_no++)
{
cout << "\n";
cout << "LINE NO.";
cout << line_no;
cout << "\t";
for (pos_no=1;pos_no<=6;pos_no++)
{
cout << lottery_lines [line_no][pos_no];
cout << "\t";
}
}
system("PAUSE");
return 0;
} // closing bracket of main() section