REDUCING A SQUARE MATRIX TO ITS UPPER TRAINGULAR FORM




#include "stdafx.h"
#include "iostream"
#define N 4
using namespace std;

void main(){
       int i, j, k;
       double **a;
       a = new double *[N + 1];
       for (i = 0; i <= N; i++)
             a[i] = new double[N + 1];
       cout << "Enter values for matrix A:" << endl;
       cout << "a11=" << endl;
       cin >> a[1][1];
       cout << "a12=" << endl;
       cin >> a[1][2];
       cout << "a13=" << endl;
       cin >> a[1][3];
       cout << "a14=" << endl;
       cin >> a[1][4];
       cout << "a21=" << endl;
       cin >> a[2][1];
       cout << "a22=" << endl;
       cin >> a[2][2];
       cout << "a23=" << endl;
       cin >> a[2][3];
       cout << "a24=" << endl;
       cin >> a[2][4];
       cout << "a31=" << endl;
       cin >> a[3][1];
       cout << "a32=" << endl;
       cin >> a[3][2];
       cout << "a33=" << endl;
       cin >> a[3][3];
       cout << "a34=" << endl;
       cin >> a[3][4];
       cout << "a41=" << endl;
       cin >> a[4][1];
       cout << "a42=" << endl;
       cin >> a[4][2];
       cout << "a43=" << endl;
       cin >> a[4][3];
       cout << "a44=" << endl;
       cin >> a[4][4];
       cout << "The matrix A["<<N<<"x"<< N <<"]:" << endl;
       for (i = 1; i <= N; i++){
             for (j = 1; j <= N; j++){
                    cout << a[i][j] << " ";
             }
             cout << endl;
       }
       double m;
       for (k = 1; k <= N - 1; k++)
             for (i = k + 1; i <= N; i++)
             {
                    m = a[i][k] / a[k][k];
                    for (j = 1; j <= N; j++)
                           a[i][j] -= m*a[k][j];
             }
       cout << "The result of A[" << N << "x" << N << "]:" << endl;
       for (i = 1; i <= N; i++)
       {
             for (j = 1; j <= N; j++){
                    cout << a[i][j] << " ";
             }
             cout << endl;
       }
       system("pause");
}


Share on Google Plus