I am currently working on tweaking a project for my class where we have to allow the user to roll dice (or generate random numbers 1-6) for a total of 13 turns. I cannot generate all of the numbers at once (like save all values to a 2D array in one go) but I have to be able to save the values from the first turn (3 4 5 5 1), second turn (4 5 6 2 1) and so on separately.
How would I go about this? Here is the current code for this if you want to take a look. As you will see, I can only generate all of the numbers at once and save them to a 2D array:
Thank you so much! This worked perfectly. Out of pure curiosity since I am a beginner in C++, would you mind explaining this bit of code to me:
1 2 3
int** array = newint*[NO_ROWS];
for(int i = 0; i < NO_ROWS; ++i)
array[i] = newint[NO_COLS];
I have no idea what it does but when I change too much about it the entire thing stops working. I just want to become a better programmer and be knowledgeable about things like this.
It's creating a 2-d array. L1 creates an array of pointers to each of the columns. L2-3 creates a column array for each of the rows and sets the appropriate row pointer to point to its column.
The provided code is missing the code needed to delete each of the allocated arrays.
Of course, the delete isn't missing at all in my code because the array is finished with and cleaned up when the program exits, the delete is automatic. There are plenty of cases where delete need to be explicitly called and this is shown in the reference.
The sort of array construction I have used to suit your data model is one amongst many so there are no hard and fast rules or implementations.
You might find this of use. You can specify the number of turns and the number of dice used on the command line. Why do you have to save the generated numbers - for just the output there's no need?
I needed to save the output so that I could plug it into a separate function taking in a 2d array that performs certain actions on each row. I don't think that I would have access to the numbers without saving them for later.