Total members 11872 |It is currently Fri Jan 27, 2023 1:41 am Login / Join Codemiles

### Weka

Matrix multiplication (Product) using C++ and print the results, user enters two matrices
Matrix.cpp:
cpp code
`// Matrix.cpp: implementation of the Matrix class.////////////////////////////////////////////////////////////////////////#include<iostream.h>#include "Matrix.h"   //////////////////////////////////////////////////////////////////////// Construction/Destruction//////////////////////////////////////////////////////////////////////Matrix::Matrix(){	C[0][0]=0;}Matrix::~Matrix(){	}void Matrix::Multplay_Two_Matrixes(float newA[70][70],float newB[70][70]){	cout<<"The matrix A is: "<<endl;	for(int i=0;i<Arow;i++)	{		cout<<'|';		for(int j=0;j<Acol;j++)		{			A[i][j]=newA[i][j];			cout<<" "<<A[i][j]<<" ";		}		cout<<'|'<<endl;	}	cout<<"The matrix B is: "<<endl;		  	for(i=0;i<Brow;i++)	{		cout<<'|';		for(int j=0;j<Bcol;j++)		{			B[i][j]=newB[i][j];			cout<<" "<<B[i][j]<<" ";		}		cout<<'|'<<endl;	}		for(i=0;i<Arow;i++)		for(int j=0;j<Bcol;j++)			C[i][j]=0;		int u=Bcol;		        for(i=0;i<Arow;i++)		{						for(int j=0;j<Bcol;j++)			{								for(int k=0;k<Brow;k++)				{					C[i][j]+=A[i][k]*B[k][j];									}							}		}}void Matrix::NumberOfcolumes(const int newcol){  }void Matrix::NumerOfrows(const int newRowA,const int newRowB,const int newColA,const int newColB){	Arow=newRowA;	Brow=newRowB;	Acol=newColA;	Bcol=newColB;}void Matrix::DisplayProduct(){	cout<<"************************************************"<<endl;	for(int i=0;i<Arow;i++)	{		cout<<'|';		for(int j=0;j<Bcol;j++)			std::cin>>j;			cout<<" "<<C[i][j]<<" ";		cout<<'|'<<endl;	}	cout<<"************************************************"<<endl;	}`

Matrix.h
cpp code
`// Matrix.h: interface for the Matrix class.////////////////////////////////////////////////////////////////////////#if !defined(AFX_MATRIX_H__22076ABE_3D9F_4A9C_BCA5_DB7DBCA43C17__INCLUDED_)#define AFX_MATRIX_H__22076ABE_3D9F_4A9C_BCA5_DB7DBCA43C17__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000class Matrix  {	float A[70][70],B[70][70],C[70][70];	int Arow,Brow,Acol,Bcol;public:		Matrix();	virtual ~Matrix();	void NumerOfrows(const int,const int,const int,const int);	void NumberOfcolumes(const int);	void Multplay_Two_Matrixes(float[][70],float[][70]);	void DisplayProduct();		};#endif // !defined(AFX_MATRIX_H__22076ABE_3D9F_4A9C_BCA5_DB7DBCA43C17__INCLUDED_)`

main.cpp
cpp code
`#include<iostream.h>#include"Matrix.h"void main(){	int Arow,Brow,Acol,Bcol;	Matrix M;	cout<<"Enter the number of row of matrix A : ";    cin>>Arow;	cout<<"Enter the number of col of matrix A : ";	cin>>Acol;	cout<<"Enter the number of row of matrix B : ";	cin>>Brow;		cout<<"Enter the number of row of matrix B : ";	cin>>Bcol;	  	 	  while(Acol!=Brow)	  {		  cout<<"Number of columes of matrix A must equal number of rows of matrix "<<endl;		  cout<<"Enter the number of col of matrix A : ";		  cin>>Acol;	  		  cout<<"Enter the number of row of matrix B : ";		  cin>>Brow;	  }	M.NumerOfrows(Arow,Brow,Acol,Bcol);	M.NumberOfcolumes(Acol);	float A[70][70]; 	float B[70][70];	for(int i=0;i<Arow;i++)		for(int j=0;j<Acol;j++)			cin>>A[i][j];			for(i=0;i<Brow;i++)		for(int j=0;j<Bcol;j++)			cin>>B[i][j];		M.Multplay_Two_Matrixes(A,B);		M.DisplayProduct();}`

_________________
M. S. Rakha, Ph.D.
Queen's University

 Tweet
Author:
 Posts: 2715Have thanks: 74 time
 Page 1 of 1 [ 1 post ]

Related Posts  to : Matrix multiplication (Product) using C++
Matrix multiplication     -
Matrix Multiplication using java     -
Perform Multiplication by Shift Left Operations     -
Matrix Operations     -
Sparse matrix using a class     -
How to do a array matrix program     -
print element in 2d matrix     -
create Sparse Matrix in java     -

### Topic Tags

C++ Arrays, C++ Math