Advertisement
akosiraff

Homework 8

Mar 13th, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/homework-8/
  3. 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.
  4. Description of how the program works
  5. 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.
  6. How to add two matrices
  7. 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
  8. General advice
  9. 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].
  10. Output
  11. 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
  12. A
  13. B
  14. C
  15. 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:
  16. -5 7
  17. 3 2
  18. 0 -4
  19. 6 1
  20. -5 3
  21. 9 3
  22. Functions
  23. 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 XXXXX XXXXX 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.
  24. Note on filling matrices
  25. 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:
  26. fill_random(A,row_size,col_size);
  27. fill_random(B,row_size,col_size);
  28. Now both of my matrices have been filled with random values.
  29. 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.
  30. // Open file here //
  31. fill_from_file(A,row_size,col_size, fin);
  32. fill_from_file(B,row_size,col_size, fin);
  33. 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 XXXXX 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:
  34. void fill_from_file(int matr
  35.  
  36. Download: http://solutionzip.com/downloads/homework-8/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement