Answer to Question #144668 in C for Unknown

Question #144668
Write a program to 4 X 4 matrix. Input the elements in the matrix and rotate the elements by one position of each row. A sample input and output is shown below:
I/P
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

O/P:
2 3 4 1
6 7 8 5
10 11 12 9
14 15 16 13
1
Expert's answer
2020-11-16T13:32:10-0500
void rotatematrix(int m, int n, int mat[R][C]) 
{ 
    int row = 0, col = 0; 
    int prev, curr; 
  
    /* 
       row - Staring row index 
       m - ending row index 
       col - starting column index 
       n - ending column index 
       i - iterator 
    */
    while (row < m && col < n) 
    { 
  
        if (row + 1 == m || col + 1 == n) 
            break; 
  
        // Store the first element of next row, this 
        // element will replace first element of current 
        // row 
        prev = mat[row + 1][col]; 
  
         /* Move elements of first row from the remaining rows */
        for (int i = col; i < n; i++) 
        { 
            curr = mat[row][i]; 
            mat[row][i] = prev; 
            prev = curr; 
        } 
        row++; 
  
        /* Move elements of last column from the remaining columns */
        for (int i = row; i < m; i++) 
        { 
            curr = mat[i][n-1]; 
            mat[i][n-1] = prev; 
            prev = curr; 
        } 
        n--; 
  
         /* Move elements of last row from the remaining rows */
        if (row < m) 
        { 
            for (int i = n-1; i >= col; i--) 
            { 
                curr = mat[m-1][i]; 
                mat[m-1][i] = prev; 
                prev = curr; 
            } 
        } 
        m--; 
  
        /* Move elements of first column from the remaining rows */
        if (col < n) 
        { 
            for (int i = m-1; i >= row; i--) 
            { 
                curr = mat[i][col]; 
                mat[i][col] = prev; 
                prev = curr; 
            } 
        } 
        col++; 
    } 
  
    // Print rotated matrix 
    for (int i=0; i<R; i++) 
    { 
        for (int j=0; j<C; j++) 
          cout << mat[i][j] << " "; 
        cout << endl; 
    } 
} 
  
/* Driver program to test above functions */
int main() 
{ 
    // Test Case 1 
    int a[R][C] = { {1,  2,  3,  4}, 
        {5,  6,  7,  8}, 
        {9,  10, 11, 12}, 
        {13, 14, 15, 16}  }; 
  
    // Tese Case 2 
    /* int a[R][C] = {{1, 2, 3}, 
                      {4, 5, 6}, 
                      {7, 8, 9} 
                     }; 
     */  rotatematrix(R, C, a); 
    return 0; 
} 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS