w3resource

C Exercises: Print a matrix in spiral form

C Array: Exercise-50 with Solution

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.

C Programming Code Editor:

Previous: Write a program in C to find majority element of an array.
Next: Write a program in C to find the maximum circular subarray sum of a given array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/array/c-array-exercise-50.php