C++ Program to Print matrix in zag-zag fashion Last Updated : 27 Jan, 2022 Comments Improve Suggest changes Like Article Like Report Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: 1 2 3 4 5 6 7 8 9 Output: 1 2 4 7 5 3 6 8 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the direction according to the previous match. C++ /* C++ Program to print matrix in Zig-zag pattern*/ #include <iostream> using namespace std; #define C 3 // Utility function to print matrix // in zig-zag form void zigZagMatrix(int arr[][C], int n, int m) { int row = 0, col = 0; // Boolean variable that will true if we // need to increment 'row' value otherwise // false- if increment 'col' value bool row_inc = 0; // Print matrix of lower half zig-zag pattern int mn = min(m, n); for (int len = 1; len <= mn; ++len) { for (int i = 0; i < len; ++i) { cout << arr[row][col] << " "; if (i + 1 == len) break; // If row_increment value is true // increment row and decrement col // else decrement row and increment // col if (row_inc) ++row, --col; else --row, ++col; } if (len == mn) break; // Update row or col value according // to the last increment if (row_inc) ++row, row_inc = false; else ++col, row_inc = true; } // Update the indexes of row and col variable if (row == 0) { if (col == m - 1) ++row; else ++col; row_inc = 1; } else { if (row == n - 1) ++col; else ++row; row_inc = 0; } // Print the next half zig-zag pattern int MAX = max(m, n) - 1; for (int len, diag = MAX; diag > 0; --diag) { if (diag > mn) len = mn; else len = diag; for (int i = 0; i < len; ++i) { cout << arr[row][col] << " "; if (i + 1 == len) break; // Update row or col value according // to the last increment if (row_inc) ++row, --col; else ++col, --row; } // Update the indexes of row and col variable if (row == 0 || col == m - 1) { if (col == m - 1) ++row; else ++col; row_inc = true; } else if (col == 0 || row == n - 1) { if (row == n - 1) ++col; else ++row; row_inc = false; } } } // Driver code int main() { int matrix[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; zigZagMatrix(matrix, 3, 3); return 0; } Output: 1 2 4 7 5 3 6 8 9 Time complexity: O(n*m) Auxiliary space: O(1) Please refer complete article on Print matrix in zig-zag fashion for more details! Comment More infoAdvertise with us Next Article C++ Program to Print matrix in zag-zag fashion K kartik Follow Improve Article Tags : Matrix C++ Programs C++ DSA pattern-printing +1 More Practice Tags : CPPMatrixpattern-printing Similar Reads C++ Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16}; Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 3 min read C++ Program to Print matrix in snake pattern Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern. Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, { 2 min read C++ Program To Print Continuous Character Pattern Here we will build a C++ Program To Print Continuous Character patterns using 2 different methods i.e: Using for loopsUsing while loopsInput: rows = 5Output: A B C D E F G H I J K L M N O 1. Using for loopApproach 1: Assign any character to one variable for the printing pattern. The first for loop i 5 min read Program to print the given Z Pattern Given an integer N, the task is to print the Alphabet Z Pattern as given below: 1 2 3 * * * N * * * 3 2 1 2 3 * * * N Examples: Input: N = 5 Output: 1 2 3 4 5 4 3 2 1 2 3 4 5 Input: N = 4 Output: 1 2 3 4 3 2 1 2 3 4 Recommended: Please try your approach on {IDE} first, before moving on to the soluti 7 min read C++ Program To Print Number Pattern Here, we will see a C++ program to print the 3 different number patterns. There are 3 number patterns covered using for loop and while loop with their respective explanation.3 Different Number Patterns: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 1 12 123 1234 12345Pattern 1:In 6 min read Like