A matrix is used in several areas in mathematics to organize information. In Data Management they are used to organize and manipulate data collected.

computer science


A matrix is used in several areas in mathematics to organize information.  In Data Management they are used to organize and manipulate data collected.  In Geometry and Discrete Math they are used to facilitate the solution of a linear system.  In all cases we can do the same operations on matrices.  

The definition of a matrix is a rectangular array of numbers set out in rows and columns.  The numbers are called elements.  A two-dimensional array!

Examples of matrices:

2x2 Matrix:                  Adding two matrices: 

Assignment must be done in steps.  Add the following functionality one at a time:

Part 1

 Write a program that:

declares two matrices (A and B) and populates the matrices by reading the data from the file "MatrixIn.txt". (Note: matrices are 2D arrays. Maximum dimensions of matrices are 5x5)

the file will contain the following content:

size of the first matrix (for example, 2 3 means a 2x3 matrix)

values for that matrix

size of the second matrix

values for that matrix

example "MatrixIn.txt" text file:

2 3

3 1 4

5 2 6

3 2

4 1

2 6

8 2

prints out the two matrices (after both 

are read in)

Part 2


declare output matrix (C). 

check whether the dimensions of A and B are the same. (compatible for addition)

add matrices A and B and put the result in C 

prints out the two input matrices 

prints out the resulting matrix

Part 3

Modify the program so that it can add, subtract or multiply the two matrices. THIS IS NOT A SIMPLE PROCESS. You need to learn how matrix multiplication works.

Create a user interface that asks the user if they would like to add, subtract or multiply the matrices. Think about making your program user friendly. (For example, display the contents of the matrices before asking them if they want to add, subtract or multiply.)

Verify that matrices are compatible (different logic than addition).  Test your program with provides test cases.  Verify with: http://www.bluebit.gr/matrix-calculator/matrix_multiplication.aspx

Write your output matrix to a text file as well as the screen.

Part 4

Possible extension options:

Create other operations (requires research.)

For information about Multiplying Matrices:




1. Good formatting, variable names and commenting.

2. Good input, good friendly prompts. 

3. Print out the full equation after entering (Follow Input, Process, Output) order.

4. Read from the specified file format and file "MatrixIn.txt". (Your program will be tested using teacher-created text files.) 


Instruction Files

Related Questions in computer science category