我正在编写一个C ++代码,该代码使用Gauss-Seidel方法来求解方程组(但你不应该知道这是什么来回答我的问题)。这主要涉及用户将一系列值输入到2D阵列中。我的问题发生在值为sin(45)之类的东西时,或者更确切地说是3.14,2.120947或3之类的任何数字等等......这里是代码主循环的片段
/* 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;
我在代码中突出了我认为我们关注的部分。以下是有人输入带有trig函数作为输入的内容时输出的示例输出:
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
正如您所看到的,在用户输入[0] [1]的sin函数后,程序完全翻转。这可能是一个很容易解决的新手错误(我在一个介绍课程中如此怜悯),但是我很难理解为什么这些代码确实会出现问题。它的工作方式,以及它为什么不要求用户提供进一步的输入,而是将网格的其余部分初始化为零(我知道其余的代码初始化为零,因为我的收敛标准函数触发器,最后退出程序)。
有人可以向我解释一下吗?
更重要的是,有什么方法可以改变我的代码以允许用户输入像sin(45)或sin(pi / 4)这样的值?
答案 0 :(得分:1)
你可以把它作为一个字符串读入每个值,然后你可以用它做任何解析/评估。现在,你要求读一个双,但不要输入一个。