Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- int main() {
- float A[3][3], C[3], X[3];
- float X1 = 0, X2 = 0, X3 = 0, TOL;
- int i, c = 0, ITR;
- cout << "Enter the matrix of coefficients:\n";
- for (i = 0; i < 3; i++)
- for (int j = 0; j < 3; j++)
- cin >> A[i][j];
- cout << "Enter the vector of constants:\n";
- for (i = 0; i < 3; i++)
- cin >> C[i];
- cout << "Enter the number of iterations and stopping tolerance respectively:\t";
- cin >> ITR >> TOL;
- for (i = 0; i < ITR; i++) {
- X[0] = (1 / A[0][0]) * (C[0] - A[0][1] * X2 - A[0][2] * X3);
- X[1] = (1 / A[1][1]) * (C[1] - A[1][0] * X1 - A[1][2] * X3);
- X[2] = (1 / A[2][2]) * (C[2] - A[2][0] * X1 - A[2][1] * X2);
- if ((abs(X[0] - X1) < TOL) && (abs(X[1] - X2) < TOL) && (abs(X[2] - X3) < TOL)) {
- printf("\n%.3f\tDiff: %.3f\n%.3f\tDiff: %.3f\n%.3f\tDiff: %.3f\n\n", X[0], abs(X[0] - X1), X[1], abs(X[1] - X2), X[2], abs(X[2] - X3));
- cout << endl << endl << "Number of iterations:" << i << endl;
- c = 1;
- break;
- }
- X1 = X[0]; X2 = X[1]; X3 = X[2];
- }
- if (c == 0)
- printf("\n%.3f\tDiff: %.3f\n%.3f\tDiff: %.3f\n%.3f\tDiff: %.3f\n\n", X[0], abs(X[0] - X1), X[1], abs(X[1] - X2), X[2], abs(X[2] - X3));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement