-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathspiralPrint.cpp
77 lines (67 loc) · 2.08 KB
/
spiralPrint.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;
int main()
{
int nrows, mcols;
cout << "Enter the number of rows and columns:" << endl;
cin >> nrows >> mcols;
int matrix[nrows][mcols];
// Taking input from the user:
cout << "Enter the elements of the matrix:" << endl;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < mcols; col++)
{
cin >> matrix[row][col];
}
}
// Printing the elements of the matrix:
cout << "The 2D array is:" << endl;
for (int row = 0; row < nrows; row++)
{
for (int col = 0; col < mcols; col++)
{
cout << matrix[row][col] << " ";
}
cout << endl;
}
int startingRow = 0;
int endingCol = mcols - 1;
int endingRow = nrows - 1;
int startingCol = 0;
int count = 0; // count is a variable which stores the number of elements that have been printed.
int total = nrows * mcols; // total is a variable which stores the total number of elements in the matrix.
cout << "Spiral traversal of the given matrix will be:" << endl;
while (count < total)
{
// Printing starting row:
for (int index = startingCol; count < total && index <= endingCol; index++)
{
cout << matrix[startingRow][index] << " ";
count++;
}
startingRow++;
// Printing ending column:
for (int index = startingRow; count < total && index <= endingRow; index++)
{
cout << matrix[index][endingCol] << " ";
count++;
}
endingCol--;
// Printing ending row:
for (int index = endingCol; count < total && index >= startingCol; index--)
{
cout << matrix[endingRow][index] << " ";
count++;
}
endingRow--;
// Printing starting column:
for (int index = endingRow; count < total && index >= startingRow; index--)
{
cout << matrix[index][startingCol] << " ";
count++;
}
startingCol++;
}
return 0;
}