Question: Language use is C++ Need to create a sparse vector class with * operator, such as Vector Vector::operator * (Vector& paramameter) A multiplication (*) operators

Language use is C++

Need to create a sparse vector class with * operator, such as Vector Vector::operator * (Vector& paramameter) A multiplication (*) operators returns element-wise multiplication of two vectors in another vector. Given two sparse vectors, A and B, and a result vector C, and C = A * B, then Language use is C++ Need to create a sparse vector class with where Sn notates nth items in the sparse vector. In the main() of you program, you need to demonstrate the * operator. Basically, create vector A and B with size 10. Add 4 non-zero items at random locations in A and add 7 non-zero items at random locations in B. Then C = A * B and show all elements of vector C.

Class looks something similar like this:

class Matrix

{

public:

Matrix(unsigned short row, unsigned short col); // Construction

bool add(unsigned short row, unsigned short col, int data); // Adds a new item to the matrix

void show(); // show all items of matrix

Matrix operator + (Matrix&);

link getfirst(int r);

private:

MatRow *Head; // A pointer to show array of the "item" pointer

unsigned short rowN; // how many row matrix has

unsigned short colN; // how many col matrix has

};

Here is an example of Add, but I need mutiply in my question.

Matrix Matrix::operator + (Matrix &param) // To make C = A + B

{

Matrix MC(rowN, colN);

for (int i = 0; i

{

link itA = Head[i].first;

link itB = param.getfirst(i);

while (itA != NULL || itB != NULL)

{

cout

if (itB == NULL)

{

MC.add(i, itA->index, itA->data);

itA = itA->next;

continue;

}

if (itA == NULL)

{

MC.add(i, itB->index, itB->data);

itB = itB->next;

continue;

}

if (itA->index index)

{

MC.add(i, itA->index, itA->data);

itA = itA->next;

}

else if (itA->index > itB->index)

{

MC.add(i, itB->index, itB->data);

itB = itB->next;

}

else

{

MC.add(i, itA->index, itB->data + itA->data);

itA = itA->next;

itB = itB->next;

}

}

}

return MC;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!