C++ 循环不正确循环

标签 c++ loops

我有一个 20 x 20 的数组,用于输出盘子的热度。我需要通过一个循环重复,直到数组中没有单元格的变化超过 0.1 度(我通过每次迭代刷新值。您将如何监视数组中任何单元格的最大变化以确定何时停止迭代?对现在我试过了,但下面的输出不正确。

#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;
}

最佳答案

while 循环的开始,您可以将名为 allSmallChanges 的 bool 变量设置为 true。在您内部的 if 语句中,您可以检查对 hot_plate[i][j] 的更改是否“太大”。如果太大,则将 allSmallChanges 设置为 false。然后,就在 while 循环结束之前,如果 allSmallChanges 仍然为真,您可以break

如果您不想达到 724 次迭代的上限,您可以去掉 runs 变量,并将循环更改为 while(true)

注意:问题中的代码在我写完这个答案后发生了变化。我不确定这个答案是否仍然适用。但是,我也确信这将是问题中代码的最后一次更改。所以,我暂时保留这个答案。

关于C++ 循环不正确循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12925276/

相关文章:

c - 为什么它不能正确添加我的号码

python - 当我希望它随每个实例而改变时,我的循环不断重复相同的值

c++ - 运算符 << 重载和常量引用

c++ - 带开始/结束 anchor 的 CAtlRegExp 表达式问题

c++ - While 循环走得太远了

c - 为什么这个语句在 while 循环中打印了两次?

python - 如何循环按顺序生成字符串?

JAVA:比较字符串和子字符串

c++ - 在函数调用上创建的对象也可以通过引用传递吗?

c++ - 加载共享库时出错 : libgsl. so.0:无法打开共享对象文件:没有这样的文件或目录