C++ 我需要帮助结束更新2D数组的循环
这段代码中有一部分我需要帮助。该程序用于计算平板上的稳态温度分布。给我的提示是: 您应该继续迭代,直到阵列中的单元变化不超过0.1度,并在每次迭代中计算所有内部单元的温度。您的程序应该监视数组中任何单元格的最大更改,以确定何时停止重复 我被卡住了!我目前正在使用while循环来获得正确的答案,但我就是不知道如何让它执行提示中要求的操作。任何帮助都将不胜感激C++ 我需要帮助结束更新2D数组的循环,c++,arrays,C++,Arrays,这段代码中有一部分我需要帮助。该程序用于计算平板上的稳态温度分布。给我的提示是: 您应该继续迭代,直到阵列中的单元变化不超过0.1度,并在每次迭代中计算所有内部单元的温度。您的程序应该监视数组中任何单元格的最大更改,以确定何时停止重复 我被卡住了!我目前正在使用while循环来获得正确的答案,但我就是不知道如何让它执行提示中要求的操作。任何帮助都将不胜感激 #include <iostream> #include <string> #include <iomanip
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
const int SIZE = 20;
const double HEAT = 100;
const double EDGES = 0;
const int SURROUNDING = 4;
const int STABLE = .1;
// Initializes the first array
void begining_plate ( double plate[][SIZE]) {}
// Calculates one ideration of the steady-state distribution
double average_temp_calc(double plate[][SIZE], int a, int b) {}
// Prints the array
void print_plate( double plate[][SIZE]) {
// Exports the array to a .csv file
bool send_plate_info(double plate[][SIZE])
int main() {
// Part 1 - Initialize and Print 2D Array
cout << "Here is the initial heat: " << endl;
double plate[SIZE][SIZE];
begining_plate(plate);
print_plate(plate);
// Part 2 - Update Elements once
double plate_saved[SIZE][SIZE];
cout << "\nHere is the first run of the averaged plate.\n";
for (int a = 0; a < SIZE; a++) {
for (int b = 0; b < SIZE; b++) {
if (a != 0 && a != SIZE - 1 && b != 0 && b != SIZE - 1) {
plate_saved[a][b] = plate[a][b];
plate[a][b] = average_temp_calc(plate, a, b);
}
else {
plate_saved[a][b] = plate[a][b];
plate[a][b] = plate[a][b];
}
}
}
print_plate(plate);
cout << endl << endl;
// Part 3 - Repeat update until stalbe
******* HERE IS THE PART I NEED HELP WITH **********
int count = 0;
int stable = 150;
while (count < stable) {
for (int a = 0; a < SIZE; a++) {
for (int b = 0; b < SIZE; b++) {
if (a != 0 && a != SIZE - 1 && b != 0 && b != SIZE - 1) {
plate_saved[a][b] = plate[a][b];
plate[a][b] = average_temp_calc(plate, a, b);
}
else {
plate_saved[a][b] = plate[a][b];
plate[a][b] = plate[a][b];
}
}
}
count++;
}
// Part 4 - Using Excel to Display Results
if (send_plate_info(plate))
{
cout << "File wrote correctly\n";
}
else
{
cout << "The file did not write!\n";
}
system("pause");
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
常数int SIZE=20;
常数双倍热=100;
常数双边=0;
常数int=4;
常数int稳定=.1;
//初始化第一个数组
空白起始板(双板[]尺寸]{}
//计算稳态分布的一个理想值
双平均温度计算(双板[]尺寸],内部a,内部b){
//打印阵列
空印版(双印版[][尺寸]){
//将阵列导出为.csv文件
bool发送板信息(双板[]尺寸])
int main(){
//第1部分-初始化和打印二维数组
cout应根据不稳定点的数量而不是循环的数量进行计数:
for (int a = 0; a < SIZE; a++) {
for (int b = 0; b < SIZE; b++)
{
if (a != 0 && a != SIZE - 1 && b != 0 && b != SIZE - 1) {
plate_saved[a][b] = plate[a][b];
plate[a][b] = average_temp_calc(plate, a, b);
}
else {
plate_saved[a][b] = plate[a][b];
plate[a][b] = plate[a][b];
}
if ( abs(plate_saved[a][b]-plate[a][b]) > STABLE )
++count;
}
编辑
请注意,不稳定点的计数器必须在每次迭代开始时重置,以便解决方案如下所示:
do
{
count = 0;
for (int a = 0; a < SIZE; a++) {
for (int b = 0; b < SIZE; b++)
{
double plate_saved = plate[a][b];
// Compute new value of plate[a][b]
if ( fabs(plate_saved-plate[a][b]) > STABLE )
++count;
}
}
while (count>0);
do
{
计数=0;
对于(int a=0;a稳定)
++计数;
}
}
而(计数>0);
所以我尝试了你的建议,并尝试了一下,但每次都陷入了无限循环。非常感谢!你是个救命恩人!我看到你的评论,说你自己解决了额外的问题,恭喜:-)
do
{
count = 0;
for (int a = 0; a < SIZE; a++) {
for (int b = 0; b < SIZE; b++)
{
double plate_saved = plate[a][b];
// Compute new value of plate[a][b]
if ( fabs(plate_saved-plate[a][b]) > STABLE )
++count;
}
}
while (count>0);