
- 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
Print a matrix in alternate manner (left to right then right to left) in C++
In this problem, we are given a 2D array. Our task is to print all the elements of the array starting from the first row, from left to right, then right to left in the next row and again from left to right and so on.
Let’s take an example to understand the problem.
Input: array = { {2, 5} {4, 9} } Output: 2 5 9 4
To solve this problem, we will print elements in the given direction (LtoR and RtoL) of a row. And a flag element to show the direction of printing will switch after every iteration.
This is an easy and efficient solution with time complexity = O(R*C)
Example
Program to show the implementation of our solution
#include<iostream> using namespace std; #define R 3 #define C 3 void printAlternateMatrix(int arr[R][C]) { bool direction = true; for (int i=0; i<R; i++){ if (direction){ for (int j=0; j<C; j++) printf("%d ", arr[i][j]); } else{ for (int j=C-1; j>=0; j--) printf("%d ",arr[i][j]); } direction = !direction; } } int main() { int arr[][C] = { { 23 , 50 , 4 }, { 89 , 9 , 34 }, { 75 , 1 , 61 }, }; cout<<"Matrix in alternate order is :\n"; printAlternateMatrix(arr); return 0; }
Output
The matrix in alternate order is −
23 50 4 34 9 89 75 1 61
- Related Articles
- Position after taking N steps to the right and left in an alternate manner in C++
- If \( \left(\frac{a}{b}\right)=\left(\frac{5}{2}\right)^{-3} \times\left(\frac{8}{15}\right)^{-3} \) then \( \left(\frac{a}{b}\right)^{-2} \) is equal to
- Print all palindromic paths from top left to bottom right in a matrix in C++
- Print all possible paths from top left to bottom right of a mXn matrix in C++
- Program to print right and left arrow patterns in C
- Simplify \( \left[\left\{\left(\frac{1}{2}\right)^{2}\right\}^{-2}\right]^{-2} \).
- Print all paths from top left to bottom right in a matrix with four moves allowed in C++
- Simplify:(i) \( \left\{\left(\frac{1}{3}\right)^{-3}-\left(\frac{1}{2}\right)^{-3}\right\} \div\left(\frac{1}{4}\right)^{-3} \)(ii) \( \left(3^{2}-2^{2}\right) \times\left(\frac{2}{3}\right)^{-3} \)(iii) \( \left\{\left(\frac{1}{2}\right)^{-1} \times(-4)^{-1}\right\}^{-1} \)(iv) \( \left[\left\{\left(\frac{-1}{4}\right)^{2}\right\}^{-2}\right]^{-1} \)(v) \( \left\{\left(\frac{2}{3}\right)^{2}\right\}^{3} \times\left(\frac{1}{3}\right)^{-4} \times 3^{-1} \times 6^{-1} \)
- How to handle right-to-left and left-to-right swipe gestures on Android?
- CSS3 Left to right Gradient
- How to handle right-to-left and left-to-right swipe gestures on iOS App?
- Simplify:\( \left[\left(\frac{1}{3}\right)^{-1}-\left(\frac{2}{5}\right)^{-1}\right]^{-2} \div\left(\frac{3}{4}\right)^{-3} \)
- Simplify:(i) \( \left(3^{2}+2^{2}\right) \times\left(\frac{1}{2}\right)^{3} \)(ii) \( \left(3^{2}-2^{2}\right) \times\left(\frac{2}{3}\right)^{-3} \)(iii) \( \left[\left(\frac{1}{3}\right)^{-3}-\left(\frac{1}{2}\right)^{-3}\right] \div\left(\frac{1}{4}\right)^{-3} \)(iv) \( \left(2^{2}+3^{2}-4^{2}\right) \div\left(\frac{3}{2}\right)^{2} \)
- Simplify:\( \left\{\left(\frac{3}{7}\right)^{-2}\right\}^{-3} \div\left(\frac{-9}{49}\right)^{2} \)
- Simplify:$\left[\left(\frac{-2}{5}\right)^{3}\right]^{4}$

Advertisements