C++ C++;循环未正确循环
我有一个20 x 20的阵列,可以输出盘子的温度。我需要通过一个循环进行重复,直到数组中的单元格变化不超过0.1度(我在每次迭代中都会刷新值。如何监视数组中任何单元格的最大变化,以确定何时停止迭代?目前我已经尝试过,但下面的输出不正确C++ C++;循环未正确循环,c++,loops,C++,Loops,我有一个20 x 20的阵列,可以输出盘子的温度。我需要通过一个循环进行重复,直到数组中的单元格变化不超过0.1度(我在每次迭代中都会刷新值。如何监视数组中任何单元格的最大变化,以确定何时停止迭代?目前我已经尝试过,但下面的输出不正确 #include <iostream> #include <string> #include <fstream> using namespace std; const int ARRAY_SIZE = 20; const i
#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;
}
#包括
#包括
#包括
使用名称空间std;
常量int数组_SIZE=20;
常数int=4;
无效初始化(双热板[]阵列尺寸];
布尔写文件(常数双热板[][阵列大小],
常量字符串文件名);
双和单元(常数双热板[][阵列大小],
const int CELL_X,const int CELL_Y);
int main()
{
双热板[阵列大小][阵列大小];
双热板前[阵列大小][阵列大小];
初始化(热板);
字符串文件\u name=“hot\u plate.csv”;
//精度高达4个数字
整数运行=724;
双热板[阵列大小][阵列大小];
双热板前[阵列大小][阵列大小];
while(true)
{
//这是你的密码
for(int i=0;i0&&i0&&j cout在while
循环开始时,可以将一个名为allSmallChanges
的布尔变量设置为true
。在内部if
语句中,可以检查对热板[i][j]
的更改是否“太大”。如果太大,则将allSmallChanges
设置为false。然后,在while
循环结束之前,如果allSmallChanges
仍然为true,则可以中断
如果您不想拥有724次迭代的上限,可以去掉运行
变量,并将循环更改为while(true)
注意:在我写下此答案后,问题中的代码发生了更改。我不确定此答案是否仍然适用。但是,我也确定这将是问题中代码的最后一次更改。因此,我将暂时保留此答案。.1
不在您的代码中,这可能与您的错误有关。这不会循环,直到没有cell变化超过0.1度,只循环724次。您的问题标题严重误导hot_plate[i][j]=sum_cell(hot_plate,j,i);/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^您确定要执行矩阵转置吗?原来发布的旧版本代码无法编译,您“修复”了错误。
@MooingDuck:是的,这也行得通,不过你必须在循环外声明并将其初始化为大于0.1的值,然后在循环内将其归零。我只是喜欢它的可读性。不过你说得对,它会有wierd初始化规则。谢谢,@MooingDuck…但现在我不确定它有多大意义;)@user1751615…这应该差不多可以用了。除了它看起来不像你在与之比较之前设置了hot\u plate\u prev[i][j]
。而且,虽然你可以用数组来做,但这有点浪费。你只需要跟踪一个变化。因此,在的内部for
循环中,你可以说double old\u val=hot\u plate[i][j]
。然后,对照old_val
。这样只会占用一个内存位置,而不是400个。