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

Still curious? Ask our experts.

Chat with our AI personalities

FranFran
I've made my fair share of mistakes, and if I can help you avoid a few, I'd sure like to try.
Chat with Fran
CoachCoach
Success isn't just about winning—it's about vision, patience, and playing the long game.
Chat with Coach
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
More answers

#include <stdio.h>

int main (void) { printf ("6*8=%d\n", 6*8); return 0; }

User Avatar

Wiki User

16y ago
User Avatar

int x, y, z;

x= 3;

y= 4;

z= x*y;

User Avatar

Wiki User

13y ago
User Avatar

Add your answer:

Earn +20 pts
Q: Using function program to multiply two matrix in c plus plus?
Write your answer...
Submit
Still have questions?
magnify glass
imp
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.