2D integer array no repeats.

Hi everyone I am new here. I am having trouble with this program. I can get the 2D array to print out the way I want it to but I can't have any repeats for the lottery numbers. I have tried about a hundred different ways to not create repeats. I have also tried creating a separate function to loop through the array and replace duplicates. I just can't get anything to work. If someone could point me in the right direction it would be greatly appreciated.


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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <ctime>
#include <algorithm>

using namespace std;

const int NUMBER_OF_ROWS = 25;
const int NUMBER_OF_COLUMS = 5;

void input(char x[]);
int getRand();
void fillMatrix(int par, int random[NUMBER_OF_ROWS][NUMBER_OF_COLUMS]);
void fillPow(int par, int x[]);



int main()
{
	int random[NUMBER_OF_ROWS][NUMBER_OF_COLUMS] = {{0},{0}};
	int pow[25];
	char date[8];
	int par = 0;
	srand( time( NULL ) + rand());

	input(date);

	cout << " Enter the Number of Participants: ";
	cin >> par;
	cout << endl;

	if (par > 25)
	{
		cout << " Number of participants can not be greater than 25" << endl;
		cout << " Please enter the number again " << endl;
		cin >> par;
	}

	

	int row = 0;
	int col = 0;

	fillMatrix(par, random);
	fillPow(par, pow);
	

	for (row = 0; row < 8; row++)
	{
	cout << date[row];
	}
	cout << endl;
	cout << "Participants: " << par << endl;
	cout << "Numbers" << "        " << "PowerBall" << endl;

	for (row = 0; row < 25; row++)
	{
		for (col = 0; col < 5; col++)
		{
			if (row < par)
				cout << setw(2) << random[row][col] << " ";
			else
				break;
		}
		if (row < par)
				cout << setw(6) << pow[row];
			else
				break;
		cout << endl;
	}
	return 0;
}

void input(char date[])
{
	cout << " Enter Date of Lottery." << endl; 
		cout << " You must include a zero such as 02 for feburary.  (mm/dd/yy): ";

	int index;

	for(index = 0; index <  8; index++)
	{
		cin >> date[index];
	}
}

int getRand()
{
	int num;
		num = (rand() % 49) + 1;
    return num;
}

void fillMatrix(int par, int random[NUMBER_OF_ROWS][NUMBER_OF_COLUMS])
{
	int row; 
	int col = 0;
	int x = col + 1;
	for (row = 0; row < NUMBER_OF_ROWS; row++)
		for (col = 0; col < NUMBER_OF_COLUMS; col++)
		{
			if (row >= par)
				break;
			else
				random[row][col] = getRand();
		}
}

void fillPow(int par, int pow[])
{
	int row;
	for (row = 0; row < 25; row++)
		{
			if (row >= par)
				break;
			else
			pow[row] = getRand();
		}
}


I would maintain an array used for used numbers, and for every time you put a random number in the array, check if it is in the used array, if not then put it in both used and random.
The problem with creating a used array is that there are a limited amount of numbers to use. Sorry I should have made myself clearer. The numbers can repeat in the array just not in the same row.
Anyone have another idea.
Keep a counter.

Your range of items is quite limited ([1,50] it seems?), so you can easily keep an array of 50 values to keep track. Rather than actually keeping a list of all used numbers, use an array with counting variables for each number. For example:

1
2
3
4
5
6
7
8
9
10
int used[50] = {0}, myRandom; // Counters are 0 at beginning.
for (int i = 0; i < rows; ++i) {
   for (int j = 0; j < colums; ++j) { 
      while(1) {
         myRandom = rand();
         if (used[myRandom] < i) { used[myRandom]++; break; }
      }           
   }
   for (int j = 0; j < 50; ++j) used[j] = i;
}

The while loop picks a random until an unused one is found (used[myRandom < i]) and increments the value so it won't pass the check again.
The last line makes sure it can't "save" uses for later rows (i.e. no '3' in the first 5 rows, then 5 times '3' in the 6th row).

Alternatively, you could keep a row of booleans and set them to false each round. It's actually better, but I changed part of my code halfway my post and I was too lazy to adapt things.

[edit]

Make sure your number of columns doesn't get bigger than your range of possible values. Trying to pull 51 unique values out of a range of 50 is difficult. My example code above will lock into an infinite loop.
Last edited on
Topic archived. No new replies allowed.