#include #include using namespace std; struct Triple { int value, row, col; }; ostream& operator<<(ostream& os, Triple& t) { os << "(" << t.row <<    ", " << t.col <<    ", " << t.value << ")" <>(istream&, Matrix&); // for reading in a matrix friend ostream& operator<<(ostream&, Matrix&); private: MatrixNode *down, *right; bool head; union { // anonymous union MatrixNode *next; Triple triple; }; MatrixNode(bool, Triple*); // constructor }; MatrixNode::MatrixNode(bool b, Triple* t) { head = b; if (b) {right = next = this;} // row/column head node else triple = *t; // head node for list of headnodes OR element node } typedef MatrixNode* MatrixNodePtr; class Matrix { Matrix operator+ (const Matrix & b) const; Matrix operator*(const Matrix& b) const; Matrix Transpose()const; friend istream& operator>>(istream&, Matrix& m); friend ostream& operator<<(ostream&, Matrix& m); private:    MatrixNode *headnode; public:       ~Matrix(); }; istream& operator>>(istream& is, Matrix& matrix) // Read in a matrix and set up its linked representation. // An auxiliary array head is used. { Triple s; int p; is >> s.row >> s.col >> s.value; // matrix dimensions if (s.row > s.col) p = s.row; else p = s.col; // set up headnode for list of head nodes. matrix.headnode = new MatrixNode(false, &s); if (p == 0) { matrix.headnode->right = matrix.headnode; return is;} // at least one row or column MatrixNodePtr *head = new MatrixNodePtr[p]; // initialize head nodes for (int i = 0; i < p; i++) head[i] = new MatrixNode(true, 0); int CurrentRow = 0; MatrixNode *last = head[0]; // last node in current row for (int i = 0; i < s.value; i++) // input triples { Triple t; is >> t.row >> t.col >> t.value; if (t.row > CurrentRow) { // close current row    last->right = head[CurrentRow];    CurrentRow = t.row;    last = head[CurrentRow]; } last = last->right = new MatrixNode(false, &t); // link new node into row list head[t.col]->next = head[t.col]->next->down = last; // link into column list } last->right = head[CurrentRow]; // close last row for (int i = 0; i < s.col; i++) head[i]->next->down = head[i]; // close all column lists // link the head nodes together for (int i = 0; i < p-1 ; i++) head[i]->next = head[i+1]; head[p-1]->next = matrix.headnode; matrix.headnode->right = head[0]; delete [] head; return is; } ostream& operator<<(ostream& os, Matrix& m) { MatrixNode *x = m.headnode; if (!x) return os << "Matrix destroyed" << endl; x = x->right; os << endl; while (x != m.headnode) {    for (MatrixNode *y = x->right; y != x ; y = y->right)    os << y->triple;    x = x->next; } x = m.headnode; x = x->right; os << endl; return os; } MatrixNode *av = 0; Matrix::~Matrix() { if (!headnode) return; // no nodes to dispose MatrixNode *x = headnode->right, *y; headnode->right = av; av = headnode; // return @headnode@ while (x != headnode) { // erase by rows y = x->right; x->right = av; av = y; x = x->next; // next row } headnode = 0; } I need other function implement 1. Matrix operator+ (const Matrix & b) const; 2. Matrix operator*(const Matrix& b) const; 3. Matrix Transpose()const; 4. Write and test a copy constructor for sparse matrices and compute time of copy constructor. please  write c++ code that fit in the template abov

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

#include <iostream>

#include <fstream>

using namespace std;

struct Triple

{

int value, row, col;

};

ostream& operator<<(ostream& os, Triple& t)

{

os << "(" << t.row <<

   ", " << t.col <<

   ", " << t.value << ")" <<endl;

return os;

}

class Matrix; // forward declaration

class MatrixNode

{

friend class Matrix;

friend istream& operator>>(istream&, Matrix&); // for reading in a matrix

friend ostream& operator<<(ostream&, Matrix&);

private:

MatrixNode *down, *right;

bool head;

union { // anonymous union

MatrixNode *next;

Triple triple;

};

MatrixNode(bool, Triple*); // constructor

};

MatrixNode::MatrixNode(bool b, Triple* t)

{

head = b;

if (b) {right = next = this;} // row/column head node

else triple = *t; // head node for list of headnodes OR element node

}

typedef MatrixNode* MatrixNodePtr;

class Matrix {

Matrix operator+ (const Matrix & b) const;

Matrix operator*(const Matrix& b) const;

Matrix Transpose()const;

friend istream& operator>>(istream&, Matrix& m);

friend ostream& operator<<(ostream&, Matrix& m);

private:

   MatrixNode *headnode;

public:

  

   ~Matrix();

};

istream& operator>>(istream& is, Matrix& matrix)

// Read in a matrix and set up its linked representation.

// An auxiliary array head is used.

{

Triple s; int p;

is >> s.row >> s.col >> s.value; // matrix dimensions

if (s.row > s.col) p = s.row; else p = s.col;

// set up headnode for list of head nodes.

matrix.headnode = new MatrixNode(false, &s);

if (p == 0) { matrix.headnode->right = matrix.headnode; return is;}

// at least one row or column

MatrixNodePtr *head = new MatrixNodePtr[p]; // initialize head nodes

for (int i = 0; i < p; i++)

head[i] = new MatrixNode(true, 0);

int CurrentRow = 0; MatrixNode *last = head[0]; // last node in current row

for (int i = 0; i < s.value; i++) // input triples

{

Triple t;

is >> t.row >> t.col >> t.value;

if (t.row > CurrentRow) { // close current row

   last->right = head[CurrentRow];

   CurrentRow = t.row;

   last = head[CurrentRow];

}

last = last->right = new MatrixNode(false, &t); // link new node into row list

head[t.col]->next = head[t.col]->next->down = last; // link into column list

}

last->right = head[CurrentRow]; // close last row

for (int i = 0; i < s.col; i++) head[i]->next->down = head[i]; // close all column lists

// link the head nodes together

for (int i = 0; i < p-1 ; i++) head[i]->next = head[i+1];

head[p-1]->next = matrix.headnode;

matrix.headnode->right = head[0];

delete [] head;

return is;

}

ostream& operator<<(ostream& os, Matrix& m)

{

MatrixNode *x = m.headnode;

if (!x) return os << "Matrix destroyed" << endl;

x = x->right;

os << endl;

while (x != m.headnode)

{

   for (MatrixNode *y = x->right; y != x ; y = y->right)

   os << y->triple;

   x = x->next;

}

x = m.headnode;

x = x->right;

os << endl;

return os;

}

MatrixNode *av = 0;

Matrix::~Matrix()

{

if (!headnode) return; // no nodes to dispose

MatrixNode *x = headnode->right, *y;

headnode->right = av; av = headnode; // return @headnode@

while (x != headnode) { // erase by rows

y = x->right;

x->right = av;

av = y;

x = x->next; // next row

}

headnode = 0;

}

I need other function implement

1. Matrix operator+ (const Matrix & b) const;

2. Matrix operator*(const Matrix& b) const;

3. Matrix Transpose()const;

4. Write and test a copy constructor for sparse matrices and compute time of copy constructor.

please  write c++ code that fit in the template above.

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education