Hello, I've searched around and found similar threads to my question, but haven't really found something that has helped this concept click. I'm trying to create 4x4 array that holds 8 pairs of numbers in randomly generated spaces. For example, the array:
1 1 2 2
3 3 4 4
5 5 6 6
7 7 8 8
Would look something like this:
3 1 2 4
8 7 7 5
1 4 3 6
6 2 5 8
Or something similar, with each number having only one copy of it exist. The function I've written to do this is as follows

``````int funArray
void createArray(int funArray[])
{
srand((unsigned)time(NULL);
for(int a = 0;a<4;a++)
{
for(int b = 0;b<4;b++)
{
funArray[a][b]=rand()%8+1;
}
}
}
``````

I understand that I'm just entering a random number from 1-8 into the two-dimensional array, but I'm having a hard time understanding how to populate it with pairs of numbers. Any help or resources would be appreciated. I'm not looking for someone to just do it for me, but hoping to understand what I need to do. Thanks.

## All 7 Replies

If each item is a set of 2 numbers then your array is really a 2X4X2

Thanks for the response, trying to understand the 2x4x2 explanation a little better. I failed to mention that I'm trying to make the array a different set of random numbers each time the program is run.

oops by bad the array should be 4X2X2, got my rows and columns backwards :).

One way to randomize the array is to create an array of 16 random numbers, from 1 - 8, and only allow each one to used twice. A series of nested loops will allow you to fill the funarray. The iterator for each loop multiplied by each other will give you the index.

oops,again, multiplying them together won't work. That's what I get for doing this half a sleep. Here's a way that works:

``````int funArray;
void createArray()
{
srand((unsigned)time(NULL));
int numlist;
for(int i = 0; i < 16; i++)
{
numlist[i] = rand()%8+1;
//because each unique digit will be have one copy I used 2 booleans.  When both are true then reject that number.
bool contains = false, contains2 = false;
for(int j = 0; j < i;j++)
{
if(!contains)
{
if(numlist[i] == numlist[j])
{
contains = true;
}
}
else
{
if(numlist[i] == numlist[j])
{
contains2 = true;
}
}
//I added an additional condition here to help eliminate the same digit appearing in a pair
if( (contains && contains2)|| (i%2==1 && numlist[i] == numlist[i-1]))

{
//set i back one to overwrite the last number
i--;
break;
}

}

}
//at this point if the multi-dimensional array is just to display the numbers, you can dispense with it and use the formula here to just display the numbers.
for(int a = 0;a<4;a++)
{
for(int b = 0;b<2;b++)
{
for(int c = 0;c<2;c++)
{
funArray[a][b][c]=numlist[(a*4) + (b*2) + c];
}
}
}

}
``````

Wow that looks interesting! Thanks for the response, I'll implement this and give it a shot. Is there any way to do it with keeping the array 2-Dimensional? Or would it pretty much have to exist as a 3-Dimensional array?

And I only ask that out of convenience. The program I'm using that function in calls that function a number of times to compare it with other , 2-Dimensional arrays.

Build the number list the same way, then to fill a 4X4 array you can use something like this.

``````for(int a = 0;a<4;a++)
{
for(int b = 0;b<4;b++)
{
funArray[a][b]=numlist[(a*4) + b];
}
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.