Note: I only need the algorithms this week for functions 1-6, Homework 3: Part 1
ID: 3603114 • Letter: N
Question
Note: I only need the algorithms this week for functions 1-6,
Homework 3: Part 1 – Part 2 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 I ONLY want the algorithms this week. You must include algorithms/psuedocode for each of your functions (so you should have 6 algorithms). (Algorithms this week). 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:
first line should contain dimensions (row size, followed by a space, followed by column size)
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 matlab’s built-in function to achieve this. You must code the function yourself if you don’t 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 matlab’s 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 matlab’s 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.
dimensions of matrix: N x M (where N and M are the actual row/column count of matrix)
mode of matrix: [whatever the mode is]
average of matrix: [whatever the average is]
min: [whatever the min is]
max: [whatever the max is]
median: [whatever the median is]
Deliverables for week 1 algorithms for 1 – 6
Explanation / Answer
1. populate_matrix
int n , m;
n = rand(1, MAX_INT);
m = rand(1, max_int);
int mat[n][m]; // dynamically declaring the matrix
for(int i=0;i<n;i++){
for(int j =0;j<m;j++){
mat[n][m] = rand(0,100); // generate random number between 0 -100
}
now create a file and write the data in the matrix to the file
2. process_file
read the file
File fp = fopen("input_file.txt");
first line represents the n and m values
string line = fp.readline()
// tokenize the line using space as delimiter to get n, m values
declare the matrix int mat[n][m]
read each line of the file till the end of the file and copy each line to the matrix
int i=0;
while(!EOF(fp)){
//read each line
string line = fp.readline();
//tokenize and values to the matrix
for(int j =0;i<token_size;j++){
mat[i][j] = token[j];
}
i++;
}
return mat, n , m
3. calc_mode
4. calc_avg
int total = 0;
for(i=0;i<n;i++){
for(j=0;i<m;j++){
total + = a[i][j];
}
}
float avg = float(total/(n+m));
5. find_mmm
sort all the elements in a matrix and store them in an array , first element is min, last element is max and middle element is median
6. output_results
printing(row_count, col_count, mode, average, minimum, maximum, median){
File fp =open("output.txt");
fp.seek(0);
fp.write(....); // use this to write into the file
fp.close() // close the file
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.