Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Matrix spiral print
//Title of this code #include <iostream> using namespace std; template<int rows, int cols> void spirallyPrint(const int t[rows][cols]) { int hStart = 0; // horizontal start int hEnd = cols - 1; // horizontal end int vStart = 0; // vertical start int vEnd = rows - 1; // vertical end while (hStart <= hEnd && vStart <= vEnd) { // horizontal left to right if (vStart <= vEnd) for (int i = hStart; i <= hEnd; ++i) cout << t[vStart][i] << " "; ++vStart; // first row done // vertical top to bottom if (hStart <= hEnd) for (int i = vStart; i <= vEnd; ++i) cout << t[i][hEnd] << " "; --hEnd; // last column done // horizontal right to left if (vStart <= vEnd) for (int i = hEnd; i >= hStart; --i) cout << t[vEnd][i] << " "; --vEnd; // last row done // vertical bottom to top if (hStart <= hEnd) for (int i = vEnd; i >= vStart; --i) cout << t[i][hStart] << " "; ++hStart; // first column done } } int main() { int t[3][4] = { {1, 2, 3, 4}, {3, 4, 5, 6}, {5, 6, 7, 8} }; spirallyPrint<3, 4>(t); cout << endl; int t1[4][3] = { {1, 2, 3}, {3, 4, 5}, {7, 9, 6}, {5, 8, 7} }; spirallyPrint<4, 3>(t1); cout << endl; int t2[1][1] = {{8}}; spirallyPrint<1, 1>(t2); cout << endl; int t3[2][1] = {{6}, {7}}; spirallyPrint<2, 1>(t3); }
run
|
edit
|
history
|
help
0
NWD, algorytm Euklidesa
1568-1644 Solar eclipses, make 1617(8) and 1620(12) Lunar eclipses the same as Chinese records
mur1
LIS
cppPyEnum
Expected GCD
Dar
Problema1
qwerty
BubbleDouble