I have solved the problem on my own but the expected output doesn’t come.Plz help through this and review my code.Here is my code.
#include
using namespace std;
int rotateMatrix(int a[][10], int rows, int cols)
{
//1st step is to calculate the transpose of the matrix
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(i < j) //Here if the element of the row is less than the element of the column then only swapping is performed.
{
int temp = a[i][j];
a[i][j] = a[j][i];
a[j][i] = temp;
}
}
}
//2nd step is to either interchange the rows or to reverse the column.
//Here i can do the first method that is interchanging the column.
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < (cols/2); j++)
{
int temp = a[i][j];
a[i][j] = a[i][cols - 1 - j];
a[i][cols - 1 - j] = temp;
}
}
}
int main()
{
int rows, cols;
cin>>rows>>cols;
int a[10][10];
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
cin>>a[i][j];
}
rotateMatrix(a, rows, cols);
return 0;
}
}