Fill a 2D array with non-repeating random numbers - c++

I am currently trying to fill up a 2D array with 16 values from 1-16. I want to fill up the array with non-repeating random values.
For example, if I have an array int array[4][4] how would I go through a loop filling the array while checking to make sure that another identical random value isn't placed into another location in the array?

int array[4][4];
int* p = &array[0][0];
std::iota(p, p+16, 1);
std::random_device r;
std::default_random_engine g(r());
std::shuffle(p, p+16, g);

Use int randomValue = rand() % 16 + 1; and store it in another helper 2D array for check purpose. you should be done.

Create a 1-d array, say arr of size 16, populate it with values from 1 to 16. Use shuffle on arr. Traverse through the array to fill the 2-D array, say arr2.
for(int i = 0; i < 16; i++){
arr2[i/4][i%4] = arr[i];
}

Related

converting the index of 1D array into 2D array

How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j). I want the opposite of that.
What you need to know is: How many columns should the 2D array have:
Lets say you have an array of 20 columns and 10 rows (array[20,10]):
int index = 47;
int numberOfColumns = 20;
int column = index % numberOfColumns;
int row = index / numberOfColumns;
// column == 7
// row == 2
You can just do the opposite. if n is the length of the row and x is index in 1D. You can index like
array[x/n][x%n]

How can I randomize an array?

Here's the part of the program I'm having problems with:
// precondition: board is initialized
// postcondition: board is shuffled by randomly swapping 20 values
void shuffle(int board[][NCOLS]) {
int num = rand();
num = num %6 + 1;
for (int i = 0; i < 20; i++) {
}
}
Pretty sure I have it wrong already, I think I may need the rand function but I'm not sure how the for loop would work.
Basically there are 6 pictures and they're in 4 columns, it's a memory game and as of the moment they stay in the same place. I need to make it so that they are random and flipped on the side where you can't see them but I can't figure it out.
I have no idea how to randomize columns especially when they're under the name of simply board and NCOLS.
I can see why this is hard - random_shuffle prefers 1D arrays, and you have a 2D array. Luckily, since arrays are contiguous, that means a 2D array can also be accessed as a 1D array - it's just NCOLS x NROWS elements in memory:
auto begin = &(board[0][0]);
auto end = begin + NCOLS*NROWS;

How to initialize dynamically created large integer array to a specific integer in C++?

I have created an array R like this:
int * R = new int[n];
where n takes a large value, say 100,000. I want to initialize all the integers of the array to 1 in C++. What is the best/fastest way to do this? Or, is looping through the whole array the only option?
"What is the best/fastest way to do this?"
In C++ you use std::vector<int> R(n,1); instead.
If you need the int* elsewhere, you refer to R.data().
You can std::fill from STL:
std::fill(R, R + n, 1);
But I suggest you to use std::vector instead:
std::vector<int> R(n, 1);
Don't use memset(R, 1, sizeof(int) * n), because it assigns 1 to each byte (not element) of the array.
for(int i = 0; i < n; i++){
R[i] = 1;
}
//don't forget to "delete[] R;"!
You can use vectors:
std::vector<int> R(n, 1);
But arrays and loops are best friends, in case you can't use vectors. Although the use of vector is recommended since manual deallocation is handled by the class.

C++ arrays - limit and size

How do you declare an array for a matrix that contains say 4 numbers (2x2)? I assume that int m[4] only allows numbers up to 4. Or does it mean any four numbers? I don't understand the difference.
In a declaration
type array_name[ array_size];
type is the data type that this array stores. The particular value of array under index i, i.e. array_name[i] can be any of the values that type can represent.
In your example int m[4]; declares an array of four integers. The particular value of any of these integers can be any of the values that integer can represent. To know these limits you can print them:
#include <limits>
int imin = std::numeric_limits<int>::min(); // minimum value
int imax = std::numeric_limits<int>::max(); // maximum value
The use of STD should simplify your life in the construction of a matrix :
std::vector<std::vector<int>> M(2, std::vector<int>(2));
But if you want to use arrays :
int x[2][2];
int m [4] would declare an array with 4 uninitialized values of type integer. Remember that these values are zero-indexed,
meaning that to call a value in the array you would call m[0-3]. You may assign any values you like to the array by the following command: m[4] = {Value1, Value2, Value3, Value4} If you prefer, you may also create a loop that will assign values to an array, which can be immensely useful at times.
Keep in mind that arrays are not commonly used in C++, std::vector is far more used, and for good reason.
http://www.cplusplus.com/reference/vector/vector/
int m[4] declares an array of 4 integers. The indexes of the integers will be 0, 1, 2, 3, while the values at those indexes can be any integers. so m[2] = 2003; sets the 3rd value in the array to 2003. As for the 2x2 aspect, you probably want to do something like int m[2][2]; . I think about this as declaring an array of size 2, containing arrays at each spot, instead of ints or floats or whatever. The arrays contained at each spot (there are only two spots so only two arrays in this case) each hold two ints. So if the first value in your matrix is 32, you could set that by doing m[0][0] = 32; or more generally, m[x][y] = value_of_(x,y);
The quickest way to do what you described is probably this, if you know the values ahead of time:
int row0col0 = value at 0th row 0th column;
int row0col1 = value at 0th row 1st column;
int row1col0 = value at 1st row 0th column;
int row1col1 = value at 1st row 1st column;
int m[2][2] = { {row0col0, row0col1}, {row1col0, row1col1} };
or equivalently:
int m[2][2] = {row0col0, row0col1, row1col0, row1col1};
This is referred to as row-major order: elements in a 2d array are sorted first by row, then by column.

Specific element of an array

How can I find specific element of an array?
For example, I have an array filled with 400 elements:
double Array1[400];
In another array let
double Array2[380];
I want to have values of Array1 from position 20.
How can I do this (current position - 20th position)?
In short, I want to fill Array2 with values of Array 1 from position [20] to [400].
You can use std::copy:
Copies the elements in the range [first,last) into the range beginning
at result.
#include <algorithm>
std::copy(Array1 + 20, Array1 + 400, Array2);
for(int i = 0; i < 380; i++)
{
Array2[i] = *(Array1 + 20 + i);
}