I have a slight problem with my code! Every time i run it, it completes as its supoosed to do but the last element of the vectors do not match - element number 6 every time. See below!
5570594
Your Numbers: 2 7 10 17 42 47
Draw numbers: 2 7 10 17 42 43
8888795
Your Numbers: 12 29 37 38 44 58
Draw numbers: 12 29 37 38 44 45
428233
Your Numbers: 15 17 18 33 40 54
Draw numbers: 15 17 18 33 40 41
218508
Your Numbers: 18 24 40 46 48 50
Draw numbers: 18 24 40 46 48 52
As you can see element 6 is always not equal but the do-while loop condition must be satisfied for it to stop looping?
The number above the lines is how many times the do-while loop has ran and 6 random numbers have been drawn.
Please can any of you code gurus help - its annoying as i can't find the erorr, plus I am new to C++.
Thank you in advance.
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>
int main()
{
int numberOfBalls = 59;//number of balls in lottery draw
int line = 6;//numbers in a line
int counter = 1;//variable increment count for vector
int number = 1;//variable to store random numbers from 1 to 59
bool generateNumbers[59];//array to store random lottery numbers
std::vector <int> firstLine (line);//vector to store lottery line
srand(time(NULL)); // randomise seed
//set all elements of generateNumbers to false in array
for ( int i = 1; i <= numberOfBalls; i++)
{
generateNumbers[i] = false;
}
//generate 6 random numbers and store them in that particular
//element of the array, do this 6 time until counter == 6
while(counter <= line )
{
number = 1 + (rand() % 59);//random number between 1 and 59
if ( !generateNumbers[number] )
{
generateNumbers[number] = true;
counter++; //increment for stop condition of loop
}
}
counter = 1;//reset counter to one again so we can use this value to
//store the random number in that element
for ( int i = 1; i <= numberOfBalls; i++ )
{//loop through array to find values then store then in vector
if ( generateNumbers[i] )
{
firstLine[counter] = i;
counter++;
}
}
//use for loop to print lottery line to screen
for ( int i = 1; i <= line; i++ )
{
std::cout << firstLine[i] << " ";
}
std::cout << std::endl;
//now generate a draw and compare both vectors for equality
//if both vectors are equal then end loop and lottery won
//use a variable to count how many draws were made before win
// use the same variables and arrays again but this time
//a different vector to store
std:: vector <int> lotteryDraw (line);//vector to store drawn numbers
int numberOfDraws = 0;
do{
//set all elements of generateNumbers to false in array
for ( int i = 1; i <= numberOfBalls; i++)
{
generateNumbers[i] = false;
}
//reset counter to one again before using
counter = 1;
number = 1;
//generate 6 random numbers and store them in that particular
//element of the array, do this 6 time until counter == 6
while(counter <= line )
{
number = 1 + (rand() % 59);//random number between 1 and 59
if ( !generateNumbers[number] )
{
generateNumbers[number] = true;
counter++; //increment for stop condition of loop
}
}
counter = 1;//reset counter to one again so we can use this value to
//store the random number in that element
for ( int i = 1; i <= numberOfBalls; i++ )
{//loop through array to find values then store then in vector
if ( generateNumbers[i] )
{
lotteryDraw[counter] = i;
counter++;
}
}
//use for loop to print lottery line to screen
//for ( int i = 1; i <= line; i++ )
//{
//std::cout << lotteryDraw[i] << " ";
//}
numberOfDraws++;
std::cout << numberOfDraws << std::endl;
}while ( firstLine != lotteryDraw );
if ( firstLine == lotteryDraw )
{
std::cout << "Your Numbers: ";
for ( int i = 1; i <= line; i++ )
{
std::cout << firstLine[i] << " ";
}
std::cout << "\nDraw numbers: ";
for ( int i = 1; i <= line; i++ )
{
std::cout << lotteryDraw[i] << " ";
}
std::cout << std::endl;
}
return 0;
}
|