C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2722
Number of posts: 5749

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Matrix Multiplication Posted by SaqibAhmed on 23 Mar 2009 at 2:24 AM
Hey!
I am just a beginner. I am trying to multiply 2 matrices but the code is not working well.Its not multiplying 1st row with 1st column,1st row with 2nd column, 2nd row with 1st column and 2nd row with 2nd column.
Its just multiplying 1st element of 1st matrix with 1st element of 2nd matrix, etc.
Heres my code Help me with the matrix multiplication.
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
int[,] matrix1 = new int[2, 2];
int[,] matrix2 = new int[2, 2];
int[,] result = new int[2, 2];

//Taking Input 1st Matrix
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.WriteLine("Enter 1st Matrix: ");
matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
}
} Console.WriteLine();

//Taking Input 2nd Matrix
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
Console.WriteLine("Enter 2st Matrix: ");
matrix2[k, l] = Convert.ToInt32(Console.ReadLine());
}
} Console.WriteLine();

//Printing 1st Matrix
Console.WriteLine("Matrix 1: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matrix1[i, j] + " ");
} Console.WriteLine();
} Console.WriteLine();

//Printing 2nd Matrix
Console.WriteLine("Matrix 2: ");
for (int k = 0; k < 2; k++)
{
for (int l = 0; l < 2; l++)
{
Console.Write(matrix2[k, l] + " ");
} Console.WriteLine();
} Console.WriteLine();

//Adding Matrices
Console.WriteLine("Matrix 1 + Matrix 2: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matrix1[i, j] + matrix2[i, j] + " ");
} Console.WriteLine();
} Console.WriteLine();

//Subtracting Matrices
Console.WriteLine("Matrix 1 - Matrix 2: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
Console.Write(matrix1[i, j] - matrix2[i, j] + " ");
} Console.WriteLine();
} Console.WriteLine();

//Multiplying Matrices
Console.WriteLine("Matrix 1 * Matrix 2: ");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{

result[i, j] += matrix1[i, j] * matrix2[i, j];
Console.Write(result[i, j] + " ");
} Console.WriteLine();
} Console.ReadLine();
}
}
}

Report
Re: Matrix Multiplication Posted by SaqibAhmed on 30 Mar 2009 at 3:36 AM
Havent got a single answer
Report
Re: Matrix Multiplication Posted by SaqibAhmed on 30 Mar 2009 at 3:37 AM
Havent got a single answer
Report
Re: Matrix Multiplication Posted by dotnetfreak89 on 6 Oct 2012 at 4:14 AM
This should solve your problem easily

http://code-kings.blogspot.in/2012/08/matrix-multiplication-in-c-45.html

just follow this link



 

Recent Jobs