0% found this document useful (0 votes)
41 views4 pages

Multpilcation of Matrix Using 2D Arrays

This document discusses multiplying matrices using 2D arrays in C#. It first defines 3 matrices (a, b, c) as 2D arrays and gets the number of rows and columns for matrices a and b from user input. It then populates matrices a and b with user input values and displays their formats. It multiplies matrices a and b into result matrix c if their dimensions are compatible, displaying c; otherwise it indicates multiplication cannot be done.

Uploaded by

Taimoor Mazari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views4 pages

Multpilcation of Matrix Using 2D Arrays

This document discusses multiplying matrices using 2D arrays in C#. It first defines 3 matrices (a, b, c) as 2D arrays and gets the number of rows and columns for matrices a and b from user input. It then populates matrices a and b with user input values and displays their formats. It multiplies matrices a and b into result matrix c if their dimensions are compatible, displaying c; otherwise it indicates multiplication cannot be done.

Uploaded by

Taimoor Mazari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Multpilcation of Matrix using 2D Arrays

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace ConsoleApplication78
{
class Program
{
static void Main(string[] args)
{

int[,] a = new int[4, 4];


int[,] b = new int[4, 4];
int[,] c = new int[4, 4];

int i = 0;
int j = 0;

c[i, j] = 0;
int no_of_rows_a = 0;
int no_of_columns_a = 0;
int no_of_rows_b = 0;
int no_of_columns_b = 0;
Console.WriteLine(" Enter the number of rows for matrix A");

no_of_rows_a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(" Enter the number of columns for matrix A");

no_of_columns_a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Enter the number of rows for matrix B");

no_of_rows_b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" Enter the number of columns for matrix B");

no_of_columns_b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" The values of A matrix is :");
for (i = 0; i < no_of_rows_a; i++)
{
for (j = 0; j < no_of_columns_a; j++)
{

a[i,j]= Convert.ToInt32(Console.ReadLine());

}
}
Console.WriteLine(" The values of B matrix is :");
for (i = 0; i < no_of_rows_b; i++)
{
for (j = 0; j < no_of_columns_b; j++)
{

b[i, j] = Convert.ToInt32(Console.ReadLine());

}
}

Console.WriteLine(" The matrix format of A is ");

for (i = 0; i < no_of_rows_a; i++)


{
for (j = 0; j < no_of_columns_a; j++)
{
Console.Write(" {0}", a[i, j]);
}
Console.WriteLine("");
}
Console.WriteLine(" The matrix format of B is ");
for (i = 0; i < no_of_rows_b; i++)
{
for (j = 0; j < no_of_columns_b; j++)
{
Console.Write(" {0}", b[i, j]);
}
Console.WriteLine("");
}
if (no_of_columns_a == no_of_rows_b)
{
for (i = 0; i < no_of_rows_a; i++)
{
for (j = 0; j < no_of_columns_b; j++)
{
c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);
Console.Write(" ");
}
Console.WriteLine("");
} Console.WriteLine(" The resultant matrix is");
for (i = 0; i < no_of_rows_a; i++)
{
for (j = 0; j < no_of_columns_b; j++)
{

Console.Write(" {0}", c[i, j]);


}
Console.WriteLine();
}
}
else
{
Console.WriteLine("The matrix multiplication cannot be performed");

}
}

You might also like