No duplicate numbers in 2d array

Hello everyone. So i'm trying to do a bingo game. I use a function to randomize my numbers inside my array and the number is between the range of the array size (zero is not included) and i have to make sure that there's no duplicate numbers inside my array because there's no duplicate number inside a bingo box. So far i just know how to check the 1d array

#include <ctime>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
int array_size = 5;
int max_num = array_size;

int value [array_size];

srand (time(NULL));

for (int i = 0; i < max_num; i++)
{
bool check;
int n;
do
{
n = rand()% max_num + 1;
check = true;

for (int x = 0; x < i;x++)
if (n == value[x])
{
check = false;
break;
}
} while (!check);

value [i] = n;
}

for (int i = 0; i < max_num; i++)
{
cout << value [i] << " ";

}

return 0;

}

Can someone help me how to check the randomize number for 2d array?
Last edited on
Lets say that the max_num is 1'000'000. You have already managed to get 999'999 unique numbers. How many times do you expect to have to guess before you will get that last missing value right?

That does not relate to 2d vs 1d, but it does impact the solution.

You know exactly what values you need, but not where they are. Therefore, take the desired values and shuffle them into random positions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <numeric>      // std::iota
#include <iostream>
#include <algorithm>   // std::random_shuffle

int main () {
  constexpr size_t max_num = 9;
  int numbers[ max_num ];

  std::iota( numbers, numbers + max_num, 1 );
  std::cout << "numbers:";
  for ( int& i : numbers ) std::cout << ' ' << i;
  std::cout << '\n';

  std::random_shuffle( numbers, numbers + max_num );
  std::cout << "numbers:";
  for ( int& i : numbers ) std::cout << ' ' << i;
  std::cout << '\n';

  return 0;
}


For 2d, you can treat the memory block that holds the contents of 2d array like it were a 1d array ...
My lecturer haven't teach that before so i don't think i can use that code in my bingo program ._.
Of course you should not use code written by others. Study the ideas and figure out a way to rewrite them in "allowed syntax".

The std::random_shuffle is documented: http://www.cplusplus.com/reference/algorithm/shuffle/
Within there you see "The behavior of this function template is equivalent to".

What is in it? A loop.
What does the loop use? std::uniform_int_distribution and std::swap
http://www.cplusplus.com/reference/random/uniform_int_distribution/
http://www.cplusplus.com/reference/algorithm/swap/


2d in 1d:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>

int main () {
  constexpr size_t max_num = 4;
  int numbers[ max_num ][ max_num ] {};

  int * foo = numbers[0];
  for ( size_t elem=0; elem < max_num*max_num; ++elem ) foo[elem] = 1+elem;

  std::cout << "numbers:\n";
  for ( auto row : numbers ) {
    for ( size_t col=0; col < max_num; ++col ) std::cout << std::setw(3) << row[col];
    std::cout << '\n';
  }

  return 0;
}
@hwan97

Here's how you can make each number unique in a 2D array, but you'll still have to use a different approach for your Bingo board. Column 1 can only have numbers 1 - 15, column 2 has 16 -30, column 3, 31 - 45, etc.

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
#include <ctime>
#include <iostream>

using namespace std;

int main()
{
	const int array_size = 12; // Change to whatever size array needed
	int max_num = array_size*array_size;
	int numbers[array_size*array_size] = { 0 }; // Keep track of all numbers created
	int value[array_size][array_size] = { { 0 }, { 0 } };
	bool ok;
	int n, z=0, i, j, x;

	srand((unsigned)time(0));

	for (i = 0; i < array_size; i++)
	{
		for (j = 0; j < array_size; j++)
		{
			do
			{
				ok = true; 
				n = 1 + rand() % max_num;
				
				for ( x = 0; x < z; x++)
				{
					if (n == numbers[x])
					{
						ok = false;
					}
				}
			} while (!ok);
			numbers[z] = n;
			value[i][j] = n;
			z++;
		}
	}
	
	for (i = 0; i < array_size; i++)
	{
		for (j = 0; j < array_size; j++)
		{
			if (value[i][j] < 10) // Just to make numbers into even columns
				cout << " ";
			if (value[i][j] < 100) // Just to make numbers into even columns
				cout << " ";
			cout << value[i][j] << " "; // Print numbers
		}
		cout << endl;
	}
	cout << endl << endl;
	return 0;
}
Topic archived. No new replies allowed.