answersLogoWhite

0

#include<iostream>

#include<iomanip>

#include<vector>

class matrix

{

private:

// a vector of vectors

std::vector< std::vector< int >> m_vect;

public:

// default constructor

matrix(unsigned rows, unsigned columns)

{

// rows and columns must be non-zero

if (!rows !columns)

{

throw;

}

m_vect.resize (rows);

for (unsigned row=0; row<rows; ++row)

{

m_vect[row].resize (columns);

for (unsigned column=0; column<columns; ++column)

{

m_vect[row][column] = 0;

}

}

}

// copy constructor

matrix(const matrix& copy)

{

m_vect.resize (copy.rows());

for (unsigned row=0; row<copy.rows(); ++row)

{

m_vect[row] = copy.m_vect[row];

}

}

// assignment operator (uses copy/swap paradigm)

matrix operator= (const matrix copy)

{

// note that copy was passed by value and was therefore copy-constructed

// so no need to test for self-references (which should be rare anyway)

m_vect.clear();

m_vect.resize (copy.rows());

for (unsigned row=0; row<copy.m_vect.size(); ++row)

m_vect[row] = copy.m_vect[row];

}

// allows vector to be used just as you would a 2D array (const and non-const versions)

const std::vector< int >& operator[] (unsigned row) const { return m_vect[row]; }

std::vector< int >& operator[] (unsigned row) { return m_vect[row]; }

// product operator overload

matrix operator* (const matrix& rhs) const;

// read-only accessors to return dimensions

const unsigned rows() const { return m_vect.size(); }

const unsigned columns() const { return m_vect[0].size(); }

};

// implementation of product operator overload

matrix matrix::operator* (const matrix& rhs) const

{

// ensure columns and rows match

if (columns() != rhs.rows())

{

throw;

}

// instantiate matrix of required size

matrix product (rows(), rhs.columns());

// calculate elements using dot product

for (unsigned x=0; x<product.rows(); ++x)

{

for (unsigned y=0; y<product.columns(); ++y)

{

for (unsigned z=0; z<columns(); ++z)

{

product[x][y] += (*this)[x][z] * rhs[z][y];

}

}

}

return product;

}

// output stream insertion operator overload

std::ostream& operator<< (std::ostream& os, matrix& mx)

{

for (unsigned row=0; row<mx.rows(); ++row)

{

for (unsigned column=0; column<mx.columns(); ++column)

{

os << std::setw (10) << mx[row][column];

}

os << std::endl;

}

return os;

}

int main()

{

matrix A(2,3);

matrix B(3,4);

int value=0, row, column;

// initialise matrix A (incremental values)

for (row=0; row<A.rows(); ++row)

{

for (column=0; column<A.columns(); ++column)

{

A[row][column] = ++value;

}

}

std::cout << "Matrix A:\n\n" << A << std::endl;

// initialise matrix B (incremental values)

for (row=0; row<B.rows(); ++row)

{

for (column=0; column<B.columns(); ++column)

{

B[row][column] = ++value;

}

}

std::cout << "Matrix B:\n\n" << B << std::endl;

// calculate product of matrices

matrix product = A * B;

std::cout << "Product (A x B):\n\n" << product << std::endl;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Write a C program to find sum of 3 matrices?

matrix


How do you find the rank of product of two matrices?

just make the matrices upper triangular by making the values below the digonal zero,and then find how many minors can be calcuted.......


Write a program to find the product of two numbers using Halving and Doubling method?

i need this answer


Write a c program using dynamic memory allocation function calloc to find sum of two matrices and also print the resultant matrix?

printf("%s",per&gt;50?:"pass",per&lt;50?:"fail");


Write a program in java to find factorial of a number?

I suggest to use a for loop, more or less like this (assuming the parameter is "n"): product = 1; for (int i = 1; i &lt;= n; i++) { product *= i; }


How do you write Square program using vb?

write a vb program to find the magic square


How do you write socket program in c?

For first find an example program.


How do you write a C program to find the adjoint of a matrix?

To write a C program to find the adjoint of a matrix, first, you need to create a function to calculate the cofactor of each element in the matrix. Then, construct the adjoint by transposing the cofactor matrix. The program should read the matrix size and elements from user input, compute the cofactors using nested loops, and finally display the adjoint matrix by transposing the cofactor matrix. Make sure to handle memory allocation for dynamic matrices if needed.


Write a program to find the grade obtained by the students of a class using structure?

Write a program to find the grade obtained by the students of a class


What are the application of matrices in field of engineering?

Matrices have a wider application in engineering. Many problems can be transformed in to simultaneous equation and their solution can easily be find with the help of matrices.


Write a c program to find eigenvalue of a matrix?

Yes, do write. That's what you always have to do when you have got a homework-program.


What is matrix programming in C programming?

C Examples on Matrix OperationsA matrix is a rectangular array of numbers or symbols arranged in rows and columns. The following section contains a list of C programs which perform the operations of Addition, Subtraction and Multiplication on the 2 matrices. The section also deals with evaluating the transpose of a given matrix. The transpose of a matrix is the interchange of rows and columns.The section also has programs on finding the trace of 2 matrices, calculating the sum and difference of two matrices. It also has a C program which is used to perform multiplication of a matrix using recursion.C Program to Calculate the Addition or Subtraction & Trace of 2 MatricesC Program to Find the Transpose of a given MatrixC Program to Compute the Product of Two MatricesC Program to Calculate the Sum & Difference of the MatricesC Program to Perform Matrix Multiplication using Recursion