Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- void main(){
- int n, i, j, count = 0;
- float **a, *b, *x, *tmp_x, exp, e = 0.0001;
- cout << "Matrix Size\n";
- cout << "n = ";
- cin >> n;
- a = new float*[n];
- for (i = 0; i < n; i++){
- a[i] = new float[n];
- }
- b = new float[n];
- x = new float[n];
- tmp_x = new float[n];
- cout << "Write Matrix\n";
- for (i = 0; i < n; i++){
- for (j = 0; j < n; j++){
- cin >> a[i][j];
- }
- }
- cout << "Free elements\n";
- for (i = 0; i < n; i++){
- cin >> b[i];;
- x[i] = 0;
- }
- do{
- count++;
- for (i = 0; i < n; i++){
- tmp_x[i] = 0.0;
- for (j = 0; j < n; j++){
- if (i != j){
- tmp_x[i] = tmp_x[i] + (a[i][j] * x[j]);
- }
- }
- tmp_x[i] = (b[i] - tmp_x[i]) / a[i][i];
- }
- exp = 0;
- for (i = 0; i < n; i++){
- if (fabs(x[i] - tmp_x[i]) > exp){
- exp = fabs(x[i] - tmp_x[i]);
- }
- x[i] = tmp_x[i];
- }
- } while (exp > e);
- delete [] tmp_x;
- for (i = 0; i < n; i++){
- delete a[i];
- }
- delete [] a;
- delete [] b;
- printf("Answer\n");
- for (i = 0; i < n; i++){
- printf("x[%d] = %.6f\n", i + 1, x[i]);
- }
- cout << "Number of iterations " << count << endl;
- delete[] x;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement