'Printing a matrix in spiral order but getting an additional element

#include<bits/stdc++.h>
using namespace std;

// Printing a matrix in spiral Order

int main()
{
    int n, m;
    cin>>n>>m;

    int arr[n][m];
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            cin>>arr[i][j];
    }

    // Spiral Order Print

    int row_start = 0, row_end = n-1, column_start = 0, column_end = m-1;

    while(row_start<=row_end && column_start<=column_end)
    {
        //For row_start
        for(int col=column_start; col<=column_end; col++)
            cout<<arr[row_start][col]<<" ";
        row_start++;
        
        cout<<endl;
        
        //For column_end
        for(int row = row_start; row<=row_end; row++)
            cout<<arr[row][column_end]<<" ";
        
        column_end--;
        
        cout<<endl;
        
        //For row_end
        for(int col = column_end; col>=column_start; col--)
            cout<<arr[row_end][col]<<" ";
        
        cout<<endl;
        
        row_end--;
        
        
        //For column_start
        for(int row=row_end; row>=row_start; row--)
            cout<<arr[row][column_start]<<" ";
        
        column_start++;

        cout<<endl<<endl<<endl;
    }


    return 0;
}

Input - 5 6

matrix -

1 5 7 9 10 11

6 10 12 13 20 21

9 25 29 30 32 41

15 55 59 63 68 70

40 70 79 81 95 105

//------------------------------------------------------------------//

Output -

1 5 7 9 10 11

21 41 70 105

95 81 79 70 40

15 9 6

10 12 13 20

32 68

63 59 55

25

29 30

29

I am printing a matrix in spiral order but am getting this last element, i.e., 29 twice. I cannot get to know why is it iterating through it again when all elements are done.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source