2015-10-30 90 views
3

有一部分代碼需要幫助。該代碼正在計算板上的穩態溫度分佈。我給出的提示狀態如下:我需要幫助結束更新2D陣列的循環

您應該繼續迭代,直到數組中的單元格變化不超過0.1度,計算每次迭代中所有內部單元的溫度。您的程序應該監視數組中任何單元的最大變化,以確定何時停止重複。

而我卡住了!我目前使用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; 
} 
+0

我看到你的評論,你自己解決了額外的問題,恭喜! - ) –

回答

1

計數應在不穩定點的數量,而不是循環的數量來完成:

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); 
+0

所以我嘗試了你的建議,並且稍微玩了一下,但每次都陷入無限循環。 –

+0

非常感謝!你是一個拯救生命的人! –