I have a list of numbers in a 2d array. I need a random number without repeating.
The number zero cannot be shuffle.

int **map;
int * list;
void randomize(){


	for ( int x=0;x<Width;x++)
	{
		for( int y = 0; y < Height; y++)
		{
          map[x][y]=list[x*y];

//add code

}

Recommended Answers

All 10 Replies

This is a c++ forum.

Still, the logic can be applied in C++.

I am trying to copy the map. It can only copy part of the map.

void randomize(int Width,int Height){

	list = new int[Width*Height];
	for ( int x=0;x<Width;x++)
	{
		for( int y = 0; y < Height; y++)
		{
			list[x*y]=map[x][y];
			cout << map[x][y];

		}
		cout<<"\n";
	}

}

The number zero cannot be shuffle.

Can you clarify what you mean by that?

It's not perfectly random by any stretch, but the easiest would be to choose a row and choose a column, check if that element's already in the list, if not add it, if so, choose a new row and column. I understand what you are trying to do, but I don't think it's the best approach.

Maybe what you're looking for is:

int* list = new int[Width][Height];

Maybe what you're looking for is:

int* list = new int[Width][Height];

Did you try that in the compiler? It doesn't work. For a dynamic nthDimensional array you have to build it up.

[rows,columns]

int ** my2Darr = new int*[rows];
for (int i = 0;i<rows;i++)
     my2Darr[i] = new int[columns]; //for each element of the row array, make a column array

Did you try that in the compiler? It doesn't work. For a dynamic nthDimensional array you have to build it up.

[rows,columns]

int ** my2Darr = new int*[rows];
for (int i = 0;i<rows;i++)
     my2Darr[i] = new int[columns]; //for each element of the row array, make a column array

Now I know. Thank you.

I want to make a map with random numbers from the map. First, copy the map. Then paste it in the list. Now I need to shuffle those numbers.I don`t want to shuffle the number zero. Can you give me a hint?

void randomize(int Width,int Height){

	list = new int*[Width];
	for ( int x=0;x<Width;x++)
	{
		list[x] = new int[Height];
		for( int y = 0; y < Height; y++)
		{
			list[x][y]=map[x][y];
			cout << map[x][y];

		}
		cout<<"\n";
	}


}

Choose an x from the range of x, chose a y from the range of y, choose x1 from the range of x, choose y1 from the range of ys.

If list[x][y] or list[x1][y1] is 0, neither is swapped and a new set of x,y,x1,y1 is chosen.
Else, swap list[x][y] and list[x1][y1] and pick a new set of x,y,x1,y1

It's not as random as can be, but it should suffice for your assignment.

Be a part of the DaniWeb community

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