Little Matrix Tutorial

I made this program for class and I hope it could serve as a little tutorial for basic matrix work. The program is written in C# but I guess one could use the idea and translate it into other languages.

I think the code is well documented but if you have any questions, please feel free to ask!

/* Wangan Dreams
File Specification: MatrixWork
Purpose: This program works with 4 X 4 square matrices.
Given two pre-defined integer 4 X 4 matrices, the user may:
add them ( both ways ), subtract them ( both ways ),
integer scalar multiply them, and multiply them ( both ways ).
This program presents a menu choice from which the user
may select which operation they with to have processed.
The program has an internal class within its scope
that accomplishes these processes.
*/

using System;

namespace MatrixWork
{
class Class1
{
static void Main(string[] args)
{
// Explicitly define the two-predefined 4 X 4 matrices
int[,] matrixA = { {1, 3, 0, -4}, {5, 6, 1, 0}, {1, -1, 1, -1},
{-3, 2, 4, -1} };
int[,] matrixB = { {6, 0, 2, 1}, {3, 4, -1, 5}, {-2, -3, -4, -6},
{0, -5, 1, 1} };

// Declare a matrix that will be used for the results
int[,] matrixC = new int[Constants.numRows, Constants.numCols];

// Declare local addresses
int menuChoice;
int scalar;

// Instantiate a reference of the Matrix class
Matrix resultant = new Matrix();

// Prime the menu choice from the user
menuChoice = ClassMenu.getMenuChoice();

// Keep running the program until the user wishes to quit
while ( menuChoice != 0 )
{
Utilities.cls( 0 );
// The matrix works begins...
switch ( menuChoice )
{
case 0 : // End execution of the program
break;
case 1 : // Addition [A] +
resultant.InitAddORSub( matrixA, matrixB, 'a' );
matrixC = resultant.EchoAddORSub();
Console.WriteLine( "Addition [A] + \n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 2 : // Addition + [A]
resultant.InitAddORSub( matrixB, matrixA, 'a' );
matrixC = resultant.EchoAddORSub();
Console.WriteLine( "Addition + [A]\n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 3 : // Subtraction [A] -
resultant.InitAddORSub( matrixA, matrixB, 's' );
matrixC = resultant.EchoAddORSub();
Console.WriteLine( "Subtraction [A] - \n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 4 : // Subtraction - [A]
resultant.InitAddORSub( matrixB, matrixA, 's' );
matrixC = resultant.EchoAddORSub();
Console.WriteLine( "Subtraction - [A]\n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 5 : // Integer scalar multiplication s[A]
scalar = InputClass.InputInt( "Enter your integer scalar --> " );
Utilities.cls( 0 );
resultant.InitScalarMultiply( matrixA, scalar );
matrixC = resultant.EchoScalarMultiply();
Console.WriteLine( "Integer scalar multiplication {0}[A]\n", scalar );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 6 : // Integer scalar multiplication s
scalar = InputClass.InputInt( "Enter your integer scalar --> " );
Utilities.cls( 0 );
resultant.InitScalarMultiply( matrixB, scalar );
matrixC = resultant.EchoScalarMultiply();
Console.WriteLine( "Integer scalar multiplication {0}\n", scalar );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 7 : // Mutliplication [A] X
resultant.InitMultiplication( matrixA, matrixB );
matrixC = resultant.EchoMultiplication();
Console.WriteLine( "Multiplication [A] X \n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
case 8 : // Multiplication X [A]
resultant.InitMultiplication( matrixB, matrixA );
matrixC = resultant.EchoMultiplication();
Console.WriteLine( "Multiplication X [A]\n" );
displayMatrix( matrixC );
Utilities.lockScreen();
break;
}

// Do they want to go again?
if ( menuChoice != 0 )
{
// Give them another menu run...
menuChoice = ClassMenu.getMenuChoice();
}
}
Console.WriteLine( "\n" );
}

// ===== Method to display the contents of a matrix
public static void displayMatrix( int[,] synMatrixC )
{
for ( int row = 0; row <= Constants.numRows - 1; ++row )
{
for ( int col = 0; col <= Constants.numCols - 1; ++col )
{
Console.Write( "{0} \t", synMatrixC[row, col] );
}
Console.WriteLine( "\n" );
}
}
}
}

// This class stores the constants that are needed for the
// numbers of rows and columns
class Constants
{
// Set constants for this program
public const int numRows = 4;
public const int numCols = 4;
}

// This class takes care of the menu work
class ClassMenu
{
// ===== Method that displays the console menu to the user
public static void displayMenu()
{
Console.WriteLine( "Menu Choices:\n" );
Console.WriteLine( " (0) End execution of the program" );
Console.WriteLine( "\n" );
Console.WriteLine( " (1) Addition [A] + " );
Console.WriteLine( " (2) Addition + [A]" );
Console.WriteLine( " (3) Subtraction [A] - " );
Console.WriteLine( " (4) Subtraction - [A]" );
Console.WriteLine( " (5) Integer Scalar Multiplication s[A]" );
Console.WriteLine( " (6) Integer Scalar Multiplication s" );
Console.WriteLine( " (7) Multiplication [A] X " );
Console.WriteLine( " (8) Multiplication X [A]" );
Console.WriteLine( "\n" );
}

// ===== Method to get the menu choice from the user
public static int getMenuChoice()
{
// Local variables for this method
int localInt = 0;
bool localBoolean;

do
{
// Assume the suer follow directions...
localBoolean = true;

// Display the menu and the choices...
Utilities.cls( 0 );
ClassMenu.displayMenu();

// Make sure the user enters a byte
try
{
Console.Write( "Select a menu choice ( 0, 1, 2, ..., 8 ) --> " );
localInt = Convert.ToByte( Console.ReadLine() );
}
catch ( Exception error )
{
Console.WriteLine( "\n" );
Console.WriteLine( error.Message );
localBoolean = false;
Console.WriteLine( "\n" );
}

// Make sure the choice is within the menu range...
if ( localBoolean )
{
localBoolean = ( ( localInt >= 0 ) &&
( localInt <= 8 ) );
}
}while ( !localBoolean );

return localInt;
}
}

// This class takes care of getting an integer from the user
class InputClass
{
// ===== Method to get an integer from the user
public static int InputInt( string synPrompt )
{
// Local identifiers for this method
int initialValue = 0;
bool localBoolean;

do
{
// Assume the user follows direction...
localBoolean = true;
Utilities.cls( 0 );

// First make sure that the value is of type int...
try
{
Console.Write( synPrompt );
initialValue = Convert.ToInt32( Console.ReadLine() );
}
catch ( Exception error )
{
Console.WriteLine( "\n" );
Console.WriteLine( error.Message );
localBoolean = false;
Console.WriteLine( "\n" );
}
}while ( !localBoolean );

return initialValue;
}
}

// This class takes care of the actual matrix work
class Matrix
{
// Declare private fields
// Field to take care of either addition or subtraction of the matrices
private int[,] addORsub = new int[Constants.numRows, Constants.numCols];

// Field to take care of scalar multiplication
private int[,] scalarMultiply = new int[Constants.numRows, Constants.numCols];

// Field to take care of multiplication of the matrices
private int[,] multiplication = new int[Constants.numRows, Constants.numCols];

// ===== Method to initialize the field for the addition
// or subtraction of the matrices
public void InitAddORSub( int[,] first, int[,] second, char operation )
{
for ( int row = 0; row <= Constants.numRows - 1; ++row )
{
for ( int col = 0; col <= Constants.numCols - 1; ++col )
{
// If the user want to add the matrices...
if ( operation == 'a' )
{
addORsub[row, col] = first[row, col] + second[row, col];
}
// else they wish to subtract the matrices
else
{
addORsub[row, col] = first[row, col] - second[row, col];
}
}
}
}

// ===== Method to return the result of the addition or
// subtraction of the matrices
public int[,] EchoAddORSub()
{
return addORsub;
}

// ===== Method to initialize the field for the scalar
// multiplication of a matrix
public void InitScalarMultiply( int[,] sentMatrix, int sentScalar )
{
for ( int row = 0; row <= Constants.numRows - 1; ++row )
{
for ( int col = 0; col <= Constants.numCols - 1; ++col )
{
scalarMultiply[row, col] = sentScalar * sentMatrix[row, col];
}
}
}

// ===== Method to return the result of the scalar
// multiplication of a matrix
public int[,] EchoScalarMultiply()
{
return scalarMultiply;
}

// ===== Method to initialize the field for the multiplication
// of matrices
public void InitMultiplication( int [,] first, int[,] second )
{
for ( int row = 0; row <= Constants.numRows - 1; ++row )
{
for ( int col = 0; col <= Constants.numCols - 1; ++col )
{
// Reset matrix to its original value
multiplication[row, col] -= multiplication[row, col];

for ( int x = 0; x <= 3; ++x )
{
// Calculate matrix
multiplication[row, col] += first[row, x] * second[x, col];
}
}
}
}

// ===== Method to return the result of the multiplication
// of matrices
public int[,] EchoMultiplication()
{
return multiplication;
}
}

// This class takes care of utility work in the program
class Utilities
{
// ===== Method to clear the screen
public static void cls( int scroll )
{
for ( int lcv = 1; lcv <= ( 25 - scroll ); ++lcv )
{
Console.WriteLine( "\n" );
}
}

// ===== Method to lock screen
public static void lockScreen()
{
string holdIt;

Console.WriteLine( "\n" );
Console.WriteLine( "Press the Enter key to continue..." );
holdIt = Console.ReadLine();
}
}

-Spets
 
Back