Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help with c++ program that adds two sparse matrices from two different input files. The results are printed on an output file. I am not

Help with c++ program that adds two sparse matrices from two different input files. The results are printed on an output file. I am not allowed to use a class, only a structure.

The file with the details is here

https://drive.google.com/file/d/1wNnNCwOVZah_TExWHF6dLKh6Dfrw6NhG/view?usp=sharing

This is what i want my program to do.

image text in transcribed

#Matrix A 2 2 1 1 1.12 2 2 2 Input 2 2 2 #Matrix B #Aliens are real 1 1 3 2 2 4 output 2 2 1 1 4.12 2 2 6

code so far: The second array is not reading values correctly

#include iostream // chegg deleted the less than and greater than for some reason

#include string

#include sstream

#include fstream

using namespace std;

struct element

{

int r;

int c;

double value;

};

void rowCol(ifstream &file, int &r, int &c)

{

string line = "";

while (getline(file, line))

{

if (line[0] == '#' || line.length() == 0)

continue;

istringstream fn(line);

fn >> r >> c;

break;

}

}

void rowcheck(int row1, int row2, int col1, int col2)

{

if (!(row1 == row2 && col1 == col2))

{

cout

system("pause");

exit(1);

}

if (row1

{

cout

system("pause");

exit(1);

}

}

void inputcheck(ifstream &file1, ifstream &file2)

{

if (!file1 && file2)

{

cout

system("pause");

exit(1);

}

if (file1 && !file2)

{

cout

system("pause");

exit(1);

}

if (!(file1 || file2))

{

cout

system("pause");

exit(1);

}

}

int main()

{

string f1, f2;

cout

cin >> f1;

cout

cin >> f2;

ifstream file1, file2;

file1.open(f1);

file2.open(f2);

inputcheck(file1, file2);

int size1, size2;

string line = "";

int row1, col1, row2, col2;

rowCol(file1, row1, col1);

rowCol(file2, row2, col2);

rowcheck(row1, row2, col1, col2);

size1 = row1 * col1;

size2 = row2 * col2;

element *m1 = new element[size1];

for (int x = 0; x

{

file1 >> m1[x].r >> m1[x].c >> m1[x].value;

--size1;

}

for (int x = 0; x

{

cout

}

cout

element *m2 = new element[size2];

for (int x = 0; x

{

while (getline(file2, line))

{

if (line[0] == '#' || line.length() == 0)

continue;

else

{

file2 >> m2[x].r >> m2[x].c >> m2[x].value;

--size2;

}

}

}

for (int x = 0; x

{

if (m2[x].r> 0)

cout

}

file1.close(); file2.close();

delete[] m1;

m1 = NULL;

delete[] m2;

m2 = NULL;

system("pause");

return 0;

}

Sparse Matrix Addition 2013 00-2 3+0 020 2034 Hints for the HW: 0003 struct element( 40 2 1 int r; int c; double value; M1 nput file1 3 4 11 4 132 141 23-2 2 43 M2 Input file 2 3 4 11-2 1 31 143 232 M3 element* m1-new element[size1]; 3 4 11 2 1 33 144 2 43 element*m2-new element[size2]; Sparse Matrix Addition 2013 00-2 3+0 020 2034 Hints for the HW: 0003 struct element( 40 2 1 int r; int c; double value; M1 nput file1 3 4 11 4 132 141 23-2 2 43 M2 Input file 2 3 4 11-2 1 31 143 232 M3 element* m1-new element[size1]; 3 4 11 2 1 33 144 2 43 element*m2-new element[size2]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

ISBN: 1461368332, 978-1461368335

More Books

Students also viewed these Databases questions