#include<iostream>
#include<vector>
#include<exception>
#include<assert.h>
#include<time.h>
class matrix
{
private:
size_t m_rows;
size_t m_cols;
std::vector<std::vector<int>> m_data;
public:
matrix (const size_t rows, const size_t cols);
size_t rows() const { return m_rows; }
size_t cols() const { return m_cols; }
std::vector<int>& operator[] (const size_t index) { return m_data[index]; }
const std::vector<int>& operator[] (const size_t index) const { return m_data[index]; }
std::vector<int>& row (const size_t index) { return m_data[index]; }
const std::vector<int>& row (const size_t index) const { return m_data[index]; }
std::vector<int> col (const size_t index);
const std::vector<int> col (const size_t index) const;
void randomise();
};
void matrix::randomise()
{
for (size_t r=0; r<rows(); ++r)
{
for (size_t c=0; c<cols(); ++c)
{
row(r)[c] = rand() % 9 + 1;
}
}
}
std::ostream& operator<< (std::ostream& os, const matrix& m)
{
for (size_t row=0; row<m.rows(); ++row)
{
for (size_t col=0; col<m.cols(); ++col)
{
os << m[row][col] << '\t';
}
os << std::endl;
}
os << std::endl;
return os;
}
matrix::matrix (const size_t rows, const size_t cols)
: m_rows (rows)
, m_cols (cols)
, m_data ()
{
if (!m_cols !m_rows)
throw std::out_of_range ("matrix: dimensions must be non-zero!");
for (size_t row=0; row<m_rows; ++row)
m_data.push_back (std::vector<int>(m_cols));
}
std::vector<int> matrix::col (const size_t index)
{
std::vector<int> column(m_rows);
for (size_t r=0; r<m_rows; ++r)
column[r] = row(r)[index];
return column;
}
const std::vector<int> matrix::col (const size_t index) const
{
std::vector<int> column(m_rows);
for (size_t r=0; r<m_rows; ++r)
column[r] = row(r)[index];
return column;
}
matrix operator* (const matrix a, const matrix b)
{
if (a.cols() != b.rows())
throw std::out_of_range("matrix operator* : row/column mismatch!");
matrix result (a.rows(), b.cols());
for (size_t r=0; r<a.rows(); ++r)
{
for (size_t c=0; c<b.cols(); ++c)
{
result[r][c]=0;
const std::vector<int>& Row = a[r];
const std::vector<int> Col = b.col(c);
assert (Row.size() == Col.size());
for (size_t index=0; index<Row.size(); ++index)
result[r][c] += (Row[index]*Col[index]);
}
}
return result;
}
int main()
{
srand((unsigned)time(nullptr));
matrix a (3,4);
a.randomise();
std::cout << "Matrix A:\n\n" << a << std::endl;
matrix b (4,5);
b.randomise();
std::cout << "Matrix B:\n\n" << b << std::endl;
std::cout << "Matrix A * B:\n\n" << a * b << std::endl;
}
Chat with our AI personalities
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
write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program
This is not a question.
A Hadamard Matrix is a square matrix composed of 1 or -1. Using a square matrix system the hadamard matrix could be created
Here is an example program: class obj{ public: float p,n,r,si; friend void calc( obj temp); }; void calc( obj temp){ si = (p*n*r)/100; } The initialization and function calling is essential.