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
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 ¶m) // 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
Get step-by-step solutions from verified subject matter experts
