Gauss-Seidelメソッドを使用して方程式系を解くC++コードを作成していますが、質問に答えるために何が必要なのかを知る必要はありません)。これは、基本的に、ユーザが一連の値を2D配列に入力することを含む。私の問題は、値がsin(45)のようなものか、あるいは3.14,2.120947、または3などの数字ではないものの場合に発生します。コードのメインループのスニペットを次に示します。配列にユーザーが入力した後のC++ランタイムエラー
/* code to solve an nxn system using the Gauss-Seidel method */
int i, j, n;
int violation_counter, answer;
int violation_rows[MAX_DIM];
double sum;
double a[MAX_DIM][MAX_DIM];
double b[MAX_DIM],x[MAX_DIM];
/* read in data */
n = MAX_DIM + 1;
while (n > MAX_DIM)
{
cout << "Enter the dimension of the system to be solved: " << endl;
cin >> n;
}
/*THIS BLOCK IS WHAT WE'RE CONCERNED WITH*/
cout << endl;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("Enter a[%d][%d] of the system matrix: ", i, j);
cin >> a[i][j];
}
printf("Enter b[%d] of the right-hand-side vector: ", i);
cin >> b[i];
cout << endl;
}
/*THE REST OF THE CODE IS FINE I THINK*/
/* test the convergence criterion */
violation_counter = 0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++)
if (i != j)
sum = sum + abs(a[i][j]);
if (abs(a[i][i]) < sum) {
violation_rows[violation_counter]= i;
violation_counter = violation_counter + 1;
}
if (a[i][i] == 0.0) {
cout << "Found diagonal element equal to zero; rearrange equations; exiting." << endl;
exit (-1);
}
}
if (violation_counter > 0) {
printf ("The Gauss-Seidel convergence criterion is violated in %d rows out of %d\n", violation_counter, n);
printf ("Specifically, it was violated in rows:\n");
for (i = 0; i < violation_counter; i++) {
printf("%d ",violation_rows[i]);
printf("\n");
}
printf("Enter 1 if you want to continue; any other number to abort: ");
cin >> answer;
if (answer != 1)
exit(-1);
printf ("Check results carefully\n\n");
}
/* initialize the solution vector -- initial guesses */
for (i = 0; i < n; i++) {
printf ("Enter an initial guess x[%d] of the solution vector: ", i);
cin >> x[i];
}
/* solve the system */
gauss_seidel (a, b, x, n);
/* output solution */
for (i = 0; i < n; i++) {
printf ("x[%d]=%f\n", i, x[i]);
}
printf("\n");
return 0;
私はが、我々は、コード内に関係していると思う部分を強調しました。ユーザーが[0] [1 sin関数を入力した後
Enter the dimension of the system to be solved: 5
Enter a[0][0] of the system matrix: 2
Enter a[0][1] of the system matrix: sin(3.14/4)
Enter a[0][2] of the system matrix:
Enter a[0][3] of the system matrix:
Enter a[0][4] of the system matrix:
Enter b[0] of the right-hand-side vector:
Enter a[1][0] of the system matrix:
Enter a[1][1] of the system matrix:
Enter a[1][2] of the system matrix:
Enter a[1][3] of the system matrix:
Enter a[1][4] of the system matrix:
Enter b[1] of the right-hand-side vector:
Enter a[2][0] of the system matrix:
Enter a[2][1] of the system matrix:
Enter a[2][2] of the system matrix:
Enter a[2][3] of the system matrix:
Enter a[2][4] of the system matrix:
Enter b[2] of the right-hand-side vector:
Enter a[3][0] of the system matrix:
Enter a[3][1] of the system matrix:
Enter a[3][2] of the system matrix:
Enter a[3][3] of the system matrix:
Enter a[3][4] of the system matrix:
Enter b[3] of the right-hand-side vector:
Enter a[4][0] of the system matrix:
Enter a[4][1] of the system matrix:
Enter a[4][2] of the system matrix:
Enter a[4][3] of the system matrix:
Enter a[4][4] of the system matrix:
Enter b[4] of the right-hand-side vector:
Found diagonal element equal to zero; rearrange equations; exiting. Program ended with exit code:
255
あなたが見ることができるように、プログラムが完全に反転します:ここでは誰かが入力として三角関数で何かを入力した場合に何が起こるかのサンプル出力です]。これは簡単に解決できるルーキーミスです(私はイントロクラスに入っているので慈悲を持っています)が、コードがなぜそれがどういう仕組みになっているのか、そしてなぜそれが求められないのかを理解するのは困っていますユーザからの入力を受け取り、グリッドの残りの部分をゼロに初期化します(私のコンバージェンス基準関数がトリガされるため、残りのコードはゼロに初期化されます)。
誰かが私にこれを説明できますか?
もっと重要なことは、ユーザーがsin(45)やsin(pi/4)のような値を入力できるようにコードを変更できる方法はありますか?
基本的には、入力を文字列(二重ではない)として受け取り、ユーザーが入力した場合に式を評価する必要があります。 C++はデフォルトでこれを行いません。 –
解析する前に入力を文字列として読み込みます。それが明確でない場合。 –
終了直前に 'if(a [i] [i] == 0.0)'のとき 'i'の値を表示できますか? 1つのセル 'a [0] [0]'だけが設定されているためです。 –