Filling 3-d array with random number with conditions

Hello all,

First off, I would like to thank you for reading this. I am grateful for any help/hints I can get.

I have a dynamically allocated 3-d array (ex: array[x-3][row-3][col-2]) but now i have to generate random numbers to fill them.

Condition 1: fill those with a random number between 0 - 100 (easy enough)

but here is where i am banging my head against the wall:

Condition 2: the number between each columns cannot differ by more than 50;

condition 3: In the first column there must be an in increase in number for three rows.

condition 4: In the second column there must not be a decrease in number for three rows

condition 5: for both columns, the number in each row cannot be more than 70.
Last edited on
5 is easy, for those, just use the same as 1 but instead of 100, use 70.

that leaves 2/3/4
there are 2 ways to do this, I guess.
1) you can generate numbers and if they fail criteria, re-roll the number or
2) make a fancy random generator function(s) that do it for you. This would involve keeping track of the last number you made (to see if it is increasing or decreasing or that it is within 50 of previous, etc.
Here are some hints:

Condition 2: (rand() % 50 + prev_col_value - 25) % 70 (Note that this may lead to negative numbers) For second column be aware of Condition 3
Condition 3:
First col: When (row_index % 3) != 0
rand() % (70 - (prev_row_value + 1)) + prev_row_value + 1
otherwise -> rand() % (70 - 3)

Second col: Maybe you get the idea? Note: Condition 2

Note that you need to test this specifically with the extreme values.
Thank you or taking the time to answer me. I would like to say that am very new to CS and haven't developed an intuition that many here have, so speak to me as you would a slow person. This is hard for me.

so this is what i got from what you said (it's hard for me to look at it and say "of course" because my brain haven't really digest it fully):


My questions is, do i need to create another function to meet condition 3 and 4? or do I just put it here? I am still playing with it but kind of busy with another classwork to give it undivided attention.

1
2
3
4
5
6
7
8
9
10
11
12

void fillArray(int* array, int scenario, int Ndays, const int PROD)
{
	for (int i = 0; i < scenario; i++)
		for (int j = 0; j < Ndays; j++)
			for(int k = 0; k < PROD; k++)
			{
				int randNumA = (rand() % MAX);
				int randNumB = abs((rand() % 50+ *(array) - 25) % 70);
				*(array + i * Ndays * PROD + j * PROD + k) = randNumB;
			}
}
just keep it all in one place.
Thank you jonnin and coder777! I appreciate the help much very much. I spent over 16 hours on this assignment in the past week and this part has been a struggle for me.

I will spend more time on this tonight!
Topic archived. No new replies allowed.