Display different random numbers' result in multiple 2d arrays

I'm trying to display different approach of random numbers in multiple 2D array, but the result that i got is the same. All my 2D arrays had the same results.

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

using namespace std;

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

	srand (time(NULL));


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 (int k = 0; k < 3; k++)
{
    cout << "Player " << k+1 << endl;
	for (i = 0; i < array_size; i++)
	{
		for (j = 0; j < array_size; j++)
		{
			if (value[i][j] < 10)
				cout << " ";
			if (value[i][j] < 100) 
				cout << " ";
			cout << value[i][j] << " "; // Print numbers
		}
		cout << endl;
	}
	cout << endl << endl;
}
	return 0;
}


Can't figured it out the solution yet. Help?
Last edited on
You only have one 2D array that you print three times.
@Peter87
How to create Mutliple 2D arrays? Any example?
Last edited on
You could use a struct and create an object for each player, or you could just add an extra dimension to your arrays.

 
int value[player_count][array_size][array_size];

Last edited on
@Peter87 can you give an example how to make it using a struct?
@hwan97

Going with what you wanted in a different thread,
http://www.cplusplus.com/forum/general/191016/
a bingo board, here is a way to create up to 10 different boards.

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

using namespace std;

struct Board
{
	string Name;
	const int array_size = 5; // 19 is maximum to keep all numbers in straight columns
	int middle = array_size / 2; // Remove this if not making Bingo board
        int max_num = array_size*array_size;
	int numbers[25]; // size is max_num - Keep track of all numbers created
	int value[5][5];
};

int main()
{
	bool ok;
	int n, z, i, j, x, y;
	int Playing;
	srand((unsigned)time(0));
	Board Player[10]; // Maximum playing
	cout << "How many players ? [1 to 10]" << endl;
	do
	{
		cin >> Playing;
		if (Playing < 1)
			cout << "Seems no one is playing. Let's try this again." << endl;
		if (Playing > 10)
			cout << "We only have 10 openings." << endl;
	} while (Playing < 1 || Playing > 10);
	
	
	for (int x = 0; x < Playing; x++)
	{
		cout << "Enter player #" << x + 1 << "'s first name.." << endl;
		cin >> Player[x].Name;
	}

	for (y = 0; y < Playing; y++)
	{
		z = 0;
		cout << "Working on player " << Player[y].Name << endl;
		for (i = 0; i < Player[y].array_size; i++)
		{
			for (j = 0; j < Player[y].array_size; j++)
			{
				do
				{
					ok = true;
					n = (1 + rand() % 15) + (j * 15); // For a 5x5 Bingo card
                                      //n = 1+rand()% Player[y].max_num;//else 1 to maximum number
					for (x = 0; x < z; x++)
					{
						if (n == Player[y].numbers[x])
						{
							ok = false;
						}
					}
				} while (!ok);
				Player[y].numbers[z] = n;
				Player[y].value[i][j] = n;
				z++;
			}
		}
     }

	for (y = 0; y < Playing; y++)
	{
		cout << "Player " << Player[y].Name << "'s board. " << endl;
		for (i = 0; i < Player[y].array_size; i++)
		{
			for (j = 0; j < Player[y].array_size; j++)
			{
				if (i == Player[y].middle && j == Player[y].middle) // Remove this if not making Bingo board
				{
					cout << " Fr "; // Don't need free space if NOT bingo
				}
				else
				{
					if (Player[y].value[i][j] < 10) // Just to make numbers into even columns
						cout << " ";
					if (Player[y].value[i][j] < 100) // Just to make numbers into even columns
						cout << " ";
					cout << Player[y].value[i][j] << " "; // Print numbers
				}
			}
			cout << endl;
		}
		cout << endl << endl;
	}
	return 0;
}
Last edited on
@whitenite1
Thankyou so much for you help. But do you know how to determine the winner as well? Because i just know how to determine the winner if it has the fixed size board. How if the user want to enter the size by itself (let say between 3 to 5)? Of course i cannot use the usual method to determine the winner.
@hwan97

I have no idea how to determine a winner, as I have NO idea on how the game is played, nor what the numbers in the arrays, mean. If you could supply the rules, I should be able to figure out how a player may win. Letting the user determine the size is relatively easy.
Change the struct to:
1
2
3
4
5
6
7
8
9
struct Board
{
	string Name;
	const int array_size = 5; // Change to largest amount of array size
	//int middle = array_size / 2; // Removed. This isn't a Bingo board
        int max_num = array_size*array_size;
	int numbers[25]; // size is value of array_size*array_size
	int value[5][5];// Change the tw0 5's to the value of array_size
};


There's a bit of code changing later also, but we'll get to that after I find out the rules.. ;)
Topic archived. No new replies allowed.