Changing Duplicate Random Numbers

We were assigned to write a program using a Matrix, that displays a table of random numbers from 1-48. In each row (horizontal) there could be NO matching numbers. We are not to use a lot of if statements. Could anyone share ideas of how to go about doing this?

My code:

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

/* Project Tickets
   by Jacob Wright 2-26-13 */

#include <iostream.h>
#include <lvp\random.h>
#include <lvp\matrix.h>

//___________________________________

void changedisp (matrix<int> change); //Changes numbers of same value if they're in the same row and displays

//___________________________________

int main()
{
	cout << "\n\n\n\n";
	randomize();
	matrix<int> rand(25, 6);

	for (int c=0; c<=5; c++)
	{
		for (int r=0; r<=24; r++)
			rand[r][c]=random(48)+1;
	}

	changedisp(rand);
	cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
	return(0);
}

//_________________________________

void changedisp (matrix<int> change)  //Changes numbers of same value if they're in the same row and displays
{
	for (int C=0; C<=24; C++) //Start of my search for duplicate numbers
	{
		for (int Y=0; Y<=5; Y++) //nested for statement to allow me to search each row for duplicates
		{







	cout.width(14);
	cout << " [1]";
	for (int x=2; x<=6; x++)
	{
		cout.width(8);
		cout << " [" << x << "]";
	}
	cout << endl << endl;
	cout << " [1]";
	for (int ze=0; ze<=5; ze++)
	{
		cout.width(10);
		cout << change[0][ze];
	}
	cout << endl;
	for (int X=2; X<=25; X++)
	{
		if (X<=9)
			cout << " ";
		cout << "[" << X << "]";
		for (int zes=0; zes<=5; zes++)
		{
			cout.width(10);
			cout << change[X-1][zes];
		}
		cout << endl;
	}
}
Why not check for duplicates when you are adding them to the array.
Are you familiar with set's http://www.cplusplus.com/reference/set/set/

Rewrite the inner loop of the following

1
2
3
4
5
for (int c=0; c<=5; c++)
{
	for (int r=0; r<=24; r++)
		rand[r][c]=random(48)+1;
}

as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
set<int> s;

for (int c=0; c<=0; c++)
{
	// clear set at start of each row
	s.clear();
		
	int r=0;
	while( r <= 24 )
	{
		int rVal = random(48)+1;

		// Do we already have this value
		if( s.find( rVal ) != s.end() )
			continue;
			
		// If we get here a new value
		s.insert(rVal);
		rand[r][c] = rVal;
		r++;
	}

We were assigned to write a program using a Matrix, that displays a table of random numbers from 1-48.


A fairly common way is to generate a list of values covering the range needed, and shuffle the list.
Topic archived. No new replies allowed.