Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how is the program for matrix multiplication written in C# and .NET?
Posted
Updated 16-Aug-18 20:08pm

using System;

class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter the number of rows and columns");
int r = int.Parse(Console.ReadLine());
int c = int.Parse(Console.ReadLine());
int[,] a = new int[r, c];
int[,] b = new int[r, c];
int[,] res = new int[r, c];
Console.WriteLine("Enter the elments to first matrix");
for (int i = 0; i < r; i++)
{
string[] s = Console.ReadLine().Split(' ');
for (int j = 0; j < c; j++)
{
a[i, j] = int.Parse(s[j]);
}
}

Console.WriteLine("the elements of first matrix are");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
Console.Write(a[i, j] + " ");
}
}
Console.WriteLine("");
Console.WriteLine("Enter the elements to second matrix");
for (int i = 0; i < r; i++)
{
string[] s1 = Console.ReadLine().Split(' ');
for (int j = 0; j < c; j++)
{
b[i, j] = int.Parse(s1[j]);
}
}
Console.WriteLine("the elements of second matrix are");
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
Console.Write(b[i, j] + " ");
}
}

for (int i = 0; i < r; i++)
{

for (int j = 0; j < c; j++)
{
res[i, j] = 0;
for (int k = 0; k < r; k++)
{
res[i, j] += a[i, k] * b[k, j];
}

}
}
Console.WriteLine("\nThe Matrix Multiplication is");
for (int i = 0; i < r; i++)
{
Console.WriteLine("");
for (int j = 0; j < c; j++)
{
Console.Write(res[i, j]+" ");
}
}
Console.ReadLine();

}
}
 
Share this answer
 
Comments
Dave Kreskowiak 17-Aug-18 10:39am    
This is not the place to post your homework and we frown upon doing someone work for them.
C#
for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
        for (int k = 0; k < N; k++) {
            C[i,j] += A[i,k] * B[k,j];
        }
    }
}
 
Share this answer
 
Comments
alireza1246 9-May-14 2:45am    
Thank u!
It does not work like this here.

Here is what is expected of enquirers:
1. TRY first what you want to do! You may find that it's not that hard.
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.


For now, start from here:
MSDN: Matrix.Multiply Method [^]
Harness the Features of C# to Power Your Scientific Computing Projects[^]
 
Share this answer
 
Comments
BillW33 19-Jun-12 14:13pm    
I like your answer, +5 :)
VJ Reddy 20-Jun-12 22:35pm    
Good answer. 5!
alireza1246 9-May-14 2:44am    
Five 4 this Solution :)

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