0

i need to create ad 2d dynamic array and then create a function that will receive the 2d dynamic array and then rotate it 90 degrees clockwise and return it back to main. However i am not sure why i am not getting any output? is it because of the wrong swapping technique? i figured that the indices swapping is as follows:

I J-----------I J
0 0           0 1
0 1           1 1
0 2           2 1

with that i came with :

for (int i = 0; i <row;i++)

    for(int j = 0;j<col; j++)
    {
        Roti[i+j][row-i]=arr[i][j];

    }

Code:

#include <iostream>
using namespace std;
void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int row, int col)
{
    int **Roti = new int *[row];

    for (int i = 0; i <row;i++)
    {
        Roti[i] = new int [col];
    }


    for (int i = 0; i <row;i++)

        for(int j = 0;j<col; j++)
        {
            Roti[i+j][row-i]=arr[i][j];

        }
    return  Roti;
}

int main()
{
    int *A[3];
    for (int i = 0; i < 3; i++)
    {
        A[i] = new int[3];
        for (int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 20;
        }
    }
    cout << "The array is :\n";
    print2DArray(A, 3, 3);
    int **ptr;
    ptr=Rotate(A,3,3);
cout<<"-----"<<endl;
    print2DArray(ptr, 3, 3);




    for (int i = 0; i < 3; i++)
    {
        delete[] A[i];
    }
    return 0;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    Have you tried solving it on paper first? If not do that first. And I also recommend that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Run your program in a debugger, to see if it crashes. If it does then the debugger will stop at the location of the crash, letting you know when and where it happened and also letting you examine variables to make sure they are okay. If there's no crash then step through the code line by line to make sure your on-paper solution is implemented correctly and does what you think it should do. – Some programmer dude Feb 20 '19 at 06:26
  • By the way, you have a memory leak. Also please take some time to read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask), as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Then try to improve you question, for example by telling us the expected output versus the actual output, or if you have a crash then give us details about that. – Some programmer dude Feb 20 '19 at 06:28

2 Answers2

1

Hopefully by "rotation" you did not mean the regular transpose because there are many answers for that already on stackoverflow. What is the fastest way to transpose a matrix in C++?

For rotation of the matrix, I modified your code to make it work as shown below. The logic is simply that the first row of the original matrix becomes the last column of the rotated matrix and using this logic, you come up with the the following code. Hope it helps.

#include <iostream>
using namespace std;

void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int num_rows, int num_cols);

int main()
{
    int num_rows = 3;
    int num_cols = 3;
    int** A_matrix = new int*[num_rows];
    for (int i = 0; i < num_rows; i++)
        A_matrix[i] = new int[num_cols];

    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_cols; col++)
        {
            A_matrix[row][col] = rand() % 20;
        }
    }

    cout << "The array is :\n";
    print2DArray(A_matrix, 3, 3);

    int** roated_matrix;
    roated_matrix = Rotate(A_matrix, num_rows, num_cols);
    cout << "Rotated array:" << endl;
    print2DArray(roated_matrix, 3, 3);


    return 0;
}

int **Rotate(int **arr, int num_rows, int num_cols)
{
    /* Rotated matrix will have dimensions reverse as well. 
       That's why we have rows and cols reversed in the following lines */
    int** rotated_matrix = new int*[num_cols];
    for (int i = 0; i < num_cols; i++)
        rotated_matrix[i] = new int[num_rows];


    for (int row = 0; row < num_rows; row++)
    {
        int col_rotated_matrix = num_rows - 1 - row; // 1st row shall be copied to the last column and so on
        for (int col = 0; col < num_cols; col++)
        {
            int row_rotated_matrix = col; // rows become columns in the rotated matrix
            rotated_matrix[row_rotated_matrix][col_rotated_matrix] = arr[row][col];
        }
    }
    return rotated_matrix;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

It produces the following output:

The array is :
1 7 14
0 9 4
18 18 2
Rotated array:
18 0 1
18 9 7
2 4 14
Frida Schenker
  • 1,219
  • 1
  • 9
  • 14
  • is it possible to modify it to rotate an array of any size? – user10798572 Feb 20 '19 at 15:17
  • 1
    You can use this code to rotate an array of any size. However, please modify the call to `print2DArray` function. Use `print2DArray(A_matrix, num_rows, num_cols);` for the normal array, and `print2DArray(roated_matrix, num_cols, num_rows);` for the rotated array. Kindly note that dimensions are reversed for the rotated array. – Frida Schenker Feb 21 '19 at 17:35
0

Try Using this;

for (int i = 0; i <row;i++)
k = row.length -1 ;
    for(int j = 0;j<col; j++)
    {
        Roti[j][k]=arr[i][j];

    }
}
Syed Mehtab Hassan
  • 1,297
  • 1
  • 9
  • 23