I cannot copy 2d array. There are no errors in my error list. What is wrong with my code? The problem might be located in create(). Thanks in advance.
Here is my code.

int **map
void randomize(int numberToSelect,int range, int *list){

int* remainingNumbers = new int[range];
int* chosenNumbers = new int[numberToSelect];
 
//create an array
for(int i = 0; i < range; i++)
{
	remainingNumbers[i] = i;
}
//randomize
for(int i = 0; i < numberToSelect; i++)
  {
    const int selectedElement = rand() % (range - i); chosenNumbers[i]=remainingNumbers[selectedElement] + 1; 
    remainingNumbers[selectedElement]=remainingNumbers[range-i];
}
delete [] remainingNumbers; 

//copy the random numbers without repeating.
for(int i = 0; i < range; i++)
{
list[i] = chosenNumbers[i];
cout << list[i] << endl;
}

delete [] chosenNumbers;

}
bool Memalloc(int Width,int Height)//allocate map
{
    map = new int*[Height];
 
    for( int x=0;x<Height;x++)
    {
        map[x] = new int[Width];
    }
 
    return true;
}
void create(int Width, int Height){

	Memalloc(Width,Height);
	int Area, *number;
	Area = Width * Height;
	number = new int[Area];

	randomize(Area,Area,number);
for (int i = 0; i < Height; i++)
{
	for (int j = 0; j < Width; j++)
	{

//This might be my error.
		int index = Width * (i -1) + j;
		while(index > 0)
		{
		map[i][j] = number[index];
		cout << map[i][j] << " ";
		}
	}
	cout << endl;
}
	}

Edited 5 Years Ago by alexchen: n/a

When i and j are 0, index = -width, which is incorrect. For the row 1 (the second row) you want width*1 + j (offset of 1 row + the column offset).

How can the index be calculated? Is there any formula to calculate index?

for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{

int index = Width * (i -1) + j;//error, Can you calculate index.
map[i][j] = number[index];
cout << map[i][j] << " ";

}
}

Edited 5 Years Ago by alexchen: n/a

How can the index be calculated? Is there any formula to calculate index?

for (int i = 0; i < Height; i++)
{
for (int j = 0; j < Width; j++)
{

int index = Width * (i -1) + j;//error, Can you calculate index.
map[i][j] = number[index];
cout << map[i][j] << " ";

}
}

This is what's confusing me. I thought you had already figured out the formula in your other thread...

int index = j + i * Width;

This looks about right to me, though I'm not guaranteeing anything. I had assumed that it had worked correctly since you had marked your last thread solved. Apparently this formula is not working for you? What is the problem?

Comments
This formula only works with create(5,2). Thanks

It only works with this numbers. create(5,2). Index might be negative or zero.
I doesn`t work with bigger numbers like create(6,5) or create(20,10) .

Edited 5 Years Ago by alexchen: n/a

>> It only works with this numbers. create(5,2). Index might be negative or zero.

Zero is perfectly acceptable. Negative is not. Get rid of the "might be". Display index
and find out for sure.


>> I doesn`t work with bigger numbers like create(6,5) or create(20,10)

Find out exactly where and how it breaks and for what values. (5,3)? (5,4)? (6,3)?

"It doesn't work" is too vague. Does it not compile? Does it give a run-time error? Does it run to completion and give bad results? What's the exact error message? Does it always break in exactly the same spot? Details, details, details.

Create(6,5) .I am sure index is zero at the begging. The other numbers are positive.

for (int i = 0; i < Height; i++)
{
	for (int j = 0; j < Width; j++)
	{

		int index = j + i * Width;
		
		switch(index)
		{
		case -1: cout << "Index is negative" <<endl;
			break;
		case 0: cout << "Index is o" << endl;
			break;
		default: cout << "It is a positive number" <<endl;
		}
      }
}

Edited 5 Years Ago by alexchen: n/a

A switch statement won't work here. You need an if-else if-else statement. What if index is -2? It'll go to the default case and your output will say it's positive.

index needs to range from 0 to Width * Height - 1. In your case, with 6 and 5, 6 times 5 is 30, 30 minus 1 is 29, so index needs to range from 0 to 29, inclusive, 30 numbers total, every number from 0 to 29 occurring once and only once and index can never be less than 0 or greater than 29.

Display index inside the loop. Make sure the above criteria is fulfilled. If it is, the formula works, as far as I can tell, and hence the problem is elsewhere. If not, it definitely doesn't work.

Experiment adjusting Width and Height. If (5,6) works, but (6,5) does not, then there is a good chance that somewhere you've mixed up Width and Height.

If everything works for small numbers, but not large numbers, particularly if there are values where anything smaller always works and anything larger always fails, you've likely run into some size constraint, possibly not enough memory allocated. You'll want to make sure that your "new" commands succeed. Right now you aren't checking for errors. You may need to.

Again, how EXACTLY does this program fail?

Edited 5 Years Ago by VernonDozier: n/a

I found a mistake. It is located in randomize(). The random number generator is not working. My random number generator produces an undefine variable.Create(6,6);

void randomize(int numberToSelect,int range, int *list){

int* remainingNumbers = new int[range];
int* chosenNumbers = new int[numberToSelect];
 
//create an array
for(int i = 0; i < range; i++)
{
	remainingNumbers[i] = i;
}
//randomize
for(int i = 0; i < numberToSelect; i++)
  {
    const int selectedElement = rand() % (range - i);
    chosenNumbers[i]=remainingNumbers[selectedElement] + 1; 
    remainingNumbers[selectedElement]=remainingNumbers[range-i];
}
delete [] remainingNumbers; 

//copy the random numbers without repeating.
for(int i = 0; i < range; i++)
{
list[i] = chosenNumbers[i];
cout << list[i] << endl;
}

delete [] chosenNumbers;

}
 int main(int *number)
 {
	number = new int[6];
	randomize(6,6,number);

	system("pause");
	return 1;
 }

Edited 5 Years Ago by alexchen: n/a

Thanks for your help.

void randomize(int numberToSelect,int range, int *list){

int* remainingNumbers = new int[range];
int* chosenNumbers = new int[numberToSelect];
 
//create an array
for(int i = 0; i < range; i++)
{
	remainingNumbers[i] = i;
}
//randomize
for(int i = 0; i < numberToSelect; i++)
  {
    const int selectedElement = rand() % (range - i);
    chosenNumbers[i]=remainingNumbers[selectedElement]; 
    remainingNumbers[selectedElement]=remainingNumbers[range-i - 1];
}
delete [] remainingNumbers; 

//copy the random numbers without repeating.
for(int i = 0; i < range; i++)
{
list[i] = chosenNumbers[i];
cout << list[i] << endl;
}

delete [] chosenNumbers;

}
This question has already been answered. Start a new discussion instead.