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;
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.