
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Print Matrix in Z form?
Here we will see how to print the matrix elements in Z form. So if the array is like below −
5 8 7 1 2 3 6 4 1 7 8 9 4 8 1 5
Then it will be printed like: 5, 8, 7, 1, 6, 7, 4, 8, 1, 5
Algorithm
printMatrixZ(mat)
Begin print the first row i := 1, j := n-2 while i < n and j >= 0, do print mat[i, j] i := i + 1, j := j - 1 done print the last row End
Example
#include<iostream> #define MAX 4 using namespace std; void printMatrixZ(int mat[][MAX], int n){ for(int i = 0; i<n; i++){ cout << mat[0][i] << " "; } int i = 1, j = n-2; while(i < n && j >= 0){ cout << mat[i][j] << " "; i++; j--; } for(int i = 1; i<n; i++){ cout << mat[n-1][i] << " "; } } main() { int matrix[][MAX] = {{5, 8, 7, 1}, {2, 3, 6, 4}, {1, 7, 8, 9}, {4, 8, 1, 5} }; printMatrixZ(matrix, 4); }
Output
5 8 7 1 6 7 4 8 1 5
- Related Articles
- Python Program to Print Matrix in Z form
- Java Program to Print Matrix in Z form
- Swift Program to Print Matrix numbers containing in Z form
- Program to Print the Squared Matrix in Z form in C
- Java program to print a given matrix in Spiral Form.
- Print matrix in antispiral form
- Print a matrix in Reverse Wave Form in C++
- Print a given matrix in zigzag form in C++
- Program to print a matrix in Diagonal Pattern.
- Print a given matrix in reverse spiral form in C++
- Python Program to Print an Identity Matrix
- Golang Program to Print an Identity Matrix
- Swift program to Print an Identity Matrix
- Swift Program to Print Diagonal Matrix Pattern
- Golang program to print right diagonal matrix

Advertisements