Here some hints and code snippet from the solution of this problem
1) Before you should start working on the actual problem, you have
good understanding about following things in c#
a) Two dimensional arrays manipulation in C#, for this you can study
MSDN following section
http://msdn2.microsoft.com/en-us/library/aa288453(VS.71).aspx
b) should have good understanding about loops, conditional statements
in C#
c) Also you should know how to parse a String value to int value, for
this please see the code snippet given below
d) And also you should know how to multiply two matrix, for that you
can see following link, just in case your math is little rusty:
http://www.purplemath.com/modules/mtrxmult.htm
CODE SNIPPET (not complete code ) of my Matrix class
read the comments carefully to understand the code...
class Matrix
{
// Matrix class, variable of int array type to store value of
Matrix instance
// remember to differntiate between array matrix and class
Matrix, in code below
int[,] matrix;
// Matrix class constructor, construct instance from given 2D
array
public Matrix(int[,] arr)
{
// not much to do to creatr an instance
// simple! just assign given array to our Matrix class
matrix variable
matrix = arr;
}
// Matrix class constructor, construct instance from scratch
// where user define and give values to the 2D array
public Matrix(String name)
{
// call method to intialize the 2D array
setMatrix(name);
// call method to get and set values of 2D array & hence
create the Matrix class instance
setMatrixValue(matrix.GetLength(0), matrix.GetLength(1),
name);
}
// method to intialize the 2D array,
// dimension (rank) of the matrix is set as per user
instruction
void setMatrix(String name)
{
// variable to store rows and cols of the 2D array
int row=0; int col=0;
// temp a string var to store user rows and cols value in
string for timebeing
// remmber ReadLine() return a string value, that we cannot
store in int
String temp;
// asking user to enter the dimensions of matrix
Console.Out.WriteLine("Enter the dimensions of Matix:{0} ",
name);
Console.Out.Write("Enter number of Rows: ");
// got value in string temp, and then rest few lines to
parse string into int
temp= Console.In.ReadLine();
temp.Trim(); // remove or trim spaces from temp
// ok first let's check before parsing if user has given
any value or not or just pressed enter
if(temp != null & temp.Length > 0)
{
// Int32 the class representation of datatype int,
// Parse() is Int32 class static method to parese
string temp to int row, check MSDN for this
row=Int32.Parse(temp);
Console.Out.WriteLine("Given row value = {0}", row);
// set temp null for next input from user
temp = null;
}
// same steps to get cols from user
Console.Out.Write("Enter number of Cols: ");
temp = Console.In.ReadLine();
temp.Trim();
if (temp != null & temp.Length > 0)
{
col = Int32.Parse(temp);
Console.Out.WriteLine("Given row value = {0}", col);
temp = null;
}
// check if row or col is not given as zero by user
if(row!=0 & col!=0)
{
// initialize matrix array, remember all arrays are
also object of the class Array,
// thats why we are using new to intialize array of
int[,] type
matrix = new int[row,col];
Console.Out.WriteLine(" {0} Matrix intialized with [{1}]
[{2}] rows and cols and Rank:{3} ", name, matrix.GetLength(0),
matrix.GetLength(1), matrix.Rank);
}
else
Console.Out.Write("{0} Matrix not intialized due to
incorrect dimensions: ", name);
}
//Method to get matrix array values from user
void setMatrixValue(int rows, int cols, string name)
{
// string temp, again for parsing int from string
String temp = null;
// two nested loops to get 2D arrays values, outer for
row, inner for col
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Out.Write("Enter value of {0}[{1}][{2}]:
", name, i + 1, j + 1);
// value from user, and then all the steps for
parsing to int
temp = Console.In.ReadLine();
temp.Trim();
if (temp != null & temp.Length > 0)
{
matrix[i,j] = Int32.Parse(temp);
Console.Out.WriteLine("Given row value = {0}",
matrix[i,j]);
temp = null;
}
}
}
Console.Out.WriteLine(" {0} [{1}][{1}] Matrix values are
set: ", name, rows, cols);
// call to method to dosply the complete values of the
matrix given by user
displaymatrix();
}
// Method to disply the values store matrix[,] of this class
public void displaymatrix()
{
// GetLength() method give the size of the given dimension
// check MSDN for GetLength(), but in which class is this
method present?
// its in Array class
// get the size of the row in matrix array, her 0 in
getLength represent row or col dimension
int rows= matrix.GetLength(0);
int cols= matrix.GetLength(1);
Console.Out.WriteLine(" Matrix size is [{0}][{1}] :
",rows,cols );
// loops for displaying the all the values of the 2D array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Out.Write("{0} ", matrix[i, j]);
}
Console.Out.WriteLine(" ");
}
}
// The function mutiply given matrix with 'this' matrix,
public Matrix mutliply( Matrix mat2)
{
// code for this method you have to write on your own
}
// method to check if we can multiply the matirx
bool checkMulti(int[,] m1, int[,] m2)
{
int m1_cols = m1.GetLength(1);
int m2_rows = m2.GetLength(0);
if (m1_cols == m2_rows)
return true;
else
return false;
}
}
Ok, happy coding
setMatrixValue( matrix.GetLength(0), matrix.GetLength(1),
name);
}
// method to intialize the 2D array,
// dimension (rank) of the matrix is set as per user
instruction
void setMatrix(String name)
{
// variable to store rows and cols of the 2D array
int row=0; int col=0;
// temp a string var to store user rows and cols value in
string for timebeing
// remmber ReadLine() return a string value, that we cannot
store in int
String temp;
// asking user to enter the dimensions of matrix
Console.Out.WriteLine("Enter the dimensions of Matix:{0} ",
name);
Console.Out.Write ("Enter number of Rows: ");
// got value in string temp, and then rest few lines to
parse string into int
temp= Console.In.ReadLine();
temp.Trim(); // remove or trim spaces from temp
// ok first let's check before parsing if user has given
any value or not or just pressed enter
if(temp != null & temp.Length > 0)
{
// Int32 the class representation of datatype int,
// Parse() is Int32 class static method to parese
string temp to int row, check MSDN for this
row=Int32.Parse(temp);
Console.Out.WriteLine("Given row value = {0}", row);
// set temp null for next input from user
temp = null;
}
// same steps to get cols from user
Console.Out.Write("Enter number of Cols: ");
temp = Console.In.ReadLine();
temp.Trim();
if (temp != null & temp.Length > 0)
{
col = Int32.Parse(temp);
Console.Out.WriteLine ("Given row value = {0}", col);
Console.Out.WriteLine (" Matrix size is [{0}][{1}] :
",rows,cols );
// loops for displaying the all the values of the 2D array
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Out.Write("{0} ", matrix[i, j]);
}
Console.Out.WriteLine(" ");
}
}
// The function mutiply given matrix with 'this' matrix,
public Matrix mutliply( Matrix mat2)
{
// code for this method you have to write on your own
}
// method to check if we can multiply the matirx
bool checkMulti(int[,] m1, int[,] m2)
{
int m1_cols = m1.GetLength(1);
int m2_rows = m2.GetLength(0);
if (m1_cols == m2_rows)
return true;
else
return false;
}
}
Ok, happy coding
Aoa,