w3resource

C Exercises: Print a matrix in spiral form


50. Spiral Matrix Print

Write a program in C to print a matrix in spiral form.

This task involves writing a C program to print a given matrix in spiral order. The program will take a 2D array as input, display the matrix in its original form, and then output the elements in a spiral traversal order starting from the top-left corner.

Visual Presentation:

C Exercises: Print a matrix in spiral form

Sample Solution:

C Code:

#include <stdio.h>

#define R 4 // Number of rows in the matrix
#define C 5 // Number of columns in the matrix

// Function to print the elements of the matrix in a spiral order
void spiralOfMatrix(int enrow, int encol, int arr1[R][C])
{
    int i, rowind = 0, colind = 0;

    // Loop through the matrix elements in a spiral pattern
    while (rowind < enrow && colind < encol)
    {
        // Print elements of the first row
        for (i = colind; i < encol; ++i)
        {
            printf("%d ", arr1[rowind][i]);
        }
        rowind++;

        // Print elements of the last column
        for (i = rowind; i < enrow; ++i)
        {
            printf("%d ", arr1[i][encol - 1]);
        }
        encol--;

        // Print elements of the last row if it is within matrix boundaries
        if (rowind < enrow)
        {
            for (i = encol - 1; i >= colind; --i)
            {
                printf("%d ", arr1[enrow - 1][i]);
            }
            enrow--;
        }

        // Print elements of the first column if it is within matrix boundaries
        if (colind < encol)
        {
            for (i = enrow - 1; i >= rowind; --i)
            {
                printf("%d ", arr1[i][colind]);
            }
            colind++;
        }
    }
}

int main()
{
    int i, j;
    int arr1[R][C] = {
        {1, 2, 3, 4, 5},
        {6, 7, 8, 9, 10},
        {11, 12, 13, 14, 15},
        {16, 17, 18, 19, 20}
    };

    // Print the original matrix
    printf("The given array in matrix form is:\n");
    for (i = 0; i < R; i++)
    {
        for (j = 0; j < C; j++)
        {
            printf("%d  ", arr1[i][j]);
        }
        printf("\n");
    }
    printf("\n");

    // Print the matrix in spiral order
    printf("The spiral form of the above matrix is:\n");
    spiralOfMatrix(R, C, arr1);
    return 0;
}

Sample Output:

The given array in matrix form is :  
1  2  3  4  5  
6  7  8  9  10  
11  12  13  14  15  
16  17  18  19  20  
The spiral form of above matrix is: 
1 2 3 4 5 10 15 20 19 18 17 16 11 6 7 8 9 14 13 12 

Flowchart:

Flowchart: Print a matrix in spiral form.

For more Practice: Solve these Related Problems:

  • Write a C program to print a matrix in spiral order and then reverse the spiral order.
  • Write a C program to display a matrix in spiral form using recursion.
  • Write a C program to output a spiral matrix and then rotate the matrix by 90 degrees.
  • Write a C program to print a matrix in spiral order and compute the sum of the spiral elements.

Go to:


PREV : Majority Element (General).
NEXT : Maximum Circular Subarray Sum.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.