Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

An upper triangular matrix is a special type of matrix where all the values belo

ID: 3784829 • Letter: A

Question

An upper triangular matrix is a special type of matrix where all the values below the main diagonal are 0. In order to save space we can store this matrix without the zeros. For example 1 2 3 0 4 5 0 0 6 Would be stored as 1 2 3 4 5 6 We would also like to be able to work with these matrices in their compressed format, again to save space. Write a C++ program called that accepts as arguments two files that contain these compressed upper triangular matrices. The program should multiply the two matrices together and then display the resulting compressed matrix in its compressed form.

• The names of the files will be given on the command line

• All matrices will be square, ie N X N

• All values will be integers

• File format:

N (dimension of the matrix)

number1 number2 number3 ...

• For help on matrix multiplication see http://www.purplemath.com/modules/mtrxmult.htm.

• Restrictions: You cannot expand the compressed matrices to do the multiplication. Again the whole point is to save space.

• In the examples on the next page the values are shown on 1 line to save space

Cat mat1.txt 4 1 2 3 17 4 51 25 6 31 9

cat mat2.txt 4 25 73 -4 -17 -99 81 -88 11 12 10

./triMatMult.out mat1.txt mat2.txt

25 -125 191 13 -396 885 510 66 382 90

This is equivalent to doing C = A * B where:

A = 1 2 3 17 0 4 51 25 0 0 6 31 0 0 0 9

B = 25 73 -4 -17 0 -99 81 -88 0 0 11 12 0 0 0 10

C = 25 -125 191 13 0 -396 885 510 0 0 66 382 0 0 0 90

Explanation / Answer

#include<iostream>
#include <fstream>

using namespace std;

int main()
{

int a[5][5],b[5][5],c[5][5],n,i,j,k,s,r;

ifstream inFile1,inFile2;
inFile1.open ("mat1.txt");
inFile2.open ("mat2.txt");

inFile1 >> n;
for(i=0;i<n;++i)
for(j=i;j<n;++j)
inFile1 >> a[i][j];

inFile2 >> n;
for(i=0;i<n;++i)
for(j=i;j<n;++j)
inFile2 >> b[i][j];

for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
c[i][j]=0;

for(s=i,r=i;r<=j;r++)
{   
c[i][j]=c[i][j]+(a[i][s]*b[r][j]);
s++;
}

}
}

for(i=0;i<n;++i)
{
for(j=i;j<n;++j)
cout<<c[i][j]<<" ";
}

inFile1.close();
inFile2.close();
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote