Assignment Topic: Addition and Multiplication of two Matrix using any
programming language.
Submitted By:
Atif Chaudhry
MSCS-1st Semester
Submitted To:
Prof. Dr. Kashif Hanif
GOVERNMENT COLLEGE UNIVERSITY FAISALABAD
Add Operation Code in C# :
public void Add_operations()
{ //Exception handling if getting error give message try { int row, col, i, j; int[,] mat = new int[100, 100]; int[,] mat2 = new int[100, 100]; int[,] sum = new int[100, 100]; Console.WriteLine("Enter Number of Rows for matrix "); row = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Number of columns for matrix "); col = Convert.ToInt32(Console.ReadLine());
//Getting Values in Matrix One
for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.WriteLine("Please Enter Value of Matrix one at Index:" + i + 1 + " " + j + 1); mat[i, j] = Convert.ToInt32(Console.ReadLine()); } } //Getting Value in 2nd Matrix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.WriteLine("Please Enter Value Matrix two at Index:" + i + 1 + " " + j + 1); mat2[i, j] = Convert.ToInt32(Console.ReadLine()); } } //Adding 2 Matrix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { sum[i, j] = mat[i, j] + mat2[i, j]; } } //Display marix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.Write(sum[i, j]); Console.WriteLine(""); } } } catch (Exception ex) { Console.Write(ex.Message); } }
Multiply Two Matrix Code in C# :
public void Multiply_operation()
{ try {
int row, col, i, j;
int[,] mat = new int[200, 200]; int[,] mat2 = new int[200, 200]; int[,] sum = new int[200, 200];
Console.WriteLine("Enter Number of Rows for
matrix :"); row = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Number of columns for matrix: "); col = Convert.ToInt32(Console.ReadLine()); //Getting Values in Matrix One for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.WriteLine("Please Enter Value of Matrix one at Index:" + i + 1 + " " + j + 1); mat[i, j] = Convert.ToInt32(Console.ReadLine()); } } //Getting Value in 2nd Matrix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.WriteLine("Please Enter Value Matrix two at Index:" + i + 1 + " " + j + 1); mat2[i, j] = Convert.ToInt32(Console.ReadLine()); } } //Multiply 2 Matrix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { sum[i, j] += mat[i, j] * mat2[i, j]; } } //Display marix for (i = 0; i < row; i++) { for (j = 0; j < col; j++) { Console.Write(sum[i, j]); Console.WriteLine(""); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }