gpt4 book ai didi

c++ - 我需要帮助结束更新二维数组的循环

转载 作者:太空狗 更新时间:2023-10-29 23:00:34 24 4
gpt4 key购买 nike

这段代码中有一部分我需要帮助。此代码正在计算板上的稳态温度分布。我得到的提示是:

You should continue to iterate until no cell in the array changes more than 0.1 degree, calculating the temperature for all interior cells on each iteration. Your program should monitor the largest change for any cell in the array in order to determine when to stop reiterating.

我卡住了!我目前正在使用 while 循环来获得正确的答案,但我无法弄清楚如何让它按照提示中的要求进行操作。任何帮助将非常感激!

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

最佳答案

应该计算不稳定点的数量而不是循环的数量:

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
{...}
while (count>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);

关于c++ - 我需要帮助结束更新二维数组的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33430016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com