-
Notifications
You must be signed in to change notification settings - Fork 46
/
wave_print.cpp
52 lines (49 loc) · 1.22 KB
/
wave_print.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Given a 2D Array of size M*N, we need to print the elements in a wave pattern i.e. frist from top-to-bottom and then from bottom-to-top and so on
#include <iostream>
using namespace std;
void wavePrint(int **input, int nRows, int mCols)
{
int j=0;
while(j<mCols)
{
if(j%2==0) // If the jth column is even, we'll print the elements from top-to-bottom
{
for(int i=0;i<nRows;i++)
{
cout<<input[i][j]<<" ";
}
}
else // Otherwise if the jth column is odd, we'll print the elements from bottom-to-top
{
for(int i=nRows-1;i>=0;i--)
{
cout<<input[i][j]<<" ";
}
}
j++;
}
}
int main()
{
int t;
cout<<"Enter the number of test cases to run : ";
cin >> t;
while (t--)
{
int row, col;
cout<<"Enter the size of the row and the column : ";
cin >> row >> col;
int **input = new int *[row];
cout<<"Enter the elements/values corresponding to the row and the column : ";
for (int i = 0; i < row; i++)
{
input[i] = new int[col];
for (int j = 0; j < col; j++)
{
cin >> input[i][j];
}
}
wavePrint(input, row, col);
cout << endl;
}
}