How to resolve the algorithm Zig-zag matrix step by step in the Ceylon programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Zig-zag matrix step by step in the Ceylon programming language
Table of Contents
Problem Statement
Produce a zig-zag array.
A zig-zag array is a square arrangement of the first N2 natural numbers, where the
numbers increase sequentially as you zig-zag along the array's anti-diagonals.
For a graphical representation, see JPG zigzag (JPG uses such arrays to encode images).
For example, given 5, produce this array:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Zig-zag matrix step by step in the Ceylon programming language
Source code in the ceylon programming language
class ZigZag(Integer size) {
value data = Array {
for (i in 0:size)
Array.ofSize(size, 0)
};
variable value i = 1;
variable value j = 1;
for (element in 0 : size^2) {
data[j - 1]?.set(i - 1, element);
if ((i + j).even) {
if (j < size) {
j++;
}
else {
i += 2;
}
if (i > 1) {
i--;
}
}
else {
if (i < size) {
i++;
}
else {
j += 2;
}
if (j > 1) {
j--;
}
}
}
shared void display() {
for (row in data) {
for (element in row) {
process.write(element.string.pad(3));
}
print(""); //newline
}
}
}
shared void run() {
value zz = ZigZag(5);
zz.display();
}
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the HicEst programming language
You may also check:How to resolve the algorithm String append step by step in the Stata programming language
You may also check:How to resolve the algorithm Special variables step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Dining philosophers step by step in the Go programming language
You may also check:How to resolve the algorithm Empty program step by step in the Quite BASIC programming language