Click Here to Download this Answer Instantly.

PIC 10A Homework 8


To gain more experience with 2-D arrays and file i/o.

Your Assignment

Your assignment is to implement matrix addition. Do not be intimidated if you have no experience with matrices. Instead of the word matrix just think 2-D array.

Description of how the program works

In main you should first ask the user to enter the dimensions of the matrix. (number of rows, number of columns). Then you should check that the number of rows and the number of columns given are valid (See below for what constitutes a valid number). If they are not, then ask the user to re-enter the dimensions. Next, ask the user if they would like to fill the matrices from a file or fill them with random values. If they choose the file option, prompt the user for a file name. Ask the user if the output should go to a file or to the console. If the user chooses output to a file, ask the user for a file name. After the matrices have been filled output the two matrices either to screen or to a file. Then perform the requested matrix operation and output the result to either the console or to a file as requested by the user. Note: Either fill both matrices from random values or both matrices from a file. Do not do a mizture of the two.

How to add two matrices

Say A and B are two matrices. In order to be able to add them, they both have to have the same number of rows and the same number of columns. For example, if A has 3 rows and 5 columns then B has to also have 3 rows and 5 columns. So, when the user enters the dimensions, we will just set both matrices to have the same dimensions. To add the two matrices, you just add corresponding entries. For example  

General advice

The user-entered matrices in this assignment will all be made of ints, and every entry has a value between -10 and 10. While we let the user enter the number of rows and number of columns, the maximum number of rows and number of columns will be 10. So, you may declare your 2-D arrays to be 10 by 10 arrays, but only use a part of the arrays. When you check that the user enters a proper number of rows and number of columns, you may assume that the user enters integers. Check that these integers are in the range [1,10]. For this assignment you may assume that you are the user. To test your program either create a file with some numbers in it, or have your program create this file. The idea is that if you choose to have your matrices randomly filled and SAVE to a file, then next time your program should be able to read the matrices from this file.


Your output should be clean, so use the setw command in an appropriate way when you output the matrix. Also make sure the newlines are in correct places. You do not have to output the matrices side-by-side. Instead you may output them as




That is, first output the matrix A then output matrix B and then the sum matrix C. A sample output for a 2×2 matrices is something like:

-5  7

 3  2

 0  -4

 6  1

-5  3

 9  3


You should write at least 5 functions. (1) A function that fills a single matrix with random values between -10 and 10; (2) a function that fills a single matrix with values from a file; (3) a function that adds two matrices (4) a function that outputs a single matrix to the console; and (5) a function that outputs a single matrix to a file. Note that I am not telling you here the types of the arguments or return values. You should figure out on your own the best implementation. As with any programming project, there are multiple ways of doing things. However one of the simpler ways is to open the file streams in main rather than in functions. ie. ask the user in main what file name is and open a file for writing in main. There is only one complication using this approach. See the discussion below for filling a matrix from a file.

Note on filling matrices

The best way to implement filling of matrices, is to pass to function a single matrix that you are trying to fill and the dimensions of the matrix. That way if I want to fill two matrices I can just call my function twice. For example, if I want to fill matrix A and matrix B, I can then call my fill function:



Now both of my matrices have been filled with random values.

Things ares slightly different for filling matrices from a file. If I opened the file for reading in main I also have to pass the ifstream object fin to the function.

// Open file here //

fill_from_file(A,row_size,col_size, fin);

fill_from_file(B,row_size,col_size, fin);

However, both matrices A and B would get the same values from the file!! This is because of the way file reading works. Every time I do fin >> number;, the next number is read from a file. There is a “file pointer” that is keeping track of where the next number to be read is. When I call the function fill_from_file the first time, the file pointer associated with fin is moved as it should be. However, these updates are done to the local copy of fin. So when I come back to main, fin file pointer is reset to the beginning of the file. So when the second call happens to fill_from_file, the matrix B gets the same values as A. This is easy to fix. When you write the function fill_from_file simply pass the ifstream object fin by reference. For example: