Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Matrix spiral print
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.5 sec, absolute running time: 0.14 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.64 sec
fork mode
|
history
|
discussion
1 2 3 4 6 8 7 6 5 3 4 5