1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include<iostream>
using namespace std;
int main()
{
const int SIZE = 4;
double a[SIZE][SIZE] = { { 4, -1, 0, 0 }, { -1, 4, -1, 0 }, { 0, -1, 4, -1 }, { 0, 0, -1, 4 } };
double b[SIZE] = { 2, 4, 6, 13 };
double m[SIZE] = { 0 };
int p = SIZE, q;
cout << "Enter the number of iterations: "; cin >> q;
while ( q-- )
{
for ( int i = 0; i < p; i++ )
{
m[i] = b[i] / a[i][i];
for ( int j = 0; j < p; j++ ) if (j != i) m[i] -= ( a[i][j] / a[i][i] ) * m[j];
cout << m[i] << '\t';
}
cout << '\n';
}
}
| |