New: 214 pattern programs with tested code in C, C++, Java and Python Explore patterns

Wave Print of a Matrix (Column-Wise)

The wave print of a matrix reads it down the first column, up the second, down the third and so on. For the grid 1 to 16 the wave reads 1 5 9 13 14 10 6 2 3 7 11 15 16 12 8 4. It is a common coding-test question.

Quick answer: Loop over the columns. For an even column read the rows from top to bottom; for an odd column read them from bottom to top. Print each element as you reach it, all on one line. For the 4 × 4 grid the first column gives 1 5 9 13 and the second column, read upwards, gives 14 10 6 2.

This program is part of the Wave Patterns collection.

Table of contents8 sections
  1. Wave Print of a Matrix output
  2. How the wave print of a matrix works
  3. Wave Print of a Matrix in C
  4. Wave Print of a Matrix in C++
  5. Wave Print of a Matrix in Java
  6. Wave Print of a Matrix in Python
  7. Time and space complexity
  8. Frequently asked questions

Wave Print of a Matrix output

Output (4 × 4 matrix 1 to 16):

1 5 9 13 14 10 6 2 3 7 11 15 16 12 8 4
Wave Print of a Matrix layout diagram, row by row (4 × 4 matrix 1 to 16)
The numbers of the wave print of a matrix laid out row by row (4 × 4 matrix 1 to 16).

How the wave print of a matrix works

The traversal only changes direction between columns, so the outer loop goes over the columns and an if picks the direction of the inner loop. It is the reading counterpart of the column snake number square, which writes numbers in the same order.

  1. Fill the matrix with 1 to n² row by row.
  2. For each column j: if j is even, print rows 0 to n − 1; otherwise print rows n − 1 down to 0.
  3. End with a new line.

Key takeaways

  • The direction flips on every column.
  • The outer loop runs over columns, the inner loop over rows.
  • Every element is printed once, in a single line.
  • Swap the loops to get the row-wise wave: left to right, then right to left.

Wave Print of a Matrix in C

#include <stdio.h>

int main(void) {
    int n = 4;
    int a[4][4];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            a[i][j] = i * n + j + 1;

    for (int j = 0; j < n; j++) {
        if (j % 2 == 0)
            for (int i = 0; i < n; i++)             /* down this column */
                printf("%d ", a[i][j]);
        else
            for (int i = n - 1; i >= 0; i--)        /* up this column */
                printf("%d ", a[i][j]);
    }
    printf("\n");
    return 0;
}

Wave Print of a Matrix in C++

#include <iostream>
#include <vector>
using namespace std;

int main() {
    int n = 4;
    vector<vector<int>> a(n, vector<int>(n));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            a[i][j] = i * n + j + 1;

    for (int j = 0; j < n; j++) {
        if (j % 2 == 0)
            for (int i = 0; i < n; i++)             // down this column
                cout << a[i][j] << " ";
        else
            for (int i = n - 1; i >= 0; i--)        // up this column
                cout << a[i][j] << " ";
    }
    cout << endl;
    return 0;
}

Wave Print of a Matrix in Java

public class WavePrintMatrix {
    public static void main(String[] args) {
        int n = 4;
        int[][] a = new int[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = i * n + j + 1;

        for (int j = 0; j < n; j++) {
            if (j % 2 == 0)
                for (int i = 0; i < n; i++)             // down this column
                    System.out.print(a[i][j] + " ");
            else
                for (int i = n - 1; i >= 0; i--)        // up this column
                    System.out.print(a[i][j] + " ");
        }
        System.out.println();
    }
}

Wave Print of a Matrix in Python

n = 4
a = [[i * n + j + 1 for j in range(n)] for i in range(n)]

result = []
for j in range(n):
    rows = range(n) if j % 2 == 0 else range(n - 1, -1, -1)   # down even columns, up odd ones
    for i in rows:
        result.append(a[i][j])
print(" ".join(str(x) for x in result))

Time and space complexity

Every element is printed once, so the program runs in O(n²) time and O(1) extra memory (the Python version collects the output in a list).

Frequently asked questions

How do I print it row-wise instead?

Make the rows the outer loop and alternate the column direction: 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13.

Does it work for rectangular matrices?

Yes. Loop over the number of columns outside and the number of rows inside; nothing else changes.

Both read every element once in a fixed path. The spiral order goes round the edges; the wave goes up and down the columns.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 8 of 12 in Wave Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.