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: 10x10
1 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90
91 92 93 94 95 96 97 98 99 100
Reshaped Matrix: 5x20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
81 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:

Press + to interact
#include <iostream>
#include <iomanip>
using namespace std;
#define maxRows 100
#define maxCols 100
void 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.

// Prototype
bool 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 the mappedWorld. We will make a nested loop with the World matrix dimensions. The outer loop will start from 0 to rows-1, and the inner loop will start from 0 to cols-1.

  • We also have two variables for the mappedWorld matrix row and column indexes mri, and mci, respectively. Both of these indexes are initialized with 0. In each iteration of the nested loop, we will increment the column index mci, but row index mri will remain 0 and map the values of the World matrix to the first row of mappedWorld.

  • If a row of the mapped world is exhausted, which can be checked by comparing mci with mCols, we need to increment in row index mri, and reset the mci to 0.

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;
}


Reshaping the matrix

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;
}
Map 1-D to 2-D array