Spiral Matrix Pattern Dry Run in C

Spiral Matrix Pattern is an interactive C dry run visualizer from the Patterns programs section. Study the source code, then use the execution controls to follow each step, variable update, highlighted line, and console output.

This page provides a browser-based dry run with source-code highlighting, auto-scroll, voice narration controls, and execution output for learning the program step by step.

Spiral Matrix Pattern Program Code

#include <stdio.h>
int main() {
    int n = 4, mat[4][4] = {0}, val = 1, i, j;
    int r1 = 0, r2 = 3, c1 = 0, c2 = 3;
    while(val <= 16) {
        for(i = c1; i <= c2; i++) mat[r1][i] = val++;
        r1++;
        for(i = r1; i <= r2; i++) mat[i][c2] = val++;
        c2--;
        for(i = c2; i >= c1; i--) mat[r2][i] = val++;
        r2--;
        for(i = r2; i >= r1; i--) mat[i][c1] = val++;
        c1++;
    }
    for(i = 0; i < 4; i++) {
        for(j = 0; j < 4; j++) printf("%2d ", mat[i][j]);
        printf("\n");
    }
    return 0;
}

View the complete Patterns programs page.

Program Console Spiral Matrix Pattern Topic: C Vignaankosh.com
Execution Panel
Step 0/0
Console is empty.