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"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include <random>
#include<time.h>
#include<iomanip>
#include <vector>
#include <map>


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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <vector>
#include <random>

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<std::vector<int>> matrix(mat_size, std::vector<int>(mat_size));

   std::random_device rnd;
   std::uniform_int_distribution<int> 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

1
2
3
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

1
2
3
4
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>

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<int> matrix(mat_size * mat_size);

   std::random_device rnd;
   std::uniform_int_distribution<int> 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <random>
#include <ctime>

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.
Topic archived. No new replies allowed.