Total members 11895 |It is currently Thu Dec 05, 2024 7:10 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

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

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


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 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






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com