2012-10-17 70 views
0

可能重複:
C++ Loop Not Looping Appropriately如何修復此循環? C++

Warning..I相信新的C++編程...我有20×20,其輸出板有多熱是一個數組。我需要重複循環,直到數組中的單元格的變化不超過0.1度(我通過每次迭代刷新值。如何監視數組中任何單元格的最大變化以確定何時停止迭代?現在我都試過了,但正確的下方不輸出。

#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 

using namespace std; 


const int array_size = 20; // set array size and neighbors to calculate rest of cells 
const int cell_neighbors = 4; 

void initialize(double hot_plate[][array_size]); //functions here 
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); 
double value_cell(const double hot_plate[][array_size], 
      const int cell_X, const int cell_Y); 



int main() 
{ 
double hot_plate[array_size][array_size];//set variables 

initialize(hot_plate); //run function 

string file_name = "hot_plate.csv"; //file name for Excel 

//repeat until stable 
int repeat_until_stable = 1000; 
while (repeat_until_stable > 0) 
{ 
    for (int a = 1; a < array_size - 1; a++) 
    { 
     for (int b = 1; b < array_size - 1; b++) 
      { 

      { 
       hot_plate[a][b] = sum_cell(hot_plate, b, a); 
      } 
     } 
    } 
    repeat_until_stable--; 
} 

if (writeFile(hot_plate, file_name)) //check functionality of the program 
{ 
    cout << "Excel File created\n"; 
} 
else 
{ 
    cout << "The Excel file did not write\n"; 
} 

system("pause"); 

return 0; 
} 

//function definition 

double sum_cell(const double hot_plate[][array_size], 
      const int cell_X, const int cell_Y) 
{ 
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 /= cell_neighbors; // find average of the 4 values closest to cell 

return cell_num; 
} 

// setup the array so all values are defined 
void initialize(double hot_plate[][array_size]) 
{ 
for (int a = 0; a < array_size; a++) 
{ 
    for (int b = 0; b < array_size; b++) 
    { 
     if (a == 0 || a == array_size - 1) 
     { 
      if (b == 0 || b == array_size - 1) 
      { 
       hot_plate[a][b] = 0.0; 
      } 
      else 
      { 
       hot_plate[a][b] = 100.0; 
      } 
     } 
     else 
     { 
      hot_plate[a][b] = 0.0; 
     } 
    } 
} 
} 
double value_cell(const double hot_plate[][array_size], 
      const int cell_X, const int cell_Y) 
{ 
double cell_value = hot_plate[cell_X][cell_Y]; // cell value 

return cell_value; 
} 
// Write the data to the Excel CSV file 
bool writeFile(const double hot_plate[][array_size], 
      const string file_name) 
{ 
// open the excel file 
ofstream fout(file_name); 
if (fout.fail()) 
    return false; 

for (int a = 0; a < array_size; a++) 
{ 
    for (int b = 0; b < array_size; b++) 
    { 
     fout << hot_plate[a][b]; 
     if (b < array_size - 1) 
     { 
      fout << ", "; 
     } 
     else if (a != array_size - 1) 
     { 
      fout << endl; 
     } 
    } 
} 

// close the input stream from the file. 
fout.close(); 
return true; 
} 
+0

你有沒有考慮告訴我們你的代碼究竟出了什麼問題? – Grizzly

+0

你試圖找出板上溫度在一定範圍內的最熱區域? – yngccc

+0

我想我不明白我的舊價值在哪裏與 – user1751615

回答

1

將保存時發出的最大變化的變量,並檢查它後for循環

double maxChange=.11; 
. 
. 
. 
while(maxChange>.1) 
{ 
     maxChange=-1; 
     for(.....) 
      for(.....) 
      { 
        change=fabs(oldValue-plate[a][b]); 
        if(change>maxChange) 
         maxChange=change; 
      } 

} 
+0

死亡簡單而高效。 +1 – Offirmo