-1

I am trying to create an 2D array. The number of columns is already defined, it will be a constant equal to 20. On the other side, I want the program will generate a number between 30 and 40, which will be the number of rows. I have already created the function to generate numbers between two values. The problem is that it won't allow me declare the array since one of the number of rows is not a constant value and the program can't figure out the space needed to reserve. How can I do it without using vectors or pointers?

const int COLUMNS = 20;  /
int numOfRows = genNumInRange(30, 40), //Gets the random number from the function.
    array[numOfRows][COLUMNS]; //
ERROR : Expression must have a constant value.

Any possible way I can do that?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • Use `std::vector`. And please change the name of the variable to something other than `array`, since there is already a `std::array` class in C++. – PaulMcKenzie Sep 12 '21 at 04:35
  • We are not allowed to use vectors for this exercise. – GGeri199 Sep 12 '21 at 04:38
  • Then what are you allowed to use? Variable arrays do not exist in C++. If you were not told what to use, go back to your teacher and ask them what you're supposed to use. A dynamic array in C++ is accomplished by using `std::vector`. Obviously you have hit a road block and cannot use what you thought you could use. – PaulMcKenzie Sep 12 '21 at 04:39
  • *Any possible way I can do that?* -- Well, if you keep trying what you've been trying using arrays, the short answer is "no, there is no possible way". – PaulMcKenzie Sep 12 '21 at 04:44
  • If you're not allowed to use vectors (which I can understand from a learn memory managment point of view, but not from a learn good C++ point of view). Then you must think of a way how you could allocate memory dynamically (which means at runtime instead of compile time) – Pepijn Kramer Sep 12 '21 at 05:07
  • *We are not allowed to use vectors for this exercise.* -- How about `std::deque`? You need to clarify with whoever gave you this assignment what you can use. If they just threw you into into the wind where you must discover what to use, then that's not the way you're supposed to teach students, given the complexity of the C++ language itself. Obviously they didn't mention anything about dynamically allocated memory (or you didn't pay attention), else you would have mentioned that in the question itself as that is the way to accomplish what you're trying to accomplish. – PaulMcKenzie Sep 12 '21 at 05:27
  • @PaulMcKenzie I even think they should start teaching about STL data structures first! And then the details of "dynamic memory managment" later (if needed at all). – Pepijn Kramer Sep 12 '21 at 05:30
  • C++ is one of those languages where the prerequisites to an exercise need to be mentioned up front by whoever is teaching the course, otherwise the teacher is wasting the student's time if that student picked something else other than `std::vector`, and then discover later they can't use that other alternative. – PaulMcKenzie Sep 12 '21 at 05:37
  • So the answer was easier than I expected. I should have just declared the array with the maximum possible number of rows (40). After that, I should get the random number of rows that would be filled with data. In case the generated number was less than 40, for example 35, 35 rows would be filled with random data, and the remaining 5 would be empty. So technically it is not a dynamic array. – GGeri199 Sep 20 '21 at 15:42

1 Answers1

-1

You can malloc as many array[COLUMNS] as you need.

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32
Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27