Problem Solving: Reshaping the Matrix
Learn to solve different matrix problems by reshaping the matrix.
Reshaping the matrix
Given a matrix of dimensions m x n, reshape it to the provided dimensions p x q given that m x n = p x q. Otherwise, display an error.
Example
Original Matrix: 10x101 2 3 4 5 6 7 8 9 1011 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 3031 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 5051 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 7071 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 9091 92 93 94 95 96 97 98 99 100Reshaped Matrix: 5x201 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2021 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 4041 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 6061 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 8081 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
The reshaped matrix should have all elements of the original matrix. If the elements of the original matrix do not fit into the reshaped matrix, then the program should display a message that mapping is impossible.
The following illustration depicts how a 3 x 4 matrix gets reshaped to 2 x 6:
Your implementation
Write your implementation in the widget below:
#include <iostream>#include <iomanip>using namespace std;#define maxRows 100#define maxCols 100void init(int World[maxRows][maxCols], int rows, int cols){// Write your code here}bool matrixReshape(int World[][maxCols], int rows, int cols, int mappedWorld[][maxCols], int &mRows, int &mCols){// Write your code here}// This function will be called for both the original World// and also for the mappedWorld.void print2DWorld(const char Msg[], int TDA[maxRows][maxCols], int rows, int cols){cout << Msg;for(int ri=0; ri<rows; ri++){for(int ci=0; ci<cols; ci++){cout << setw(3)<<TDA[ri][ci]<<" ";}cout << endl;}}int main(){int World[maxRows][maxCols], rows=10, cols = 10;init(World, rows, cols);print2DWorld("Original World:\n\n", World, rows, cols);int mappedWorld[maxRows][maxCols], mapRows = 5, mapCols=20;if(!matrixReshape(World, rows, cols, mappedWorld, mapRows, mapCols)){cout << "\n\nReshaping was not possible...!"<<endl;}else{print2DWorld("\n\nReshaped World:\n\n", mappedWorld, mapRows, mapCols);}return 0;}
Implementation walkthrough
From here onward, we provide you with a guide on implementing the solution for reshaping the matrix problem that you can follow along in the playground above. The guide contains both an explanation and the implementation of the functions. You should look at the solution if you are stuck. Best of luck!
Initializing the two-dimensional world
For testing, let's create a function, init()
, that takes three parameters – a two-dimensional matrix, World
, and its dimension, rows
and cols
. This function should initialize the array with integers. Here's a sample program; you may change it in whatever way you prefer.
void init(int World[][maxCols], int rows, int cols){int val = 1;for(int r=0; r<rows; r++){for(int c=0; c<cols; c++)World[r][c] = val, val++;}}
Instruction: Write the initialization code in your playground. You can have a different implementation as well.
Algorithmic details of reshaping the matrix
For reshaping the matrices, let's implement a function, matrixReshape()
, in which we will receive two matrices as parameters – World
with dimensions rows
, cols
, and mappedWorld
with dimensions, mrows
and mcols
.
// Prototypebool matrixReshape(int World[][maxCols], int rows, int cols,int mappedWorld[][maxCols], int &mRows, int &mCols)
Inside the function, we will do the following:
As we have dimensions of both matrices. First, we will compare both dimensions by multiplying their rows and columns. If the sizes of the two matrices are not the same, we should immediately return
false
, because reshaping is impossible. If both multiplication results are the same, we can reshape the matrix, so in that case, we proceed to the next step.Now, we want to map each value of the
World
matrix to themappedWorld
. We will make a nested loop with theWorld
matrix dimensions. The outer loop will start from0
torows-1
, and the inner loop will start from0
tocols-1
.We also have two variables for the
mappedWorld
matrix row and column indexesmri
, andmci
, respectively. Both of these indexes are initialized with0
. In each iteration of the nested loop, we will increment the column indexmci
, but row indexmri
will remain0
and map the values of theWorld
matrix to the first row ofmappedWorld
.If a row of the mapped world is exhausted, which can be checked by comparing
mci
withmCols
, we need to increment in row indexmri
, and reset themci
to0
.
Instruction: Write your implementation by following the directions given above. If you are stuck, look at the hint.
Complete implementation of reshaping the matrix
#include <iostream> #include <iomanip> using namespace std; #define maxRows 100 #define maxCols 100 bool matrixReshape(int World[][maxCols], int rows, int cols, int mappedWorld[][maxCols], int &mRows, int &mCols); void init(int World[maxRows][maxCols], int rows, int cols); void print2DWorld(const char Msg[], int TDA[maxRows][maxCols], int rows, int cols); int main() { int World[maxRows][maxCols], rows=10, cols = 10; init(World, rows, cols); print2DWorld("Original World:\n\n", World, rows, cols); int mappedWorld[maxRows][maxCols], mapRows = 5, mapCols=20; if(!matrixReshape(World, rows, cols, mappedWorld, mapRows, mapCols)) { cout << "\n\nReshaping was not possible...!"<<endl; } else { print2DWorld("\n\nReshaped World:\n\n", mappedWorld, mapRows, mapCols); } return 0; }
Exercise: Mapping a 1-D array to a 2-D array
Create a function, mapping()
, in which you pass singleDimWorld[]
(a 1-D array) with size
and twoDimWorld
(a 2-D array) with the dimension of rows
and cols
. You have to map the 1-D array to a 2-D array.
Your implementation
Write your implementation here:
#define maxRows 100 #define maxCols 100 #define max_capacity 1000 #include <iostream> #include <iomanip> using namespace std; bool mapping(int singleDimWorld[], int size, int twoDimWorld[][maxCols], int &rows, int &cols) { // Write your code: } void print1DWorld(const char Msg[], int projectionMap[max_capacity], int size) { cout << Msg; for(int mi=0; mi<size; mi++) { cout << projectionMap[mi]<<" "; } } void init(int A[], int size) { int val = 1; for(int i=0; i<size; i++) { A[i] = val++; } } void print2DMappedWorld(const char Msg[], int TDA[maxRows][maxCols], int rows, int cols) { cout << Msg; for(int ri=0; ri<rows; ri++) { for(int ci=0; ci<cols; ci++) { cout << setw(3)<<TDA[ri][ci]<<" "; } cout << endl; } } int main() { int A[max_capacity]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}, size=15; int twoDimWorld[maxRows][maxCols], rows=3, cols=4; mapping(A, size, twoDimWorld, rows,cols); print1DWorld("\nOne-dimensional world:\n", A, size); if(mapping(A, size, twoDimWorld, rows,cols)) { print2DMappedWorld("\n\nTwo-dimensional mapped world:\n", twoDimWorld, rows, cols); } else { cout << "\n\n*** Mapping was not possible...!!! \n"; } return 0; }