[try Beta version]
Not logged in

 
display unique numbers

Jul 9, 2012 at 11:39am
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
void randNumber (int n1, int n2, int a[][4])
{
  if((n1 == 4) && (n2 == 3))
    {
      b = rand() % 7;
      c = rand() % 4;

      randNumber(b, c, a);
    }
  /*else if (...other numbers that are not allowed...)
    {
      b = rand() % 7;
      c = rand() % 4;

      randNumber(b, c, a);
    }*/
    else if (a[n1][n2] == a[n1][n2]) // i think the problem is here
    { 
      b = rand() % 7;
      c = rand() % 4;

      randNumber(b, c, a);
    }
    else 
    {
    	cout << n1 << " " << n2 << " " << "\n";
    	cout << a[n1][n2] << " ";
    }
    cout << "\n";

}

int random(int a[][4])
{
    int r1[4];
    int r2[4];
    srand(time(NULL));

    for( int i = 0; i < 4; i++)
    {
	r1[i] = rand() % 7;
	r2[i] = rand() % 4;

        if((r1[i] == 4) && (r2[i] == 3))
        {
          b = rand() % 7;
          c = rand() % 4;
          randNumber(b, c, a);
        }
        /*else if (...other numbers that are not allowed...)
        {
          b = rand() % 7;
          c = rand() % 4;
          randNumber(b, c, a);
        }*/
        else if (a[r1[i]][r2[i]] == a[r1[i]][r2[i]]) // i think the problem is here
        { 
          b = rand() % 7;
          c = rand() % 4;
          randNumber(b, c, a);
        }
        else 
        {
    	  cout << r1 << " " << r2 << " " << "\n";
    	  cout << a[r1[i]][r2[i]] << " ";
        }
        cout << "\n";
    }
}


The code above generates two numbers and stores them into separate arrays (r1 and r1).

If r1[i] and r2[i] are invalid it generates two random numbers again.
if the values of a[r1[i]][r2[i]] == a[r1[i]][r2[i]] are the same it generates two random numbers again and is pass to another functions randNumber().

I basically have to display four unique random numbers. But it gives an error segmentation fault.

Any tips and hints??
Jul 9, 2012 at 12:21pm
r1[i] can contain values up to 6 so a[r1[i]][r2[i]] can be out of bounds. You also have the same thing on both sides of == so the value should always be true.
Last edited on Jul 9, 2012 at 12:21pm
Jul 10, 2012 at 1:30am
Array a is 7x4 and I generate two random numbers r1 (1-7) and r2 (1-4) and use them as index for array a. So it will only access the element returned by r1 and r2 from array a. How can it be out of bounds?
Jul 10, 2012 at 8:56am
Sorry I confused the r1 and r2 arrays with that other array.

I don't see what is wrong then. Maybe the problem is somewhere else. Could you post a complete example that compiles and shows the problem you have?
Jul 12, 2012 at 1:05am
Here is a sample run

1st Run
1
2
3
4
5
6
7
8
9
10
11
12
13
4 5 6 7 
11 12 13 14 
18 11 4 21 
25 26 27 28 
32 15 26 
172 255 
45 

Four Random Numbers from the array above
5 1 255
1 2 13
0 3 7
3 1 26


2nd run
1
2
3
4
5
6
7
8
9
10
11
12
13
4 5 6 7 
11 12 13 14 
18 11 4 21 
25 26 27 28 
32 15 26 
172 255 
45 

Four Random Numbers from the array above
2 2 4
3 1 26
0 3 7
3 0 25


3rd Run
1
2
3
4
5
6
7
8
9
10
4 5 6 7 
11 12 13 14 
18 11 4 21 
25 26 27 28 
32 15 26 
172 255 
45 

3 1 26
Segmentation fault (core dumped)


The four random numbers are from array a the first column is the row index and the second column is the col index and the third is it's value.

I basically want the four random numbers to be unique without repetition. If the number has been displayed already it should generate two random numbers again for the index of array a.
Topic archived. No new replies allowed.