Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Loop Not Looping Appropriately
    text
    copied!<p>I have an array of 20 x 20 that outputs how hot a plate is. 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.</p> <pre><code>#include &lt;iostream&gt; #include &lt;string&gt; #include &lt;fstream&gt; using namespace std; const int ARRAY_SIZE = 20; const int NEIGHBORS = 4; void initialize(double hot_plate[][ARRAY_SIZE]); bool writeFile(const double HOT_PLATE[][ARRAY_SIZE], const string FILE_NAME); double sum_cell(const double HOT_PLATE[][ARRAY_SIZE], const int CELL_X, const int CELL_Y); int main() { double hot_plate[ARRAY_SIZE][ARRAY_SIZE]; double hot_plate_prev[ARRAY_SIZE][ARRAY_SIZE]; initialize(hot_plate); string file_name = "hot_plate.csv"; //accuracy up to 4 decmials int runs = 724; 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 &lt; ARRAY_SIZE; i++) { for (int j = 0; j &lt; ARRAY_SIZE; j++) { if (i &gt; 0 &amp;&amp; i &lt; ARRAY_SIZE - 1 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; ARRAY_SIZE - 1) { hot_plate[i][j] = sum_cell(hot_plate, j, i); } } } bool theSame = true; for (int i = 0; i &lt; ARRAY_SIZE; i++) { for (int j = 0; j &lt; ARRAY_SIZE; j++) { if (abs(hot_plate[i][j] - hot_plate_prev[i][j]) &lt; 0.1) { theSame = false; } hot_plate_prev[i][j] = hot_plate[i][j]; } } if (!theSame) break; } } if (writeFile(hot_plate, file_name)) { cout &lt;&lt; "File wrote correctly\n"; } else { cout &lt;&lt; "The file did not write!\n"; } //system("pause"); return 0; } double sum_cell(const double HOT_PLATE[][ARRAY_SIZE], const int CELL_X, const int CELL_Y) { /* This code should never go out of bounds as it's in an if statement if (i &gt; 0 &amp;&amp; i &lt; ARRAY_SIZE - 1 &amp;&amp; j &gt; 0 &amp;&amp; j &lt; ARRAY_SIZE - 1) */ double cell_num = HOT_PLATE[CELL_X - 1][CELL_Y]; // Top cell_num += HOT_PLATE[CELL_X][CELL_Y - 1]; // Left cell_num += HOT_PLATE[CELL_X][CELL_Y + 1]; // Right cell_num += HOT_PLATE[CELL_X + 1][CELL_Y]; // Bottom cell_num /= NEIGHBORS; return cell_num; } // setup the Array so all values are defined when starting void initialize(double hot_plate[][ARRAY_SIZE]) { for (int i = 0; i &lt; ARRAY_SIZE; i++) { for (int j = 0; j &lt; ARRAY_SIZE; j++) { if (i == 0 || i == ARRAY_SIZE - 1) { if (j == 0 || j == ARRAY_SIZE - 1) { hot_plate[i][j] = 0.0; } else { hot_plate[i][j] = 100.0; } } else { hot_plate[i][j] = 0.0; } } } } // Write the data to the CSV file bool writeFile(const double HOT_PLATE[][ARRAY_SIZE], const string FILE_NAME) { // open the file ofstream fout(FILE_NAME); if (fout.fail()) return false; for (int i = 0; i &lt; ARRAY_SIZE; i++) { for (int j = 0; j &lt; ARRAY_SIZE; j++) { fout &lt;&lt; HOT_PLATE[i][j]; if ( j &lt; ARRAY_SIZE - 1) { fout &lt;&lt; ", "; } else if (i != ARRAY_SIZE - 1) { fout &lt;&lt; endl; } } } // close the input stream from the file. fout.close(); return true; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload