Print a given matrix in counter-clockwise spiral form in c++

In this tutorial, we will learn how to print a given matrix in the counter-clockwise spiral form in c++. Also, given an RxC matrix, we will print the matrix in the counter-clockwise spiral form.

Specifically, the main idea is:
Firstly, we have to read elements from the given matrix one by one and simply, print it in the spiral order anticlockwise. Moreover, for this order, we will use four loops each for top, bottom, left, right corner of the matrix.
In other words, every loop defines a direction along with the matrix.

  • The first for loop directs the movement from top to bottom.
  • The second for loop directs the movement from left to right.
  • The third for loop directs the movement from bottom to top.
  • The fourth for loop directs the movement from right to left.

Therefore, we will use four variables here.

  • a – starting row index
  • b – starting column index
  • c – ending row index
  • d – ending column index

You may also like:
Find the sum of each row and column of a matrix in C++



 

Print a matrix in the clockwise spiral form in C++

Below is the implementation of the above approach

#include<iostream>
#include <bits/stdc++.h> 
using namespace std; 
  
#define R 4 
#define C 4 
  
void counterclockspiral(int c,  
                             int d,  
                             int arr[R][C]) 
{ 
    int i, a = 0, b = 0; 
  
    //    a - starting row index 
    //    c - ending row index 
    //    b - starting column index 
    //    d - ending column index 
  
  
    // initialize 
    int count = 0; 
  
    // total number of elements in matrix 
    int total = c * d; 
  
    while (a < c && b < d)  
    { 
        if (count == total) 
            break; 
  
        // Print first column from remaining
        for (i = a; i < c; ++i) 
        { 
            cout << arr[i][b] << " "; 
            count++; 
        } 
        b++; 
  
        if (count == total) 
            break; 
  
        // Print the last row from the remaining rows  
        for (i = b; i < d; ++i)  
        { 
            cout << arr[c - 1][i] << " "; 
            count++; 
        } 
        c--; 
  
        if (count == total) 
            break; 
  
        // Print the last column from the remaining columns  
        if (a < c)  
        { 
            for (i = c - 1; i >= a; --i)  
            { 
                cout << arr[i][d - 1] << " "; 
                count++; 
            } 
            d--; 
        } 
  
        if (count == total) 
            break; 
  
        // Print the first row  from the remaining rows  
        if (b < d)  
        { 
            for (i = d - 1; i >= b; --i)  
            { 
                cout << arr[a][i] << " "; 
                count++; 
            } 
            a++; 
        } 
    } 
} 
  
int main() 
{ 
    int arr[R][C] = {{ 1, 2, 3, 4 }, 
                     { 5, 6, 7, 8 }, 
                     { 9, 10, 11, 12 }, 
                     { 13, 14, 15, 16 }}; 
    counterclockspiral(R, C, arr); 
    return 0; 
}

OUTPUT EXPLANATION:

OUTPUT: 1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7

Leave a Reply

Your email address will not be published. Required fields are marked *