Tuesday, 27 November 2012

Matrix Manipulation

using System;
using System.Collections.Generic;
using System.Text;

namespace matrixmanipulation
{
class mat
{
int[,] a=new int[30,30];
int m,n;
int i,j,k;
public void input()
{
Console.WriteLine("Enter the row");
m=int.Parse(Console.ReadLine());
Console.WriteLine("Enter the column");
n=int.Parse(Console.ReadLine());
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{

a[i,j]=int.Parse(Console.ReadLine());
}
}
}
public void getdata(mat m1, mat m2, int ch)
{
switch (ch)
{
case 1:
{
if (m1.m == m2.n)
{
for (i = 1; i <= m1.m; i++)
{
for (j = 1; j <= m2.n; j++)
{
a[i, j] = 0;
for (k = 1; k <= m1.n; k++)
{
a[i, j] = a[i, j] + m1.a[i, k] * m2.a[k, j];
}
}
}
m = m1.m;
n = m2.n;
output();
}
else
{
Console.WriteLine("cant perform");
}
break;


}
case 2:
{
if (m1.m == m2.m && m1.n == m2.n)
{
for (i = 1; i <= m1.m; i++)
{
for (j = 1; j <= m2.n; j++)
{
a[i, j] = m1.a[i, j] + m2.a[i, j];
}
m = m1.m;
n = m2.n;
output();
}
}
else
{
Console.WriteLine("cant perform");
}
break;

}
case 3:
{
if (m1.m == m2.m && m1.n == m2.n)
{
for (i = 1; i <= m1.m; i++)
{
for (j = 1; j <= m2.n; j++)
{
a[i, j] = m1.a[i, j] - m2.a[i, j];
}
m = m1.m;
n = m2.n;
output();
}
}
else
{
Console.WriteLine("cant perform");
}
break;

}
}
}



public void output()
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
Console.Write("{0}",a[i,j]);
}
Console.WriteLine("\n");
}
}
}


class Program
{
static void Main(string[] args)
{
mat m1=new mat();
mat m2=new mat();
mat m3=new mat();
int ch=0;
while(ch<4)
{
Console.WriteLine("1.matix multiplication");
Console.WriteLine("2.Addition");
Console.WriteLine("3.Subtrraction");
Console.WriteLine("4.exit");
Console.WriteLine("enter your choice");
ch=int.Parse(Console.ReadLine());
if(ch<4)
{
m1.input();
m2.input();
m3.getdata(m1,m2,ch);
}
}

}
}
}

No comments:

Post a Comment