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