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

I am writing a program in c++. this is the skeleton I have so far: #include <ios

ID: 3823168 • Letter: I

Question

I am writing a program in c++.

this is the skeleton I have so far:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
int n1, n2;

ifstream inFile1, inFile2;
inFile1.open ("mat1.txt");
inFile2.open ("mat2.txt");
  
// cout<<n1+1;

int x = n1;
int i=0;
int size = 0;
for(i=1;i<=x;i++){
  
size = size + i;
}
// cout<<size;

int* file1contents = new int[n1*n1];
for(int i=0; i<n1; ++i) {
for(int j=0; j<n1; ++j) {
inFile1 >> file1contents[i*n1 + j];
}
}

inFile2 >> n2;
int* file2contents = new int[n2*n2];
for(int i=0; i<n2; ++i) {
for(int j=0; j<n2; ++j) {
inFile2 >> file2contents[i*n2 + j];
}
}

// Print file contents to check it worked
cout << "File 1, size: " << n1 << ", contents:" << endl;
for(int i=0; i<n1; ++i) {
for(int j=0; j<n1; ++j) {
cout << file1contents[i*n1 + j] << ",";
}
cout << " ";
}
cout << endl;

cout << "File 2, size: " << n2 << ", contents:" << endl;
for(int i=0; i<n2; ++i) {
for(int j=0; j<n2; ++j) {
cout << file2contents[i*n2 + j] << ",";
}
cout << " ";
}
cout << endl;

delete [] file1contents;
delete [] file2contents;

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

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, triMatMult.cpp,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 NXN All values will be integers File format o N (dimension of the matrix) o number 1 o number 2 o number 3 For help on matrix multiplication see h urplemath.com/modules/mtrkmult.htm Restrictions: You cannot expand the compressed matrices to do the multiplication. If you do this you will receive no credit for this portion of the assignment. Again the whole point is to save Space

Explanation / Answer

Please find the exact solution to your query-

#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[] )
{
int n1, n2;
ifstream inFile1, inFile2;
inFile1.open (argv[1]);
inFile2.open (argv[2]);
  
// cout<<n1+1;
int x = n1;
int i=0;
int size = 0;
for(i=1;i<=x;i++){
  
size = size + i;
}
// cout<<size;
inFile1 >> n1;
int* file1contents = new int[n1*n1];
for(int i=0; i<n1; ++i) {
for(int j=i; j<n1; ++j) {
inFile1 >> file1contents[i*n1 + j];
}
}
inFile2 >> n2;
int* file2contents = new int[n2*n2];
for(int i=0; i<n2; ++i) {
for(int j=i; j<n2; ++j) {
inFile2 >> file2contents[i*n2 + j];
}
}
// Print file contents to check it worked
cout << "File 1, size: " << n1 << ", contents:" << endl;
for(int i=0; i<n1; ++i) {
for(int j=0; j<n1; ++j) {
cout << file1contents[i*n1 + j] << ",";
}
cout << " ";
}
cout << endl;
cout << "File 2, size: " << n2 << ", contents:" << endl;
for(int i=0; i<n2; ++i) {
for(int j=0; j<n2; ++j) {
cout << file2contents[i*n2 + j] << ",";
}
cout << " ";
}
cout << endl;
int* file3contents = new int[n1*n1];
for(int i = 0; i < n1; ++i)
for(int j = 0; j < n2; ++j)
for(int k = 0; k < n1; ++k)
{
file3contents[i*n1 +j] += file1contents[i*n1 + k] * file2contents[k* n2 + j];
}
cout << "File 3, size: " << n1 << ", contents:" << endl;
for(int i=0; i<n1; ++i) {
for(int j=i; j<n1; ++j) {
cout << file3contents[i*n1 + j] << " ";
}
}
delete [] file1contents;
delete [] file2contents;
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