answersLogoWhite

0


Best Answer

#include<iostream>

#include<algorithm>

#include<cassert>

unsigned multiply_recursive (unsigned a, unsigned b)

{

if (!a !b) return 0; // Note: not(a) or not (b)

if (a==1) return b;

if (b==1) return a;

return a + multiply_recursive (a, --b);

}

unsigned multiply_iterative (unsigned a, unsigned b)

{

int product = 0;

int x = std::min (a, b);

int y = std::max (b, a);

while (x--)

product += y;

return product;

}

int main()

{

unsigned x, y;

// Test all combinations with 0.

x = multiply_recursive (0, 0);

assert (x==0);

x = multiply_iterative (0, 0);

assert (x==0);

x = multiply_recursive (0, 1);

assert (x==0);

x = multiply_iterative (0, 1);

assert (x==0);

x = multiply_recursive (1, 0);

assert (x==0);

x = multiply_iterative (1, 0);

assert (x==0);

// Test non-zero values with 1.

x = multiply_recursive (1, 42); // inefficient: lowest value must be on right (fewer recursions).

y = multiply_iterative (1, 42);

assert (x==42 && y==42);

x = multiply_recursive (42, 1);

y = multiply_iterative (42, 1);

assert (x==42 && y==42);

// Test other non-zero values are commutative.

x = multiply_recursive (24, 42); // inefficient: lowest value must be on right (fewer recursions).

y = multiply_iterative (24, 42);

assert (x==1008 && y==1008);

x = multiply_recursive (42, 24);

y = multiply_iterative (42, 24);

assert (x==1008 && y==1008);

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

#include<random>

template<const size_t R, const size_t C>

class Matrix

{

public:

using row_type = int[C];

private: // attributes

int m_data[R][C];

public: // construction/assignment

Matrix ();

Matrix (const Matrix& source);

Matrix (Matrix&& source);

Matrix& operator= (const Matrix<R,C>& source);

Matrix& operator= (Matrix<R,C>&& source);

~Matrix () {}

public: // accessors

row_type& row (const size_t index) { return m_data[index]; }

const row_type& row (const size_t index) const { return m_data[index]; }

row_type& operator[] (const size_t index) { return m_data[index]; }

const row_type& operator[] (const size_t index) const { return m_data[index]; }

size_t size() const { return R * C; }

size_t rows() const { return R; }

size_t cols() const { return C; }

void randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator);

public: // operations

Matrix<R,C>& operator+= (const Matrix<R,C>&);

Matrix<R,C>& operator-= (const Matrix<R,C>&);

};

template<const size_t R, const size_t C>

void Matrix<R,C>::randomise(std::uniform_int_distribution<int>& distribution, std::default_random_engine& generator)

{

for (size_t row=0; row!=R; ++row)

{

for (size_t col=0; col!=C; ++col)

{

m_data[row][col] = distribution (generator);

}

}

}

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix()

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = 0;

}

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix(const Matrix<R,C>& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = source.m_data[row][col];

}

template<const size_t R, const size_t C>

Matrix<R,C>::Matrix(Matrix<R,C>&& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = std::move (source.m_data[row][col]);

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator= (const Matrix<R,C>& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = source.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator= (Matrix<R,C>&& source)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] = std::move (source.m_data[row][col]);

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator+= (const Matrix<R,C>& rhs)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] += rhs.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C>& Matrix<R,C>::operator-= (const Matrix<R,C>& rhs)

{

for (size_t row=0; row<R; ++row)

for (size_t col=0; col<C; ++col)

m_data[row][col] -= rhs.m_data[row][col];

return *this;

}

template<const size_t R, const size_t C>

Matrix<R,C> operator+ (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs)

{

Matrix<R,C> sum (lhs);

return sum += rhs;

}

template<const size_t R, const size_t C>

Matrix<R,C> operator- (const Matrix<R,C>& lhs, const Matrix<R,C>& rhs)

{

Matrix<R,C> sub (lhs);

return sub -= rhs;

}

template<const size_t R, const size_t C, const size_t R1, const size_t C1>

Matrix<R,C1> operator* (const Matrix<R,C>& lhs, const Matrix<R1,C1>& rhs)

{

static_assert (C==R1, "Matrix dimension mismatch!");

Matrix<R,C1> mul;

for (size_t x=0; x!=R; ++x)

{

for (size_t y=0; y!=C1; ++y)

{

int prod = 0;

for (size_t z=0; z!=C; ++z)

{

prod += lhs[x][z] * rhs[z][y];

}

mul[x][y] = prod;

}

}

return mul;

}

template<const size_t R, const size_t C>

std::ostream& operator<< (std::ostream& os, const Matrix<R,C>& m)

{

for (size_t row=0; row<R; ++row)

{

for (size_t col=0; col<C; ++col)

{

std::cout << m[row][col] << '\t';

}

std::cout << std::endl;

}

return os;

}

int main()

{

std::default_random_engine generator;

std::uniform_int_distribution<int> distribution (1,9);

const size_t rows = 2;

const size_t cols = 3;

Matrix<rows, cols> a, b;

a.randomise (distribution, generator);

b.randomise (distribution, generator);

std::cout << "Matrix a:\n\n" << a << '\n' << std::endl;

std::cout << "Matrix b:\n\n" << b << '\n' << std::endl;

std::cout << "Matrix a + b:\n\n" << a + b << '\n' << std::endl;

std::cout << "Matrix a - b:\n\n" << a - b << '\n' << std::endl;

Matrix<cols, rows> c;

c.randomise (distribution, generator);

std::cout << "Matrix c:\n\n" << c << '\n' << std::endl;

std::cout << "Matrix a * c:\n\n" << a * c << '\n' << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Yes, but it would be an extremely slow way to perform multiplication.

#include<stdio.h>

unsigned multiply (unsigned a, unsigned b)

{

unsigned x, y, z;

x = a<b?a:b; /* x is the smaller value */

y = a<b?b:a; /* y is the larger value */

if (!x) return x; /* if x is zero, the result is zero */

if (x==1) return y; /* if x is 1, the result is y */

z = y;

while (--x)

z += y;

return z;

}

int main()

{

for (unsigned x=0; x<=12; ++x)

for (unsigned y=0; y<=12; ++y)

printf ("%u times %u is %u\n", x, y, multiply (x,y));

return 0;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write a c plus plus program to multiply matrices using function template?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Program to display multiplication of two matrix?

The matrix multiplication in c language : c program is used to multiply matrices with two dimensional array. This program multiplies two matrices which will be entered by the user.


How do you write a program that will call a function to multiply 4 numbers and return the answer to main program?

In Windows, use notepad.exe; in linux, use program xedit.


How do you develop a JAVA program that computes matrices?

Matrices can't be "computed" as such; only operations like multiplication, transpose, addition, subtraction, etc., can be done. What can be computed are determinants. If you want to write a program that does operations such as these on matrices, I suggest using a two-dimensional array to store the values in the matrices, and use for-loops to iterate through the values.


Write a C program to find sum of 3 matrices?

matrix


Program to find biggest number among two numbers using template function?

template&lt;class T&gt; T&amp; max(const T&amp; x, const T&amp;y){return(x&gt;y?x:y); }


Is there any benefit in a C plus plus program to add two matrices?

No.


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


How to write C plus plus Program to take 2 distant values as argument and return the larger value on the screen?

Use the following template function: template&lt;class T&gt; T&amp; max(T&amp; x, T&amp; y){return(y&lt;x?x:y;}


What program has a modifiable calendar template?

There are many online shops that sell calendar template. Amazon.com is one of online shop that sells calendar template for any kind of models. You can visit www.amazon.com


Where can one obtain a table of contents template?

Someone may obtain a table of contents template from Window's Microsoft Word program and other various Microsoft programs will carry this template aid as well.


Which function is supported by an application program?

which function is support by an application program


What are its program components?

the main() function,the#include Directive, the variable definition. function prototype, program statements, the function definition,program comments and braces are the components of program in C++