### Trying create an array size based on user input filled with random ranged numbers

My goal is for the user to input a number from the range 3 to 8 and it will a create matrices based on that size and then fill it with random numbers from the range of -12 to 8.

I think I've got the concept right but the variable length array confuses cause I know it not possible to create an array that isn't constant.

I keep getting the error "subscript requires array or pointer type"

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include #include #include #include #include using namespace std; void array_gen(int* array, int arraysize) // void function to generate the array with random numbers { int i; for (i = 0;i < arraysize;i++) { array[i] = rand() % -12 + 8; // used to generate numbers from 0 to 10 } } void arraydis(int* array, int arraysize) // function to sort array { int i; for (i = 0;i < arraysize;i++) { cout << "\n"; std:cout << array[i][j] << " "; } } int main() { int arraysize = 0, i, p; cout << "Enter array in the range of 50 <= size <= 200: "; // records the users input cin >> arraysize; int* array = new int[arraysize], freq[10]; array_gen(array, arraysize); cout << "unSorted array is:"; // shows the unsorted array arraydis(array, arraysize); } ``````
Last edited on
A 2D std::vector would be easier to create and manipulate. No need to worry about manually managing heap memory.
 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````#include #include #include int main() { unsigned mat_size { }; while (mat_size < 3 || mat_size > 8) { std::cout << "Size of matrix? (3-8) "; std::cin >> mat_size; } std::cout << '\n'; std::vector> matrix(mat_size, std::vector(mat_size)); std::random_device rnd; std::uniform_int_distribution dist(-12, 8); for (auto& row_itr : matrix) { for (auto& col_itr : row_itr) { col_itr = dist(rnd); } } for (const auto& row_itr : matrix) { for (const auto& col_itr : row_itr) { std::cout << col_itr << '\t'; } std::cout << '\n'; } }``````
 ```Size of matrix? (3-8) 9 Size of matrix? (3-8) 2 Size of matrix? (3-8) 5 -6 -12 7 1 2 -11 -5 4 -12 -8 2 3 -10 -8 7 -2 -7 -8 -11 -1 4 1 -5 4 4```
From: https://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

 ``123`` ``````int** a = new int*[rowCount]; for(int i = 0; i < rowCount; ++i) a[i] = new int[colCount];``````

Don't forget to delete ... which is the reverse ... so 2 steps

 ``1234`` ``````for(int i = 0; i < sizeY; ++i) { delete [] ary[i]; } delete [] ary;``````
A simulated 2D std::vector in 1D is just as easy to create and manipulate, 1D makes sorting the entire matrix contents from end to end easier:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include #include int main() { unsigned mat_size { }; while (mat_size < 3 || mat_size > 8) { std::cout << "Size of matrix? (3-8) "; std::cin >> mat_size; } std::cout << '\n'; std::vector matrix(mat_size * mat_size); std::random_device rnd; std::uniform_int_distribution dist(-12, 8); for (auto& itr : matrix) { itr = dist(rnd); } std::cout << "Unsorted matrix:\n"; for (size_t row { }; row < mat_size; row++) { for (size_t col { }; col < mat_size; col++) { std::cout << matrix[(mat_size * row) + col] << '\t'; } std::cout << '\n'; } std::sort(matrix.begin(), matrix.end()); std::cout << "\nSorted matrix:\n"; for (size_t row { }; row < mat_size; row++) { for (size_t col { }; col < mat_size; col++) { std::cout << matrix[(mat_size * row) + col] << '\t'; } std::cout << '\n'; } }``````
@OP didn't ask for a <vector> solution, let alone a double dose.
OP doesn't mention 2-d array. The code is mainly 1-d based. The main issue is when array is defined.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include #include #include using namespace std; void array_gen(int* array, int arraysize) // void function to generate the array with random numbers { for (int i = 0; i < arraysize; ++i) array[i] = rand() % 21 - 12; // used to generate numbers from -12 to 8 } void arraydis(int* array, int arraysize) // function to sort array { for (int i = 0; i < arraysize; i++) { cout << "\n"; cout << array[i] << " "; } } int main() { srand(time(0)); int arraysize = 0; cout << "Enter array in the range of 50 <= size <= 200: "; // records the users input cin >> arraysize; int* array = new int[arraysize]; array_gen(array, arraysize); cout << "unSorted array is:"; // shows the unsorted array arraydis(array, arraysize); delete[] array; }``````

This compiles, creates a 1d matrix using random numbers in the required range and displays the matrix. If a 2-d matrix is needed, then without using vectors one can be created as per againtry's post.
 OP doesn't mention 2-d array.

More often than not when someone mentions a matrix in a programming context they mean 2D. If I misunderstood the OP's intent, then my mistake.
 OP doesn't mention 2-d array.

No, but line 30 of the initial paste is:

`std:cout << array[i][j] << " ";`

This makes it look like @fifios is trying to treat the 1D array as a 2D array.