Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3 41 42 43 44 45 So the value at row:2, column:3 is 34 (rows and columns both start at 0) However, there is a

image text in transcribedimage text in transcribed

3 41 42 43 44 45 So the value at row:2, column:3 is 34 (rows and columns both start at 0) However, there is a fairly easy way to translate between a 2D vector as shown above and a ID vector. Below is a 1D vector representation of the matrix above with the 1D indicies in bold. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 11 12 13 14 15 21 22 23 24 25 31 32 33 34 35 41 42 43 44 45 The question is really, how to translate row:2, column:3 to a 1D vector index and how to translate a ID vector index back to a row/column format. To do this we have to know the number of columns in the 2D vector. In this particular case, 5 columns. row-index/5 (integer division), where 5 is the number of columns. column index%5 (modulus), where 5 is the number of columns index- row 5+column, where 5 is the number of columns For the example, row: 2 column: 3 is index (2 * 5) + 3 13 index 13 is row-13/5 2. column 13%5 3 Program Specifications Each of the functions below is doing calculations on a 2D vector using a 1D vector as the underlying parameter that is passed. The parameters are passed as const references: references to prevent copying but const to prevent modification of the parameter function: ostreams print_vector (const vector&v, ostream& out) print the vector to the provided stream, returning that stream. Each element of the vector is separated from the next by a comma, no comma after the last element. You print out to the provide ostream, not cout! Look at the Mimir tests for examples. You return the stream you passed in!! function: long four_corner_sum (const vectorlong> v, int rows, int cols) return the sum of the 4 corners of the provided 1D vector, interpreted as a 2D matrix with provided row and column count. For the matrix above, it would return 11+15+41+45112 Error Check: if there are less than either two rows or two columns (thus no corners), return 0 function: column vector& rows, int cols) vector order (const v, int return a new vector that is a reordering of the original 1D vector in column order. Collect each column, first to last, and within the column in row order 0 11 12 13 14 15 1 21 22 23 24 25 11,21,31,41,12, 22, 32,42,13,23, 33, 43, 14, 24, 34,44, 15, 25, 35, 45 2 31 32 33 34 35 3 41 42 43 44 45 function: vector rotaterows_up (const vector& v, int rows, int cols) return a new vector that is a rotation of the 2D matrix rows up by one. Here, the 2d row becomes the 1, the 3d the 2d etc. and the 1st row becomes the last. 0 11 12 13 14 15 1 21 22 23 2425 2 31 32 33 34 35 341 42 434445 0 21 22 23 24 25 1 31 32 33 34 3 2 41 42 43 44 45 3 11 12 13 14 15 21, 22,23, 24,25, 31,32, 33, 34, 35, 41, 42,43, 44,45,11,12, 13,14, 15 Error Check: If there are less than two rows, return an empty vector function; vector matrix_ccw_rotate (const vector& v return a new 1D vector that is a rotation of the entire 2D matrix counter-clockwise 90 degrees. int rows, int cols) 0 11 12 13 14 15 1 21 22 23 2425 2 31 32 33 34 35 3 41 4243 4445 0 15 25 35 45 1 14 24 34 44 13 23 33 43 3 12 2232 42 4 11 21 31 41 15, 25,35,45,14,24,34, 44,13, 23,33, 43,12, 22, 32, 42,11, 21,31, 41 function: long max column diff (const vector& v int rows, int cols) Take the sum of each column. Given those column sums find the maximum difference between any of the two column sums in the matrix. Return the max difference. For our example, the max diff is between column 0 (sum 104) and column 4 (sum 120) 16. function: long trapped vals (const vector& v, int rows, int cols) We are looking to see if any of values in the 2D matrix have 4 neighbors (up, down, left, right) that are all greater than that value. If so, the value is trapped. We return the number of trapped values. For this, we assume that the 2D matrix displays "wrap-around" behavior. Easier to show than explain. 011 12 13 14 15 1 21 22 23 24 25 2 31 32 3334 35 3 41 42434445 ,-8 1 2 3 4 011 12 13 14 15 11 21 22 23 24 25 2 31 32 33 34 35 3 41 42 43 44 45 The value at (0,0), index 0, is 11. We are checking the 4 neighbors (up, down, left, right) of that value to see if they are all greater than 1 1. The value to the right is (0.1), ndex I, is 12. The value below at (1,0), index 5, is 21. The value to the left, well there is no value to the left. But we assume the rows wrap around. Thus the a column left of (0,0), index 0 is (0,4), index 4, value 15. The value above, well there is no value above. But we assume the columns also wrap around. Thus a row up from (0,0), index 0, is (3,0) index 15, value 41 11 at (0,0), index 0 is a trapped value. It is the only trapped value in this example Deliverables You will turn in one file: proj06_functions.cpp. We provide you only with proj06 functions.h, you must write your own main to test your functions. Mimir can test the individual functions without a main program but it's a good idea for you to test your own code with a main, perhaps in the manner that we did previously

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions