How to resolve the algorithm Magic squares of odd order step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Magic squares of odd order step by step in the ERRE programming language

Table of Contents

Problem Statement

A magic square is an   NxN   square matrix whose numbers (usually integers) consist of consecutive numbers arranged so that the sum of each row and column,   and   both long (main) diagonals are equal to the same sum (which is called the   magic number   or   magic constant). The numbers are usually (but not always) the first   N2   positive integers. A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square.

For any odd   N,   generate a magic square with the integers   1 ──► N,   and show the results here. Optionally, show the magic number.
You should demonstrate the generator by showing at least a magic square for   N = 5.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Magic squares of odd order step by step in the ERRE programming language

Source code in the erre programming language

PROGRAM MAGIC_SQUARE

!$INTEGER

PROCEDURE Magicsq(size,filename$)

LOCAL DIM sq[25,25] ! array to hold square

IF (size AND 1)=0 OR size<3 THEN
     PRINT PRINT(CHR$(7)) ! beep
     PRINT("error: size is not odd or size is smaller then 3")
     PAUSE(3)
     EXIT PROCEDURE
END IF

! filename$ <> "" then save magic square in a file
! filename$ can contain directory name
! if filename$ exist it will be overwriten, no error checking

! start in the middle of the first row
   nr=1   x=size-(size DIV 2) y=1
   max=size*size

! create format string for using
   frmt$=STRING$(LEN(STR$(max)),"#")

! main loop for creating magic square
   REPEAT
      IF sq[x,y]=0 THEN
        sq[x,y]=nr
        IF nr MOD size=0 THEN
           y=y+1
         ELSE
           x=x+1
           y=y-1
        END IF
        nr=nr+1
      END IF
      IF x>size THEN
         x=1
         WHILE sq[x,y]<>0 DO
            x=x+1
         END WHILE
      END IF
      IF y<1 THEN
         y=size
         WHILE sq[x,y]<>0 DO
            y=y-1
         END WHILE
      END IF
   UNTIL nr>max

! printing square's bigger than 19 result in a wrapping of the line
   PRINT("Odd magic square size:";size;"*";size)
   PRINT("The magic sum =";((max+1) DIV 2)*size)
   PRINT

   FOR y=1 TO size DO
      FOR x=1 TO size DO
          WRITE(frmt$;sq[x,y];)
      END FOR
      PRINT
   END FOR

  ! output magic square to a file with the name provided
  IF filename$<>"" THEN
        OPEN("O",1,filename$)
           PRINT(#1,"Odd magic square size:";size;" *";size)
           PRINT(#1,"The magic sum =";((max+1) DIV 2)*size)
           PRINT(#1,)

           FOR y=1 TO size DO
             FOR x=1 TO size DO
               WRITE(#1,frmt$;sq[x,y];)
             END FOR
             PRINT(#1,)
           END FOR
  END IF
  CLOSE(1)

END PROCEDURE

BEGIN
PRINT(CHR$(12);)  ! CLS
Magicsq(5,"")
Magicsq(11,"")
!----------------------------------------------------
! the next line will also print the square to a file
! called 'magic_square_19txt'
!----------------------------------------------------
Magicsq(19,"msq_19.txt")

END PROGRAM

  

You may also check:How to resolve the algorithm Forest fire step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Rep-string step by step in the D programming language
You may also check:How to resolve the algorithm Take notes on the command line step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the Ruby programming language
You may also check:How to resolve the algorithm Spiral matrix step by step in the ooRexx programming language