forked from akshatkumar27/Hactoberfest_Challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_print.cpp
More file actions
53 lines (50 loc) · 1.3 KB
/
Copy pathwave_print.cpp
File metadata and controls
53 lines (50 loc) · 1.3 KB
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
53
// Given a 2D matrix of size M*N, we need to print the matrix in a sinusoidal wave form. This can be achieved by printing the matrix row-wise.
// Now when the jth column is even we'll print the row from top-to-bottom and when the jth column the odd we'll print the row from bottom-to-top
#include <iostream>
using namespace std;
void wavePrint(int **input, int nRows, int mCols)
{
int j=0;
while(j<mCols)
{
if(j%2==0) // Checking whether the jth column is odd or even. If even print top-to-bottom
{
for(int i=0;i<nRows;i++)
{
cout<<input[i][j]<<" ";
}
}
else // Otherwise print 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 values corresponding to the row and te 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;
}
}