Related questions
#include <iostream>
using namespace std;
struct Triple
{
int row, col, value;
};
class Matrix;
class MatrixNode
{
friend class Matrix;
friend istream& operator>>(istream&, Matrix&);
private:
MatrixNode *down, *right;
bool head;
union
{
MatrixNode *next;
Triple triple;
};
MatrixNode(bool, Triple*);
};
MatrixNode::MatrixNode(bool b, Triple *t)
{
head = b;
if (b)
{
right = down = this;
}
else triple = *t;
};
class Matrix
{
friend istream& operator>>(istream&, Matrix&);
public:
~Matrix();
MatrixNode*
private:
MatrixNode *headnode;
};
Matrix::~Matrix()
{// Return all nodes to the av list, which is a chain linked
// via the right field.
// av is a static variable pointing to the first of the av list.
if (!headnode )
return; // no nodes to delete
MatrixNode *x = headnode->right;
headnode->right = av;
av = headnode; // return headnode
while (x != headnode) { // return nodes by rows
MatrixNode *y = x->right;
x->right = av;
av = y;
x = x->next; // next row
}
headnode = 0;
}
istream& operator>>(istream& is, Matrix& matrix)
{
// Read in a maxtix and set up its linked representation
Triple s;
is >> s.row >> s.col >> s.value; // matrix dimensions
int p = max(s.row, s.col);
// set up header node for list of header nodes
matrix.headnode = new MatrixNode(false, &s);
if (p == 0)
{
matrix.headnode->right = matrix.headnode;
return is; // for supporting "cin >> mi >> mj;"
}
// at least one row or column
MatrixNode **head = new MatrixNode* [p];
for (int i = 0; i < p; i++)
head[i] = new MatrixNode(true, 0);
// please continue on the next page
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) // end of current row
{
last->right = head[currentRow]; // close current row
currentRow = t.row;
last = head[currentRow];
} // end of if
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
} // end of for
// please continue on the next page
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 header nodes together
for (int i = 0; i < p; i++)
head[i]->next = head[i + 1];
head[p-1]->next = matrix.headnode;
matrix.headnode->right = head[0];
delete [] head;
return is;
}
int main()
{
Matrix m, n, k;
cin >> m >> n >>k;
cout << "Hello world!" << endl;
return 0;
}
Based on this class, do the following tasks.
(a) Write the C++ function, operator+(const Matrix& b) const, which returns the matrix *this + b.
(b) Write the C++ function, operator*(const Matrix& b) const, which returns the matrix *this * b.
(c) Write the C++ function, operator<<(), which outputs a sparse matrix as triples (i, j, aij).
(d) Write the C++ function, Transpose(), which transpose a sparse matrix.
(e) Write and test a copy constructor for sparse matrices. What is the computing time of your copy constructor?
Note:
I have tried to code it, but the overloaded operator>> has a bug, I think it's this line "head[t.col]->next = head[t.col]->next->down = last;". for illustration I attach two pictures.
Step by stepSolved in 2 steps
- plz, answer it urgently with an explanation.arrow_forwardnamespace CS3358_FA2022_A04_sequenceOfNum{template <class Item>class sequence{public:// TYPEDEFS and MEMBER SP2020typedef Item value_type;typedef std::size_t size_type;static const size_type CAPACITY = 10;// CONSTRUCTORsequence();// MODIFICATION MEMBER FUNCTIONSvoid start();void end();void advance();void move_back();void add(const value_type& entry);void remove_current();// CONSTANT MEMBER FUNCTIONSsize_type size() const;bool is_item() const;value_type current() const;private:value_type data[CAPACITY];size_type used;size_type current_index;bool is_item();};} error: invalid use of template-name 'CS3358_FA2022_A04_sequenceOfNum::sequence' without an argument list 120 | sequence::size_type sequence<Item>::size() const { | ^~~~~~~~arrow_forwardStatic data members and static member functions aren't precisely what I'm looking for.arrow_forward
- #include <iostream>using namespace std;class Player{private:int id;static int next_id;public:int getID() { return id; }Player() { id = next_id++; }};int Player::next_id = 1;int main(){Player p1;Player p2;Player p3;cout << p1.getID() << " ";cout << p2.getID() << " ";cout << p3.getID();return 0;} Run the program and give its output.arrow_forwardcomplex.h #pragma once #include <iostream> #include "imaginary.h" using namespace std; class Complex { private: int real; Imaginary imagine; public: //YOU: Implement all these functions Complex(); //Default constructor Complex(int new_real, Imaginary new_imagine); //Two parameter constructor Complex operator+(const Complex &rhs) const; Complex operator-(const Complex &rhs) const; Complex operator*(const Complex &rhs) const; bool operator==(const Complex &rhs) const; Complex operator^(const int &exponent) const; friend ostream& operator<<(ostream &lhs,const Complex& rhs); friend istream& operator>>(istream &lhs,Complex& rhs); }; complex.cc #include <iostream> #include "complex.h" using namespace std; //Class definition file for Complex //YOU: Fill in all of these functions //There are stubs (fake functions)...arrow_forwardWhat effect does the new operator have when constructing instances of structures?arrow_forward
- CONSTRUCTOR// IntSet()// Pre: (none)// Post: The invoking IntSet is initialized to an empty// IntSet (i.e., one containing no relevant elements).// CONSTANT MEMBER FUNCTIONS (ACCESSORS)// int size() const// Pre: (none)// Post: Number of elements in the invoking IntSet is returned.// bool isEmpty() const// Pre: (none)// Post: True is returned if the invoking IntSet has no relevant// relevant elements, otherwise false is returned.// bool contains(int anInt) const// Pre: (none)// Post: true is returned if the invoking IntSet has anInt as an// element, otherwise false is returned.// bool isSubsetOf(const IntSet& otherIntSet) const// Pre: (none)// Post: True is returned if all elements of the invoking IntSet// are also elements of otherIntSet, otherwise false is// returned.// By definition, true is returned if the invoking IntSet// is empty (i.e., an empty IntSet...arrow_forward#include <iostream>using namespace std;class Player{private:int id;static int next_id;public:int getID() { return id; }Player() { id = next_id++; }};int Player::next_id = 1;int main(){Player p1;Player p2;Player p3;cout << p1.getID() << " ";cout << p2.getID() << " ";cout << p3.getID();return 0;} Run the program and give its output.arrow_forwardC:/Users/r1821655/CLionProjects/untitled/sequence.cpp:48:5: error: return type specification for constructor invalidtemplate <class Item>class sequence{public:// TYPEDEFS and MEMBER SP2020typedef Item value_type;typedef std::size_t size_type;static const size_type CAPACITY = 10;// CONSTRUCTORsequence();// MODIFICATION MEMBER FUNCTIONSvoid start();void end();void advance();void move_back();void add(const value_type& entry);void remove_current();// CONSTANT MEMBER FUNCTIONSsize_type size() const;bool is_item() const;value_type current() const;private:value_type data[CAPACITY];size_type used;size_type current_index;};} 48 | void sequence<Item>::sequence() : used(0), current_index(0) { } | ^~~~ line 47 template<class Item> line 48 void sequence<Item>::sequence() : used(0), current_index(0) { }arrow_forward
- #include <iostream>using namespace std;class Player{private:int id;static int next_id;public:int getID() { return id; }Player() { id = next_id++; }};int Player::next_id = 1;int main(){Player p1;Player p2;Player p3;cout << p1.getID() << " ";cout << p2.getID() << " ";cout << p3.getID();return 0;} Run the program and give its output.arrow_forwardDesign a struct with following members: p_num_lots, int* type pa_lots, int [] type (dynamically allocated) Implement following methods: parameterized constructor which takes an int for num of lots copy constructor copy assignment operator destructorarrow_forwardclass IndexItem { public: virtual int count() = 0; virtual void display()= 0; };class Book : public IndexItem { private: string title; string author; public: Book(string title, string author): title(title), author(author){} virtual int count(){ return 1; } virtual void display(){ /* YOU DO NOT NEED TO IMPLEMENT THIS FUNCTION */ } };class Category: public IndexItem { private: /* fill in the private member variables for the Category class below */ ? int count; public: Category(string name, string code): name(name), code(code){} /* Implement the count function below. Consider the use of the function as depicted in main() */ ? /* Implement the add function which fills the category with contents below. Consider the use of the function as depicted in main() */ ? virtualvoiddisplay(){ /* YOU DO NOT NEED TO IMPLEMENT THIS FUNCTION */ } };arrow_forward
- Text book imageComputer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONText book imageComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceText book imageNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Text book imageConcepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningText book imagePrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationText book imageSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY