How to resolve the algorithm Zig-zag matrix step by step in the Objeck programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Zig-zag matrix step by step in the Objeck 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 Objeck programming language

Source code in the objeck programming language

function : native : ZigZag(size : Int) ~ Int[,] {
  data := Int->New[size, size];
  i := 1;
  j := 1;
  
  max := size * size;
  for(element := 0; element < max ; element += 1;) {
    data[i - 1, j - 1] := element;
    
    if((i + j) % 2 = 0) {
      # even stripes
      if(j < size){
        j += 1;
      }
      else{
        i+= 2;
      };
      
      if(i > 1) {
        i -= 1;
      };
    }
    else{
      # ddd stripes
      if(i < size){
        i += 1;
      }
      else{
        j+= 2;
      };
      
      if(j > 1){
        j -= 1;
      };
    };
  };
  
  return data;
}


  

You may also check:How to resolve the algorithm Extend your language step by step in the Haskell programming language
You may also check:How to resolve the algorithm Chaos game step by step in the Wren programming language
You may also check:How to resolve the algorithm String length step by step in the MATLAB programming language
You may also check:How to resolve the algorithm Logical operations step by step in the Octave programming language
You may also check:How to resolve the algorithm String prepend step by step in the Perl programming language