C ++循环不适当循环 [英] C++ Loop Not Looping Appropriately
问题描述
我有一个20 x 20的数组,输出一个板是多么热。我需要通过一个循环重复,直到数组中的单元格变化不超过0.1度(我通过每次迭代刷新值)。你将如何监视数组中的任何单元格的最大变化,以确定何时停止迭代?现在我已经尝试,但下面的输出不正确。
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.
#include <iostream>
#include <string>
#include <fstream>
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 < 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;
}
}
if (writeFile(hot_plate, file_name))
{
cout << "File wrote correctly\n";
}
else
{
cout << "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 > 0 && i < ARRAY_SIZE - 1 && j > 0 && j < 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 < ARRAY_SIZE; i++)
{
for (int j = 0; j < 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 < ARRAY_SIZE; i++)
{
for (int j = 0; j < ARRAY_SIZE; j++)
{
fout << HOT_PLATE[i][j];
if ( j < ARRAY_SIZE - 1)
{
fout << ", ";
}
else if (i != ARRAY_SIZE - 1)
{
fout << endl;
}
}
}
// close the input stream from the file.
fout.close();
return true;
}
推荐答案
code> while loop,你可以设置一个布尔变量,类似 allSmallChanges
到 true
。在您的内部 if
语句中,您可以检查 hot_plate [i] [j]
太大。如果太大,则将 allSmallChanges
设置为false。然后,在 while
循环结束之前,您可以 break
if allSmallChanges
仍然是真的。
At the beginning of your while
loop, you can set a boolean variable called something like allSmallChanges
to true
. In your inner if
statement, you can check to see if the change to hot_plate[i][j]
is "too big". If it is too big, then set allSmallChanges
to false. Then, just before the end of the while
loop, you can break
if allSmallChanges
is still true.
如果你不想有724次迭代的上限,你可以摆脱你的运行
变量,并将循环更改为 while(true)
。
If you don't want to have that cap of 724 iterations, you can get rid of your runs
variable, and change the loop to while(true)
.
在写这个答案后,问题中的代码发生了变化。我不确定这个答案是否仍然适用。但是,我也确信这将是问题中的代码的最后一个更改。所以,我现在离开这个答案。
Note: The code in the question got changed after I wrote this answer. I'm not sure this answer still applies. However, I'm also sure that will be the last change to the code in the question. So, I'll leave this answer as-is for now.
这篇关于C ++循环不适当循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!