-
Notifications
You must be signed in to change notification settings - Fork 25
/
arraySineWave.cpp
54 lines (48 loc) · 956 Bytes
/
arraySineWave.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
53
54
//in a 2D integer array of size N x M print in a sine wave pattern
//print the first column top to down, next column down to top and so on
#include <bits/stdc++.h>
using namespace std;
void sineWave(int **arr, int nRows, int mCols)
{
int i;
for(int j=0;j<mCols;j++)
{
//even column number
if(j%2==0)
{
for(i=0;i<nRows;i++)
{
cout<<arr[i][j]<<" ";
}
}
//odd column number
else
{
for(i=nRows-1;i>-1;i--)
{
cout<<arr[i][j]<<" ";
}
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int row, col;
cin >> row >> col;
int **arr = new int *[row];
for (int i = 0; i < row; i++)
{
arr[i] = new int[col];
for (int j = 0; j < col; j++)
{
cin >> arr[i][j];
}
}
sineWave(arr, row, col);
cout << endl;
}
}