Matrices

Data 1
P=[█(3 2 0 1@4 0 1 2@3 0 2 1@9 2 3 1)]
and
Q=[█(3 0 2 -1@1 2 0 -2@4 0 6 -3@5 0 2 0)]

Data 2
P=[■(5&-2&3@4&-1&-5@6&7&9)]
and
Q=[■(1&3&2@4&1&3@2&5&2)]


Create two different input files for Data 1 and Data 2.

Then, write a single C++ program to:

a) read (and display) the matrices P and Q from each of the input file,

b) find and display the upper triangular matrix for both P and Q,

c) find and display the determinant of P,

d) find and display the inverse of Q,

e) compute and display P+Q,

f) compute and display P-Q,

g) compute and display P×Q.

#include <iostream>
#include <fstream>
#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];
ifstream InFile;
InFile.open ("P.in", ios::in);
for (i=1; i<=N; i++)
for (j=1; j<=N; j++)
InFile >> a[i][j];
InFile.close();

double A[N+1][N+1];
cout.setf (ios::fixed);
cout.precision(5);

cout << "Input matrix A: " << endl;
ifstream InFile ("P.in");
cout << "Matrix A:" <<endl;
for (i=1; i<=N; i++)
{
for (j=1; j<=N; j++)
{
InFile >> A[i][j];
cout << A[i][j];
}
cout << a[i][j] << "\t";
cout << endl;
}
InFile.close();

//row operations
double Product, 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 << "Reduced Matrix A after row operations:" <<endl;
cout << endl << "Matrix U:" << endl;
for (i=1; i<=N; i++)
{
for (j=1; j<=N; j++)
cout << a[i][j] << "\t";
cout <<endl;
}

Product=1;
for (i=1; i<=N; i++)
Product *=A[i][j];

//display results
cout << endl << "det(A)=" << Product << endl;
cin.get();
}
Topic archived. No new replies allowed.