random number problem

Given this array size 7x4
1
2
3
4
5
6
7
12 5  6 34 
10 45 5 23 
20 89 76 5 
11 34 3 44 
34 2  3 
22 89 
72 


I generate a random number using this function
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
void sample()
{
  // some statements

  srand(time(NULL));
  int r1 = rand() % 7;
  int r2 = rand() % 4;

  // r1, r2, array is past to random()
  random(r1, r2, array);
}

This function checks if the r1 and r2 from sample() is [4][3], [5][2], [5][3], [6][1], [6][2], [6][3]. I use r1 and r2 as index. If it is, it calls the function again (random()) otherwise the value is displayed. 

The problem is it often gives me segmentation fault if r1 and r2 falls under the index above else it displays the number from the list.  
  
void random(int r1, int r2, int a[][4])
{
    switch(r1 && r2)
    {
        case 4 && 3 || 5 && 2 || 5 && 3 ||
             6 && 1 || 6 && 2 || 6 && 3:
                random(r1, r2, a);
                break;
        default:
            cout << a[r1][r2] << " ";
    }
    cout << "\n";
}


Any tips on how to solve this problem?
switch doesn't work like that, and case doesn't work like that.

Let's see how you're creating a.
&& and || is for boolean values. When used on integers it treats 0 as false and everything else as true. That means
1
2
case 4 && 3 || 5 && 2 || 5 && 3 ||
     6 && 1 || 6 && 2 || 6 && 3:
is the same as writing
case true:
Is that what you want?
Last edited on
The array 7x4 below comes from another array 8x8 initialize with random numbers. From that 8X8 array I need to display it like the array below.

1
2
3
4
5
6
7
12 5  6 34 
10 45 5 23 
20 89 76 5 
11 34 3 44 
34 2  3 
22 89 
72 


since my code above compiled with no errors I thought it might work.haha

Here's what I'm trying to do:
Given the 7x4 array above

1. generate two numbers and use them as the index for the array a[r1][r2]
1
2
3
  srand(time(NULL));
  int r1 = rand() % 7;
  int r2 = rand() % 4;


2. if the generated numbers are one of the following:
[4][3], [5][2], [5][3], [6][1], [6][2], [6][3]
I need to generate two numbers again.

3. if the value has been displayed already generate two numbers again.

4. if the value are the same but have different index for example
[1][0] = 1 and [2][0] = 1
then display the value
else generate random numbers again
And how do you actually create a?
a is
1
2
3
4
5
6
7
8
9
10
int sub[7] = {0,0,0,0,1,2,3};

for(int i = 0; i < 7; i++)
{
    for(int j = 0; i < 4 - sub[i]; i++)
    {
       a[i][j] = array[i][j];
    }
}
i tried this so far

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
int random(int a[][4])
{
    int r1, r2;

    srand(time(NULL));
	r1 = rand() % 7;
	r2 = rand() % 4;

    /* if the generated numbers are one of the following:
       [4][3], [5][2], [5][3], [6][1], [6][2], [6][3]
       generate two numbers again */

    if((r1 == 4) && (r2 == 3))
    	randNumber(a);
    else if((r1 == 5) && (r2 == 2) || (r1 == 5) && (r2 == 3))
    	randNumber(a);
    else if((r1 == 6) && (r2 == 1) || (r1 == 6) && (r2 == 2) || (r1 == 6) && (r2 == 3))
    	randNumber(a);
    else 
    {
    	cout << r1 << " " << r2 << " " << "\n";
    	cout << a[r1][r2] << " ";
    }
    cout << "\n";
}


But sometimes it gives a segmentaion fault or displays nothing
Topic archived. No new replies allowed.