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";
}
|