Answered step by step
Verified Expert Solution
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
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started