C# Matrix Operations - Addition, Subtraction, Multiplication








Simple C sharp program for adding, subtracting and multiplyilng two matrices.
The program takes inputs for rows and columns of the two matrices seperately and then calculates the following :

1. Addition
2. Subtraction
3. Multiplication.

Functions have been created for addition, subtraction, multiplication and display of the matrices. The program using OOPs is posted seperately. The Functions and variables have to be declared static.

using System;

namespace Array_Pgm
{

class MatrixCalc
{
static int i, j;

static void addMatrix(int[,] A, int[,] B, int m, int n)
{
int[,] C = new int[10, 10];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
C[i, j] = A[i, j] + B[i, j];
}
display(C, m, n);
}

static void subMatrix(int[,] A, int[,] B, int m, int n)
{
int[,] C = new int[10, 10];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
C[i, j] = A[i, j] - B[i, j];
}
display(C, m, n);
}

static void display(int[,] A, int m, int n)
{
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
Console.Write(A[i, j] + "\t");
Console.WriteLine();
}
}

static void mulMatrix(int[,]A,int [,]B,int m,int n)
{
int [,]C = new int[10,10];
for(i=0;i< m;i++)
{
for(j=0;j< n;j++)
{
C[i,j] = 0;
for(int k=0;k< n;k++)
{
C[i,j] = C[i,j] + A[i,k] * B[k,j];
}
}
}
display(C, m, n);
}

public static void Main(string[] args)
{
// Input First Matrix say A;
int m, n;
Console.Write("Enter No Of Roes And Columns Of Matrix A : ");
m =Convert.ToInt16(Console.ReadLine());
n =Convert.ToInt16(Console.ReadLine());
int[,] A = new int[10, 10];
Console.Write("\nEnter The First MNatrix : ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
A[i, j] =Convert.ToInt16(Console.ReadLine());
}

// Input Second Matrix say B;
int p, q;
Console.Write("Enter No Of Rows And Columns Of Matrix B :");
p =Convert.ToInt16(Console.ReadLine());
q =Convert.ToInt16(Console.ReadLine());
int[,] B = new int[10, 10];
Console.Write("\nEnter The Second Matrix:");
for (i = 0; i < p; i++)
{
for (j = 0; j < q; j++)
B[i, j] = Convert.ToInt16(Console.ReadLine());
}
Console.Clear();

// Display The Two Matrixes

Console.WriteLine("\nMatrix A : ");
display(A, m, n);

Console.WriteLine("\nMatrix B: ");
display(B, p, q);

// Menu
Console.WriteLine("**** MENU For Matrix Opearations *****");
Console.WriteLine(" 1. Addition");
Console.WriteLine(" 2. Subtraction");
Console.WriteLine(" 3. Multiplication");
Console.WriteLine(" 4. Exit");
Console.WriteLine("Enter Your option");
int choice = Convert.ToInt16(Console.ReadLine());

switch (choice)
{
case 1:
if (m == p && n == q)
{
Console.WriteLine("Addition ");
addMatrix(A, B, m, n);
}
else
Console.WriteLine("ERROR !!!!!! Rows and Columns Of Both martixes should be same");
break;

case 2:
if (m == p && n == q)
{
Console.WriteLine("Subtraction ");
subMatrix(A, B, m, n);
}
else
Console.WriteLine("ERROR !!!!!! Rows and Columns Of Both martixes should be same");
break;

case 3:
if (n == p)
{
Console.WriteLine("Multiplication: ");
mulMatrix(A, B, m, q);
}
else
Console.WriteLine("ERROR !!!!!! Column of First matrix and Row Of Second Matrix should be same");
break;

case 4:
Console.Beep(1000, 1000);
break;
/* default:
Console.WriteLine("Error");*/
}
}
}
} 
Previous Post Next Post