vectors and arrays problem

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;
		
	}
		
Note that array and vector indices start at zero. If the vector has a size of 6 it means you should use index 0-5 to access the elements.
Last edited on
First thing wrong I see; generateNumbers[0] (the first element) isn't being used, and you are using generateNumbers[59] which doesn't exist.

Arrays start counting at zero.
Cheers! Thanks very much. I thought tha I could use 1 to 59 for my numbers but clearly i was wrong.
I have altered my code to suit and will give it a try.
What I nee to realise then is that if number 1 is drawn randonly it will be stored inelemnt 0 and therefore number 59 stored in element 58 etc.

Is there no wy i can shift the whole array up by 1 thus making element zero 'redundant' in this application? Will I have to make my arays 60 insteadof 59 for this - or is it simplt too much hassle....

Thanks again
No. It's just something you have to get used to. All you need to change when looping is to start at 0 and use < instead of <= in the condition.

 
for (int i = 0; i < numberOfBalls; i++)
If bool generateNumbers[60]; The indexes 1 to 59 are valid.
Thanks very much for your help! I have taken heed of your advice and made the alterations and it runs fine now - cheers!
I am new to C++ and haven't had any formal training/tuition and I am trying to 'teach myself' lol

I used a boolean array to save having to sort through the random numbers to put them in order, just set that particluar element to true then loop through untill the 6 true elements are then put into the vector for a comparison.

Is this a sound approach or is it better to sort the arrays and not use vectors?

Cheers!!
Topic archived. No new replies allowed.