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

Published on 12 May 2024 09:40 PM

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

Source code in the rexx programming language

/*REXX program  produces and displays a    zig─zag  matrix   (a square array).          */
parse arg n start inc .                          /*obtain optional arguments from the CL*/
if     n=='' |     n==","  then     n= 5         /*Not specified?  Then use the default.*/
if start=='' | start==","  then start= 0         /* "      "         "   "   "     "    */
if   inc=='' |   inc==","  then   inc= 1         /* "      "         "   "   "     "    */
row= 1;           col= 1;        size= n**2      /*start: 1st row & column;  array size.*/
        do j=start  by inc  for size;    @.row.col= j
        if (row+col)//2==0  then do;  if col
                                      if row\==1  then row= row-1
                                 end
                            else do;  if row
                                      if col\==1  then col= col-1
                                 end
        end   /*j*/                              /* [↑]     //    is REXX  ÷  remainder.*/
call show                                        /*display a (square) matrix──►terminal.*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: w= max(length(start), length(start+size*inc))  /*max width of any matrix elements,*/
        do   r=1  for n  ;  _=   right(@.r.1, w)     /*show the rows of the matrix.     */
          do c=2  for n-1;  _= _ right(@.r.c, w)     /*build a line for output of a row.*/
          end   /*c*/;  say _                        /* [↑]  align the matrix elements. */
        end     /*r*/;      return


/*REXX program  produces and displays a zig-zag  matrix (a square array) */
Parse Arg n start inc .   /* obtain optional arguments from command line */
if     n=='' |     n==","  then     n= 5 /*Not specified? use the default*/
if start=='' | start==","  then start= 0 /* "      "       "   "     "   */
if   inc=='' |   inc==","  then   inc= 1 /* "      "       "   "     "   */
Parse Value 1 1 n**2 With row col size
Do x=start By inc For size
  m.row.col=x
  If (row+col)//2=0 Then do  /* moving upward                            */
    Select
      when row=1 Then Do     /* at upper bound                           */
        If col
          col=col+1          /* move right                               */
        Else
          row=2              /* move down                                */
        End
      when col=n Then        /* at right border                          */
        row=row+1            /* move down                                */
      Otherwise Do           /* in all other cases                       */
        row=row-1            /* move up                                  */
        col=col+1            /* and to the right                         */
        End
      End
    End
  Else Do                    /* moving downward                          */
    Select
      When col=1 Then Do     /* at lower bound                           */
        If row=n Then        /* in bottom row                            */
          col=2              /* move right                               */
        Else                 /* otherwise                                */
          row=row+1          /* move down                                */
        End
      When row=n Then        /* at lower bound                           */
        col=col+1            /* move right                               */
      Otherwise Do           /* in all other cases                       */
        row=row+1            /* move down                                */
        col=col-1            /* and to the left                          */
        End
      End
    End
  End
Call show
Exit
/*-----------------------------------------------------------------------*/
show:
  w=length(start+size*inc)            /* max width of any matrix element */
  Do row=1 To n                       /* loop over rows                  */
    line=right(m.row.1,w)             /* first element                   */
    Do column=2 To n                  /* loop over other elements        */
      line=line right(m.row.column,w) /* build output line               */
      End
    Say line
    End                               /* display the line                */
  Return


  

You may also check:How to resolve the algorithm Hello world/Line printer step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Comments step by step in the Dylan programming language
You may also check:How to resolve the algorithm Vector step by step in the Ring programming language
You may also check:How to resolve the algorithm Terminal control/Display an extended character step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Visualize a tree step by step in the Elena programming language