### Define Previous array in loop

Here is part of my code. The rest is just the function definitions. .I have an array of 20 x 20 that outputs the temperature of a plate. I need to reiterate through a loop until no cell in the array changes more than 0.1 degree(I refresh the values through every iteration. How would you monitor the largest change for any cell in an array in order to determine when to stop iterating? Right now I have tried, but the below doesn't output correctly. I believe it is because I am incorrectly defining my previous one to compare the current one with. Any ideas?
 1234567891011121314151617181920212223242526272829303132 double hot_plate[ARRAY_SIZE][ARRAY_SIZE]; double hot_plate_prev[ARRAY_SIZE][ARRAY_SIZE]; while (true) { // This is your code for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { if (i > 0 && i < ARRAY_SIZE - 1 && j > 0 && j < ARRAY_SIZE - 1) { hot_plate[i][j] = sum_cell(hot_plate, j, i); } } } bool theSame = true; for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { if (abs(hot_plate[i][j] - hot_plate_prev[i][j]) < 0.1) { theSame = false; } hot_plate_prev[i][j] = hot_plate[i][j]; } } if (!theSame) break; }
:)
A couple of observations.

1) I assume sum_cell is supposed to give you the current value for the cell. Are j and i supposed to be reversed?

2) hot_plate_prev is never initialized. Therefore, you're comparing against random values the first time through.

1) Correct assumption; No, that was an error, I'll fix it.

2) So where would be the spot to initialize it? Still learning loops
2) Before you use it the first time.
so..

hot_plate_prev[i][j] = sum_cell(hot_plate), i, j)

above the if statement?
That depends on which if statement you're referring to.
If you're referring to the if statement at line 23, that will wipe out the previous value on subsequent iterations through the loop.

BTW, what's the purpose of the if statement at line 11? It appears to prevent you from storing into the edges of the array. Not sure if that's what you intended.
So what do you suggest then??

And yes I will probably get rid of the first IF
Last edited on
Tried all night but no luck. Dang beginner mind
I'd replace lines 7-16 with the following:
 123456 for (int i = 0; i < ARRAY_SIZE; i++) { for (int j = 0; j < ARRAY_SIZE; j++) { hot_plate_prev[i][j] = 0; hot_plate[i][j] = sum_cell(hot_plate, i, j); } }

Topic archived. No new replies allowed.