1

I'm writing a C++ code that uses the Gauss-Seidel method to solve for a system of equations (but you shouldn't need to know what that is to answer my question). This essentially involves the user inputting a series of values into a 2D array. My problem occurs when the value is something like sin(45), or rather anything that isn't a number such as 3.14, 2.120947, or 3, etc... Here's a snippet of the main loop of the code.

/* 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;

I highlighted the part that I think we are concerned with within the code. Here's a sample output of what happens if someone enters something with a trig function as an input:

 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

As you can see, the program completely flips out after the user enters the sin function for a[0][1]. This is likely a rookie mistake that's easily fixable (I'm in an intro class so have mercy), but I'm having trouble understanding why exactly the code "freaks out" the way it does, and why it doesn't ask for further input from the user and instead initializes the rest of the grid to be zero (I know for a fact that the rest of the code is initialized to be zero because my convergence criterion function triggers, exiting the program at the end).

Could someone explain this to me?

More importantly, is there any way I can change my code to allow the user to enter a value such like sin(45) or sin(pi/4)?

wally
  • 10,717
  • 5
  • 39
  • 72

1 Answers1

1

You could have it read in each value as a string, and then you can do whatever parsing/evaluating you want with that. Right now, you are asking to read a double, but not entering one.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101