//prog1.cc
//brian jahns
//cs252 matrix driver
//C1999
#include <iostream.h>
#include "matrix.h"
//function prototypes
//subtract
void subtract();
//multiply
void multiply();
//transpose
void transpose();
int main()
{
int choice=0;
do
{
cout << "1 .. Subtract" << endl;
cout << "2 .. Multiply" << endl;
cout << "3 .. Transpose" << endl;
cout << "4 .. Quit" << endl;
cout << endl;
cout << "Select: ";
cin >> choice;
switch (choice) {
case 1:
subtract();
break;
case 2:
multiply();
break;
case 3:
transpose();
break;
case 4:
return 0;
break;
default:
cout << "Choose 1 through 4" << endl;
break;
}
}
while (choice != 4);
return 0;
}
void subtract()
{
int rows1, rows2 = 0;
int cols1, cols2 = 0;
//first matrix
cout << "Enter rows and columns: ";
cin >> rows1 >> cols1;
Matrix matrix1(rows1, cols1);
cout << endl << "Enter the matrix elements:" << endl;
cin >> matrix1;
cout << endl << endl;
//second matrix
cout << "Enter rows and columns: ";
cin >> rows2 >> cols2;
Matrix matrix2(rows2, cols2);
cout << endl << "Enter the matrix elements:" << endl;
cin >> matrix2;
cout << endl << endl;
//error check
if (rows1 == rows2 && cols1 == cols2)
{
//calculate the difference
Matrix result = matrix1 - matrix2;
//output difference
cout << "The difference:" << endl;
cout << result;
cout << endl << endl;
return;
}
else
{
cout << "Incompatible matrices: Subtraction not possible!" << endl;
return;
}
}
void multiply()
{
int rows1, rows2 = 0;
int cols1, cols2 = 0;
//first matrix
cout << "Enter rows and columns: ";
cin >> rows1 >> cols1;
Matrix matrix1(rows1, cols1);
cout << endl << "Enter the matrix elements:" << endl;
cin >> matrix1;
cout << endl << endl;
//second matrix
cout << "Enter rows and columns: ";
cin >> rows2 >> cols2;
Matrix matrix2(rows2, cols2);
cout << endl << "Enter the matrix elements:" << endl;
cin >> matrix2;
cout << endl << endl;
//error check
if (cols1 == rows2)
{
//calculate the product
Matrix result = matrix1 * matrix2;
//output product
cout << "The product:" << endl;
cout << result;
cout << endl << endl;
return;
}
else
{
cout << "Incompatible matrices: Multiplication not possible!" << endl;
return;
}
}
void transpose()
{
int rows1= 0;
int cols1= 0;
//matrix
cout << "Enter rows and columns: ";
cin >> rows1 >> cols1;
Matrix matrix1(rows1, cols1);
cout << endl << "Enter the matrix elements:" << endl;
cin >> matrix1;
cout << endl << endl;
//print
cout << "The transpose:" << endl;
cout << ~matrix1;
return;
}