Question: For this homework assignment, you will write a program that will populate a file with an NxM matrix, then it will read the NxM matrix
For this homework assignment, you will write a program that will populate a file with an NxM matrix,
then it will read the NxM matrix in from the file and perform some basic mathematical operations on it
(calculate the mode, avg, and find the min, max, and median in the matrix). We will cover file I/O next
week. For your algorithm, it is sufficient to state that you will write a specific thing to a file. For example,
you could say something like:
Write: Answer is + var1 to file1
You should have a main script that is responsible for calling each of the functions necessary to
run the program. (Functions and script next week. No algorithm required for script.).
Required Functions
1. populate_matrix this function does not accept any parameters and does not return anything.
It should create a 2d matrix (use the randi function when you are writing your code next week)
that contains random numbers between 0 and 100. The dimensions should also be generated
randomly. Once the matrix is created, it should write the following to a text file:
a.
first line should contain dimensions (row size, followed by a space, followed by column
size)
b. each subsequent line should contain one row of the matrix until the entire matrix has
been written to file
2. process_file this function accepts 1 parameter ( a file name should be the file you created in
your populate_matrix function ) and outputs an NxM matrix (dimensions determined by reading
the file), plus it should output N and M (which are the row and column size). This function
should be able to read in a given file that is correctly formatted, extract all of the numbers in the
file and store them as an NxM matrix and return that matrix plus the dimensions of the matrix
to the user as return arguments.
3. calc_mode this function should accept an NxM matrix and it should output the mode of the
numbers in the matrix. Do NOT use matlabs built-in function to achieve this. You must code the
function yourself
a.
if you dont know what mode is, follow this link:
https://www.mathsisfun.com/mode.html
4. calc_avg this function should accept an NxM matrix and it should output the average of all of
the numbers in the matrix. Do NOT use matlabs mean function to do this. You must code this
yourself.
5. find_mmm this function should accept an NxM matrix and output (The output arguments
should be in this order) the minimum, maximum, and median values found in the matrix. Do
NOT use matlabs built-in functions to do this. You must write the code to do this yourself.
6. output_results this function should accept the following parameters (in this order): row_count,
col_count, mode, average, minimum, maximum, median). This function will print the following
items (in this order) into a text file. Each of the items must be on a separate line.
a.
dimensions of matrix:
N
x
M
(where N and M are the actual row/column count of matrix)
b. mode of matrix:
[whatever the mode is]
c.
average of matrix:
[whatever the average is]
d. min:
[whatever the min is]
e. max:
[whatever the max is]
f.
median:
[whatever the median is]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
