answersLogoWhite

0


Best Answer

class complex {

private:

double real;

double imaginary;

public:

complex() {...} // constructor, etc.

operator+(const& complex a) { // operator plus

this->real += a.real;

this->imaginary += a.imaginary;

}

}

User Avatar

Wiki User

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

Wiki User

14y ago

#include<iostream>

#include<conio.h>

#include<math.h>

using namespace std;

class Polynomial

{

private: public:

int degree()

{ }

Polynomial addition(const Polynomial &p)

{ }

Polynomial sub(const Polynomial &p)

{ }

Polynomial multiply(const Polynomial&p)

{ }

bool equal(const Polynomial&p)

{ }

void read()

{ }

void print()

{

void read(); }

int evaluate(int x)

{ }

};

main()

{

int n;

do

{

cout<<"What would you like to do?"<<"\n";

cout<<"------------------------------------"<<endl;

cout<<"1.Give a value to a polynomial(print the polynomial}"<<"\n";

cout<<"2.Add the two polynomials"<<"\n";

cout<<"3.Multiply the two polynomials"<<"\n";

cout<<"4.Subtract the two polynomials"<<"\n";

cout<<"5.Test equality of the two polynomial"<<"\n";

cout<<"6.Evaluate the two polynomials for a given value of x"<<"\n";

cout<<"7.Quit"<<"\n";

cout<<"------------------------------------"<<endl;

cout<<"please choose one of the above options"<<"\n";

cin>>n;

switch(n)

{

case 1:

break;

case 2:

break;

case 3:

break;

case 4:

break;

case 5:

break;

case 6:

break;

case 7:

exit(1);

break;

default:

cout<<"you have entered a wrong choice";

break;

}

}

while(n>0&&n<8);

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

// this is incomplete, showing only the operator+ overload

class complex {

public:

double real, imaginary;

operator+ (const complex& operand) {

real += operand.real;

imaginary += operand.imaginary;

}

}

complex a = {1, 2};

complex b = {3, 4};

a = a+b; // the result is {4, 6}

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

#include<time.h>

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

public: // operations

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

};

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

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()

{

srand ((unsigned)time(nullptr));

const size_t rows = 3;

const size_t cols = 4;

Matrix<rows, cols> a, b, c;

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

{

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

{

a[row][col] = rand() % 10;

b[row][col] = rand() % 10;

}

}

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;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

here is a cpp program that illustrates multiplication of two rational numbers objects and store it another object...

class rational

{

private : int nr,dr;

public : void accept();

rational multiply(rational);

void display();

}

void main()

{

rational r1,r2,r3;

ri.accept();

r2.accept();

r3=r1.multplyr2;

r3.display;

}

void rational :: accept()

{

cout<<"\n enter nnr and dr";

cinn>>"nr>>dr;

}

rational rational ::multiply(rational m)

{

rational r;

r.nr=nr*m.nr;

r.dr=dr*m.dr;

return;

}

void rational::display()

{cout<<'\n result="<<nr<<"/""<<dr;

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

C++ provides <ratio> for performing compile-time operations upon rational numbers. To achieve the same thing at runtime you must either define your own runtime class or use a 3rd-party library that provides a runtime rational, such as boost::rational which can be found in the <boost/rational.hpp> header of the Boost C++ library.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

The following program demonstrates one way of handling rational numbers. It uses operator overloading rather than function overloading, however you can provide the equivalent function overloads if required.

Note that although 0/n is a valid rational number, its reciprocal (which is required for division) is n/0 which is invalid. There is no valid method of dealing with this invariant from within the class itself, thus the class throws a divide by zero exception whenever an invalid rational is constructed or assigned.

Although the default constructor accepts two integrals for p and q (creating a p/q rational number), assignments are primarily handled through strings of the form "p/q", where p and q are integers. The assignment operator's implementation is extensive to ensure the input string is correctly formed, including correct handling of partial forms such as nullstr, "/", "1/" and "/1". Invalid input will result in an out of range exception, including any divide by zero forms.

To test string input prior to construction, use the static is_valid method.

#include<iostream>

#include<string>

#include<sstream>

#include<stdexcept>

// GCF: greatest common factor (AKA: GCD, greatest common divisor)

int gcf (int a, int b)

{

// Recursive Euclid algorithm.

return (b != 0 ) ? gcf (b, a % b) : a;

}

// LCM: least common multiple

int lcm(int a, int b)

{

return (b / gcf (a, b)) * a;

}

// Class to store rational numbers (fractions).

class rational

{

friend std::ostream& operator<< (std::ostream&, const rational&);

private:

int m_p;

int m_q;

public:

rational (const int = 1, const int = 1);

rational (const rational&);

rational (const std::string& str);

rational (const char* c_str);

rational& operator= (const rational&);

rational& operator= (const std::string&);

rational& operator= (const char*);

rational& operator-= (const rational&);

rational& operator*= (const rational&);

rational& operator/= (const rational&);

rational& operator+= (const rational&);

rational operator- (const rational&);

rational operator* (const rational&);

rational operator/ (const rational&);

rational operator+ (const rational&);

rational reciprocal() const;

rational& simplify ();

rational& unsimplify (int multiple);

bool operator 0)

{

copy.insert (copy.begin(), tokens[10]);

++slash_pos;

}

// Ensure there is only one slash.

if (copy.rfind (tokens.front()) != slash_pos)

{

ss << "must contain one '/' character!";

throw std::out_of_range (ss.str());

}

// Separate the p and q portions.

std::string p = copy.substr (0, slash_pos++);

std::string q = copy.substr (slash_pos, copy.size() - slash_pos);

// Convert to integers.

std::stringstream ssp (p);

std::stringstream ssq (q);

if (!(ssp >> m_p && ssq >> m_q))

{

// Should never get here.

ss << "cannot be converted to a rational number!";

throw std::out_of_range (ss.str());

}

if (!m_q)

throw std::out_of_range ("divide by zero");

return *this;

}

// Equality operator.

bool rational::operator== (const rational& other) const

{

// Simplify both fractions before comparing.

rational copy (*this);

rational temp (other);

copy.simplify();

temp.simplify();

return copy.m_p==temp.m_p && copy.m_q==temp.m_q;

}

// Return the reciprocal by value (swap numerator/denominator)

rational rational::reciprocal() const

{

return rational (m_q, m_p);

}

// Simplifies the fraction.

rational& rational::simplify()

{

// Simplify the sign.

if (m_q<0)

{

m_p *= -1;

m_q *= -1;

}

int factor = gcf (this->m_p, this->m_q);

this->m_p /= factor;

this->m_q /= factor;

return *this;

}

// Usimplifies the fraction (use given denominator)

rational& rational::unsimplify (int denominator)

{

this->m_p *= denominator / m_q;

this->m_q = denominator;

return *this;

}

bool rational::is_valid (const char* c_str)

{

return is_valid (std::string (c_str));

}

bool rational::is_valid (const std::string& str)

{

try {

rational temp (str);

}

catch (std::exception& /*e*/)

{

// show the exception message (optional)

// std::cerr << e.what() << std::endl;

return false;

}

return true;

}

// Test drive the rational class.

int main()

{

std::cout << "Rational class tests." << std::endl;

std::cout << "\nTesting basic arithmetic:\n" << std::endl;

rational a (1, 2); // e.g., "1/2"

rational b (2, 5); // e.g., "2/5"

std::cout << a << " + " << b << " = " << a + b << std::endl;

std::cout << a << " - " << b << " = " << a - b << std::endl;

std::cout << a << " * " << b << " = " << a * b << std::endl;

std::cout << a << " / " << b << " = " << a / b << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

The following code creates two matrices of the same size initialised with random data and multiples them together to create a third matrix before printing all three matrices.

#include<iostream>

#include<vector>

#include<time.h>

#include<string>

#include<iomanip>

void print_matrix (const std::string& title, const std::vector<std::vector<int>>& matrix)

{

std::cout<<title<<'\n'<<std::endl;

for (size_t row=0; row<matrix.size(); ++row)

{

for (size_t column=0; column<matrix[row].size(); ++column)

{

std::cout<<std::setw(10)<<matrix[row][column]<<' ';

}

std::cout<<std::endl;

}

std::cout<<std::endl;

}

// operator overload (throws if array sizes do not match).

std::vector<std::vector<int>> operator* (std::vector<std::vector<int>>& matrix1, std::vector<std::vector<int>>& matrix2)

{

if( matrix1.size() != matrix2.size() )

throw;

for (size_t index=0; index<matrix1.size(); ++index )

if( matrix1[index].size() != matrix2[index].size() )

throw;

std::vector<std::vector<int>> result (matrix1.size(), std::vector<int> (matrix1[0].size()));

for (size_t row=0; row<matrix1.size(); ++row)

{

for (size_t column=0; column<matrix1[row].size(); ++column)

{

result[row][column]=matrix1[row][column]*matrix2[row][column];

}

}

return( result );

}

int main()

{

srand((unsigned)time(NULL));

const size_t rows=10;

const size_t columns=5;

std::vector<std::vector<int>> matrix1 (rows, std::vector<int> (columns));

std::vector<std::vector<int>> matrix2 (rows, std::vector<int> (columns));

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

{

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

{

matrix1[row][column]=rand();

matrix2[row][column]=rand();

}

}

std::vector<std::vector<int>> product=matrix1*matrix2;

print_matrix ("Matrix 1", matrix1);

print_matrix ("Matrix 2", matrix2);

print_matrix ("Product", product);

}

Example output:

Matrix 1

11790 12960 27632 18422 6235

28745 29045 2712 31426 18470

3656 26306 2289 24650 5680

13170 24295 10690 8073 16094

27004 3338 5057 25325 7894

26261 4059 27068 1781 21322

5360 16923 5160 19713 2217

1037 19349 5898 1478 17739

15797 31399 31898 15809 20181

21389 9101 15375 20375 27750

Matrix 2

6060 17418 5888 3927 22023

20319 12894 16317 16007 21042

7813 4065 4774 6847 9377

25413 10976 26216 5663 1994

15670 23602 6963 30778 13228

5959 4436 11966 4811 16757

31163 1896 28704 15790 30695

12971 30907 25222 24604 14301

1969 17592 21983 1980 19610

4514 31355 7911 531 31172

Product

71447400 225737280 162697216 72343194 137313405

584069655 374506230 44251704 503035982 388645740

28564328 106933890 10927686 168778550 53261360

334689210 266661920 280249040 45717399 32091436

423152680 78783476 35211891 779452850 104421832

156489299 18005724 323895688 8568391 357292754

167033680 32086008 148112640 311268270 68050815

13450927 598019543 148759356 36364712 253685439

31104293 552371208 701213734 31301820 395749410

96549946 285361855 121631625 10819125 865023000

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

There is no need to overload the multiplication operator to do this. Multiplication of complex numbers is already an overloaded operation within the std::complex template class:

#include<iostream>

#include<complex>

int main()

{

std::complex<int> c1(-1,3);

std::complex<int> c2(-1,-3);

std::cout<<c1<<" * "<<c2<<" = "<<c1*c2<<std::endl;

}

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the C plus plus program for the multiplication of two matrices?
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


A c program for multiplication of two integers a and b?

/*PROGRAM TO ACCEPT TWO NUMBERS FROM THE USER AND PRINT THEIR MULTIPLICATION. */ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int a, b, c; // Declaration of Variables. Variables 'a' &amp; 'b' to hold first &amp; second number. And 'c' to hold result. clrscr(); // To clear the output screen every time program is executed. printf("\n Enter the first number : "); scanf("%d", &amp;a); // To accept the first number. printf("\n Enter the second number : "); scanf("%d", &amp;b); // To accept the second number. c = a*b; // Logic to get the product of the entered two numbers. printf("\n Multiplication of %d &amp; %d = %d", a, b, c); // Displaying result. getch(); // To hold the output screen. }


How many main functions can a c plus plus program have?

1. In C language, you cannot compile a source-file if it has two (or more) functions with the same name. 2. You cannot link a program if it has two (or more) global (ie: non-static) functions with the same name.


Can you swap two matrix?

Provided both matrices are mutable, two matrices A and B can be swapped like any other two items: create temporary storage to store a copy of A, then assign B to A, and finally assign the temporary copy of the previous version of A to B. Note that in the C programming language, matrices cannot be assigned to each as such. One implementation of this algorithm might operate on the basis of references (pointers), and can thus swap two matrix references by swapping two pointers in the manner detailed above. Implementations wishing to actually transfer the data held in one matrix to another would use a library function such as memcpy() to transfer data.


VB Script program to display multiplication of two matrices?

&lt;html&gt; &lt;head&gt; &lt;/head&gt; &lt;script language="VBscript"&gt; sub fact(n) dim f,i f=1 i=1 do while(i&lt;=cint(n)) f=cint(f)*cint(i) i=i+1 loop document.write("&lt;br&gt;Factorial of"&amp;n&amp;"is"&amp;f) end sub &lt;/script&gt; &lt;body&gt; &lt;form name="form1" method="post" action=" "&gt; &lt;input name="n" type="text" id="n"&gt; &lt;input name="Button" type="button" onClick="fact(form1.n.value)" value="Factorial"&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;

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.


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

No.


Write an algorithm for multiplication of two sparse matrices?

how to multiply two sparse matrices


What is the Flowchart for multiplication of two matrices?

[object Object]


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.


Multiplication of two 2 X 2 matrices is?

Closed . . . .A+


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


How are the inverse matrix and identity matrix related?

If an identity matrix is the answer to a problem under matrix multiplication, then each of the two matrices is an inverse matrix of the other.


How do you determine if you can multiply two matrices?

Tthe matrix multiplication A*Bis defined only if the number of columns in the first matrix, A, is the same as the number of rows in the second, B. Note that the condition for the multiplication of B*A will be the reverse.


Are there Exceptions to the commutative property?

The commutative property works for adding and multiplying e.g. 2+4=4+2 and 3x4=4x3. But it doesn't work for subtraction and division so 5-3&ne;3-5 and 6&divide;2&ne;2&divide;6 so subtraction and division could be considered as exceptions.


What is commuting use?

Commuting in algebra is often used for matrices. Say you have two matrices, A and B. These two matrices are commutative if A * B = B * A. This rule can also be used in regular binary operations(addition and multiplication). For example, if you have an X and Y. These two numbers would be commutative if X + Y = Y + X. The case is the same for X * Y = Y * X. There are operations like subtraction and division that are not commutative. These are referred to as noncommutative operations. Hope this helps!!


How do two negatives make a plus?

The short answer: That is the definition of multiplication with negative numbers.