forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake_pattern.c
65 lines (32 loc) · 873 Bytes
/
Snake_pattern.c
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
55
56
57
58
59
60
61
62
63
64
65
// C++ program to print matrix in snake order
#include <iostream>
#define M 4
#define N 4
using namespace std;
void print(int mat[M][N])
{
// Traverse through all rows
for (int i = 0; i < M; i++) {
// If current row is even, print from
// left to right
if (i % 2 == 0) {
for (int j = 0; j < N; j++)
cout << mat[i][j] << " ";
// If current row is odd, print from
// right to left
} else {
for (int j = N - 1; j >= 0; j--)
cout << mat[i][j] << " ";
}
}
}
// Driver code
int main()
{
int mat[][] = { { 10, 20, 30, 40 },
{ 15, 25, 35, 45 },
{ 27, 29, 37, 48 },
{ 32, 33, 39, 50 } };
print(mat);
return 0;
}