Q&A
以下のコードを実行すると、
#t=0
のみ書かれたファイルが作られてしまいます。
エラーは表示されていないのですが、どうしたら配列hの値を計算、出力されるようになるのでしょうか。
該当のソースコード
C++
1#include<iostream> 2#include<string> 3#include<cstdlib> 4#include<ctime> 5#include<fstream> 6using namespace std; 7 8string name = "sand.data"; 9ofstream datafile; 10double h[100][100]; 11double D = 0.1; 12double Q = 0.1; 13int L, L0 = 1, b = 1; 14int i = 0, j = 0; 15 16//boundary conditions 17int i_bc(int i) { 18 if (i > 100) return i - 100; 19 else if (i < 0) return i + 100; 20 else return i; 21} 22 23int j_bc(int j) { 24 if (j > 100) return j - 100; 25 else if (j < 0) return j + 100; 26 else return j; 27} 28 29 30int main() 31{ 32 datafile.open(name); 33 34 srand(time(NULL)); 35 //yuragi 36 for (i = 0; i < 100; i++) { 37 for (j = 0; j < 100; j++) { 38 h[i][j] = rand(); 39 } 40 } 41 42 for (int t = 0; t < 100; t++) { 43 44 datafile << "#t=" << t << endl; 45 46 //creep 47 for (i = 0; i < 100; i++) { 48 for (j = 0; j < 100; j++) { 49 if (h[i][j] > h[i_bc(i + 1)][j_bc(j)] && h[i][j] > h[i_bc(i)][j_bc(j + 1)] && h[i][j] > h[i_bc(i)][j_bc(j - 1)] && h[i][j] > h[i_bc(i - 1)][j_bc(j)] && h[i][j] > h[i_bc(i + 1)][j_bc(j + 1)] && h[i][j] > h[i_bc(i - 1)][j_bc(j + 1)] && h[i][j] > h[i_bc(i - 1)][j_bc(j - 1)] && h[i][j] > h[i_bc(i + 1)][j_bc(j - 1)]) { 50 h[i][j] -= D * h[i][j]; 51 h[i_bc(i + 1)][j_bc(j)] += D * h[i][j] / 6; 52 h[i_bc(i)][j_bc(j + 1)] += D * h[i][j] / 6; 53 h[i_bc(i - 1)][j_bc(j)] += D * h[i][j] / 6; 54 h[i_bc(i)][j_bc(j - 1)] += D * h[i][j] / 6; 55 h[i_bc(i + 1)][j_bc(j + 1)] += D * h[i][j] / 12; 56 h[i_bc(i - 1)][j_bc(j + 1)] += D * h[i][j] / 12; 57 h[i_bc(i - 1)][j_bc(j - 1)] += D * h[i][j] / 12; 58 h[i_bc(i + 1)][j_bc(j - 1)] += D * h[i][j] / 12; 59 } 60 else continue; 61 } 62 } 63 64 //saltation 65 for (int k = 0; k < 10; k++) { 66 int i_ran = rand() % 100; 67 int j_ran = rand() % 100; 68 if (h[i_ran][j_ran] > 0) { 69 L = L0 + b * h[i_ran][j_ran]; 70 h[i_bc(i_ran)][j_bc(j_ran)] -= Q; 71 h[i_bc(i_ran + L)][j_bc(j_ran)] += Q; 72 } 73 else continue; 74 } 75 76 for (i = 0; i < 100; i++) { 77 for (j = 0; j < 100; j++) { 78 datafile << i << " " << j << " " << h[i][j] << endl; 79 } 80 datafile << endl << endl; 81 } 82 83 } 84 85 datafile.close(); 86 cout << "Output data in " << name << "." << endl; 87 return 0; 88 89}
回答1件
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/02/03 06:09
2023/02/04 01:37
2023/02/04 03:12