Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Matrix Multiplication

SaqibAhmedSaqibAhmed Posts: 8Member
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();
}
}
}

Comments

Sign In or Register to comment.