answersLogoWhite

0

#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;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

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


Write a program that read phrase and print the number of lower-case letter in it using function of counting?

write a program that reads a phrase and prints the number of lowercase latters in it using a function for counting? in C program


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


How do you implement Hadamard matrix in c?

A Hadamard Matrix is a square matrix composed of 1 or -1. Using a square matrix system the hadamard matrix could be created


Can somebody give me the program to find simple interest using friend function in c plus plus?

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.

Related Questions

Write a c program for matrix addition using function?

#include&lt;


How do you rotate figure 360 degrees using matrix?

Multiply it by the identity matrix.


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.


C plus plus program -matrix multiplication using class?

for(int i=0;i


How can Mathematica be used to compute and normalize eigenvectors of a given matrix?

Mathematica can be used to compute and normalize eigenvectors of a given matrix by using the Eigensystem function to find the eigenvectors and eigenvalues of the matrix. Then, the Normalize function can be applied to normalize the eigenvectors.


C program for matrix multiplication using dynamic memory alllocation?

Poor boy


What is the use of function in c?

using function we can call the function in the program any where. by using functions we can reduce the code redundancy


Write a C program using dynamic memory allocation to find the sum of elements of a matrix?

Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.


How do you write c program to perform sum of elements of matrix using pointers with functions?

i cant write


Write a program using iostreams to take as input two multi-dimensional arrays and print their sum as output Matrix Addition in Matrix format?

http://www.assignmentsclub.com/


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");


What is the syntax for calculating eigenvalues and eigenvectors in MATLAB in a specific order using the 'eig' function?

To calculate eigenvalues and eigenvectors in MATLAB using the 'eig' function, the syntax is as follows: eigenvectors, eigenvalues eig(matrix) This command will return the eigenvectors and eigenvalues of the input matrix in a specific order.