Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make a code that calculate the multiplied 2*2 matrix, I made it in C++ but now want it with classes and function. Here is the code I have written:

//this program will find the product of two matrices (2columns X 2rows)...
#include<iostream.h>
void main() 
{
	int a[4][4],b[4][4],c[4][4];
	int i,j,k,l;
	cout<<"Enter the value of the matrix a:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cin>>a[i][j];
		}
	}
	cout<<endl;
	cout<<"Enter the value of the matrix b:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cin>>b[i][j];
		}
	}

	cout<<"The multiplied value is:\n";
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			c[i][j]=0;
			for(k=0;k<2;k++){
				c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
			}
		}
	}
	for(i=0;i<2;i++){
		for(j=0;j<2;j++){
			cout<<c[i][j]<<"  ";
		}
		cout<<"\n";
	}
}
Posted
Updated 19-May-10 3:50am
v3

Create a Matrix class with a constructor or function that takes the row and column counts and creates the arrays (via new). Add a function which calculates the product and either saves the values or prints them out. If necessary add a function that prints out the results. Do this for a 2x2 matrix and see what is needed to scale up from there.
 
Share this answer
 
Alternatively to solution 1, you can use the Matrix class in the Boost library.

http://www.boost.org/doc/libs/1_39_0/libs/numeric/ublas/doc/matrix.htm[^]
 
Share this answer
 
Comments
Richard MacCutchan 11-Mar-14 16:54pm    
This question is nearly four years old. Do you really think the OP is still waiting for an answer?
Shelby Robertson 11-Mar-14 16:56pm    
LOL didn't notice. It was listed as "active" and at the top of the list when I clicked on "Quick Answers".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900