## Matrix Calculator, C++ Program by David Woodford

Ive been learning c++ and doing matrices in maths as well so I’ve decided to combine to the two in a simple command based program. The basic idea of the program is that it can quickly multiply matrices together and perform reflection and rotation transformations on them. Below is a download of the program and the source code. In the following weeks ill also make some tutorials on doing matrices by hand (click here for my multiplication lesson). Later im hoping to add some more features to it like finding the inverse matrix and the determinate

So how to use the program, well first of all type help. This will then display a list of the commands and what they do. When using it type your command, eg mlt, on its own, the program will then ask you to enter which is the first matrix and the second matrix in the multiplication. Every value should have the enter key pressed after its typed.

The program allows up to 10 matrices to be stored in memory plus the answer to the last calculation. They are stored in an array and are given numerical values. The first one you define, using dim, is matrix 0 the second is matrix 1 etc. The answer to the last calculation is matrix 10. When the program ask you which matrix you want to use it is asking you to enter this value for the matrix

Using the let command allows you to give one matrix the value of another. The first matrix number you enter is the one that the values being assigned to, the second is the value thats being assigned. This command is useful if you want to store the answer, after youve done the next calculation, to do this type let, when prompted type matrix that you want to be given the value of the answer and then give the value 10 for the value its being assigned.

Below is the code, i wrote it in visual c++ 2008, for the program. You may have to make changes if you using a diffrent complier to the includes at the top.

Enjoy using, but only to check, note all angles are in degrees recomplie if you want radians.

// matrix.cpp : main project file.

#include “stdafx.h”

#include <iostream>

#include <cmath>

#include <string>

using namespace std;

class mat

{

public:

double mata[20][3];

int lgth;

void dimmat()

{

cout << “enter length of matrix” << endl;

cin >> lgth;

// cout << “” << endl;

for(int n=0;n<3;n++)

{

cout << “enter values for row” << n << ” , each followed by ‘enter’:” << endl;

for(int m=0;m<lgth;m++)

{

cin >> mata[n][m];

}

}

}

void display()

{

int rcount = 0;

int ccount = 0;

while(rcount < 3)

{

while(ccount < lgth)

{

//cout << “matrix:”<<endl;

cout << mata[rcount][ccount] << ” , “;

ccount++;

}

cout << ‘\n’;

ccount=0;

rcount++;

}

}

/*void rotate()

{

mat mat2;

mat2.dimmat();

mat mat3;

mat3 = mult(this, mat2);

}*/

};

mat mult(mat A, mat B)

{

cout << “multiply started” << endl;

//char pause;

mat ans;

ans.lgth=B.lgth;

int rcount = 0;

int ccount = 0;

int c2 = 0;

while(rcount < 3)

{

//cout << “row: ” << rcount << ” started” << endl;

while(ccount < A.lgth)

{

//cout << “collum: ” << ccount << “started” << endl;

ans.mata[rcount][ccount] = 0;

while(c2 < 3)

{

ans.mata[rcount][ccount] = ans.mata[rcount][ccount] + (A.mata[rcount][c2] * B.mata[c2][ccount]);

c2++;

}

//cout << “value is: ” << ans.mata[rcount][ccount] << endl;

//cin >> pause;

c2=0;

ccount++;

}

ccount=0;

rcount++;

}

int smelly;

ans.display();

return ans;

}

mat rotate(mat A, int angle)

{

mat T;

T.lgth = 3;

mat matans;

//creat transformation matrix

double pi = 3.14159265;

double theta = (angle*pi)/180;

T.mata[0][0] = cos(theta);

T.mata[0][1] = 0 – sin(theta);

T.mata[0][2] = 0;

T.mata[1][0] = sin(theta);

T.mata[1][1] = cos(theta);

T.mata[1][2] = 0;

T.mata[2][0] = 0;

T.mata[2][1] = 0;

T.mata[2][2] = 1;

matans = mult(T, A);

return matans;

}

mat reflect(mat A, int angle)

{

mat matans;

mat T;

T.lgth = 3;

//creat transformation matrix

double pi = 3.14159265;

double theta = (angle*pi)/180;

T.mata[0][0] = cos(2 * theta);

T.mata[0][1] = sin(2 * theta);

T.mata[0][2] = 0;

T.mata[1][0] = sin(2*theta);

T.mata[1][1] = 0 – cos(2*theta);

T.mata[1][2] = 0;

T.mata[2][0] = 0;

T.mata[2][1] = 0;

T.mata[2][2] = 1;

matans = mult(T, A);

return matans;

}

void input()

{

string dim = “dim”;

string com;

int end = 0;

mat matans;

int matcount = 0;

mat mats[11];

while(end == 0)

{

cout << “enter command>”;

getline(cin, com);

if(dim == com)

{

cout << “matrix” << matcount <<endl;

mats[matcount].dimmat();

matcount++;

}

if(com == “rot”)

{

cout << “which matirx?” <<endl;

int matnum;

cin >> matnum;

cout << “what angle (degrees)” << endl;

int rotang;

cin >> rotang;

matans = rotate(mats[matnum], rotang);

mats[10] = matans;

}

if(com == “rlt”)

{

cout << “which matirx?” <<endl;

int matnum;

cin >> matnum;

cout << “what angle (degrees)” << endl;

int rotang;

cin >> rotang;

matans = reflect(mats[matnum], rotang);

mats[10] = matans;

}

if(com == “ans”)

{

matans.display();

}

if(com == “mlt”)

{

cout << “first matrix” << endl;

int mat1;

cin >> mat1;

cout << “second matirx” << endl;

int mat2;

cin >> mat2;

matans = mult(mats[mat1], mats[mat2]);

mats[10] = matans;

}

if(com == “dsp”)

{

cout << “which matirx?” << endl;

int matdsp;

cin >> matdsp;

mats[matdsp].display();

}

if(com == “let”)

{

cout << “which matirx?” << endl;

int mat1;

cin >> mat1;

int mat2;

cout <<“eaqual to (10 is answer matrix)” << endl;

cin >> mat2;

mats[mat1] = mats[mat2];

}

if(com == “help”)

{

cout <<“Davids Woodfords matrix calculator” << endl;

cout << “takes the following commands” << endl;

cout <<” ‘dim’ :: allows you to dfine a matrix”<< endl;

cout <<” ‘rot’ :: roates a matrix through an agnle” << endl;

cout <<” ‘rlt’ :: reflects a matrix through the line y=tan(a) where a is given” << endl;

cout <<” ‘ans’ :: displays the answer to the last calculation” << endl;

cout <<” ‘mlt’ :: lets u multiply 2 matricies together” << endl;

cout <<” ‘dsp’ :: displays a matrix specified”<<endl;

cout <<” ‘let’ :: allows u to assign one matrix the value of another, eg answer”<<endl;

cout<<“===================================================================”<<endl;

cout <<“matricies are sotred in an array of 10, with numerical values starting at 0″<<endl;

cout<<“matrix 10 is the answer matrix”<<endl;

cout<<“any parameters will be asked for wen needed”<<endl;

}

}

}

int main()

{

cout << “Welcome to David Woodfords Matrix calculator” << endl << endl;

cout<<“type ‘help’ for a list of commands” <<endl;

// mat mata;

// mata.dimmat();

/* mat matb;

matb.dimmat();

mat matans;

matans = mult(mata, matb);

// mata.display();

*/

//rotate(mata, 30);

input();

return 0;

}

## Quadratic Formula

The quadratic formula is a quick(unless you can factorise) way of solving quadratic equations. You basically take the coefficient’s of x, x^{2} and numbers, put then in the formula, work out the two answers and have your 2 solutions of x. And if that wasn’t easy enough written a console program in c++ that will solve them for you(and gives dodgy answers for complex solution ie) imaginary answers when there are no real roots.

so for the general quadratic

ax^{2}+bx+c=0

So what do you do, well enter a, b and c from the general equation into the formula, work out the answers and they are your solutions.

Whats the plus/minus thingy. You might be wondering what the thing is after the -b, well its a plus or minus sign. Because when you work out a square root it can have to answers, eg)root 9 = +3 and -3, he formula takes this into account by saying you must use the plus and the minus answers. This therefore means you will be 2 solutions to the quadratic, which makes sense as the graph is a curve and it therefore must cut the x-axis twice.

When the root part is negative(before you find the root) there are no real roots, only complex ones. This means that the curve comes down above the x-axis and doesn’t cut it. If you work out the complex roots, using i for root(-1), your pair of answers will be a conjugate pair.

**David Woodfords Quadratic Calculator**

This is a console program that i wrote in c++ that will solve a quadratic for you. Just download the file and run it, follow the instructions and it will output the 2 answers for you. I tried to make it work for complex roots as well but somewhere in the decimal data types Ive messed up so only the second parts of the complex answers are correct – though im sure working out -b/2a isn’t too hard for you.

Download quadratic calculator

### Other posts relating to the quadratic formula

**Derive the Quadratic formula**: an explanation of how to derive and prove the quadratic formula by completing the square**Factorise Quadratics**: how to solve a quadratic without using the formula

## Comments