Click here to Skip to main content
15,895,774 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
#include "stdafx.h"


Using namespace System;

class MatrixMultiplication
{
int[,] a;
int[,] b;
int[,] c;
	
	public void ReadMatrix()
	{
		Console.WriteLine("\n Size of Matrix 1:");
		Console.Write("\n Enter the number of rows in Matrix 1 :");
		int m=int.Parse(Console.ReadLine());
		Console.Write("\n Enter the number of columns in Matrix 1 :");
		int n=int.Parse(Console.ReadLine());
		a=new int[m,n];
		Console.WriteLine("\n Enter the elements of Matrix 1:");
		for(int i=0;i<a.GetLength(0);i++)
		{
			for(int j=0;j<a.GetLength(1);j++)
			{
				a[i,j]=int.Parse(Console.ReadLine());
			}
		}
		
		Console.WriteLine("\n Size of Matrix 2 :");
		Console.Write("\n Enter the number of rows in Matrix 2 :");
		m=int.Parse(Console.ReadLine());
		Console.Write("\n Enter the number of columns in Matrix 2 :");
		n=int.Parse(Console.ReadLine());
		b=new int[m,n];
		Console.WriteLine("\n Enter the elements of Matrix 2:");
		for(int i=0;i<b.GetLength(0);i++)
		{
			for(int j=0;j<b.GetLength(1);j++)
			{
				b[i,j]=int.Parse(Console.ReadLine());
			}
		}
	}
	
	public void PrintMatrix()
	{
		Console.WriteLine("\n Matrix 1:");
		for(int i=0;i<a.GetLength(0);i++)
		{
			for(int j=0;j<a.GetLength(1);j++)
			{
				Console.Write("\t"+a[i,j]);
			}
			Console.WriteLine();
		}
		Console.WriteLine("\n Matrix 2:");
		for(int i=0;i<b.GetLength(0);i++)
		{
			for(int j=0;j<b.GetLength(1);j++)
			{
				Console.Write("\t"+b[i,j]);
			}
			Console.WriteLine();
		}
		Console.WriteLine("\n Resultant Matrix after multiplying Matrix 1 & Matrix 2:");
		for(int i=0;i<c.GetLength(0);i++)
		{
			for(int j=0;j<c.GetLength(1);j++)
			{
				Console.Write("\t"+c[i,j]);
			}
			Console.WriteLine();
		}
		
	}
	public void MultiplyMatrix()
	{
		if(a.GetLength(1)==b.GetLength(0))
		{
			c=new int[a.GetLength(0),b.GetLength(1)];
			for(int i=0;i<c.GetLength(0);i++)
			{
				for(int j=0;j<c.GetLength(1);j++)
				{
					c[i,j]=0;
					for(int k=0;k<a.GetLength(1);k++) // OR k<b.GetLength(0)
					c[i,j]=c[i,j]+a[i,k]*b[k,j];
				}
			}
		}
		else
		{
			Console.WriteLine("\n Number of columns in Matrix1 is not equal to Number of rows in Matrix2.");
			Console.WriteLine("\n Therefore Multiplication of Matrix1 with Matrix2 is not possible");
			Environment.Exit(-1);
		}
	}
}
class Matrix
{
	public static void Main()
	{
		MatrixMultiplication MM=new MatrixMultiplication();
		MM.ReadMatrix();
		MM.MultiplyMatrix();
		MM.PrintMatrix();
	}
}
Posted
Updated 29-Sep-10 21:53pm
v2
Comments
Abhinav S 30-Sep-10 3:53am    
What is the problem you are facing?
Suthish Nair 30-Sep-10 3:54am    
its better to put here the errors also..
CPallini 30-Sep-10 3:55am    
You should debug yourself the code. Debugging is a fundamental task of any developer. Please debug and then ask for specific help.
Sandeep Mewara 30-Sep-10 8:30am    
And what else you need to do ? I am complete.

The statement:
#include "stdafx.h"

does not belong in a C# program.
 
Share this answer
 
Comments
Rohan Garuda 30-Sep-10 5:04am    
if i remove that from the code .. it says missing ** #include #Quote#stdafx.h#Quote#**
Sauro Viti 30-Sep-10 5:29am    
do you know the difference between C# and C++/CLI? Pheraps you need to change the tags on your question...
Hi,
If you copy and paste the code properly it will work!

http://code.wikia.com/wiki/Matrix_multiplication[^]

Alan.
 
Share this answer
 

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