How to resolve the algorithm Averages/Mode step by step in the ERRE programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Averages/Mode step by step in the ERRE programming language

Table of Contents

Problem Statement

Write a program to find the mode value of a collection. The case where the collection is empty may be ignored. Care must be taken to handle the case where the mode is non-unique. If it is not appropriate or possible to support a general collection, use a vector (array), if possible. If it is not appropriate or possible to support an unspecified value type, use integers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Averages/Mode step by step in the ERRE programming language

Source code in the erre programming language

PROGRAM MODE_AVG

!$INTEGER

DIM A[10],B[10],Z[10]

PROCEDURE SORT(Z[],P->Z[])
   LOCAL N,FLIPS
   FLIPS=TRUE
   WHILE FLIPS DO
      FLIPS=FALSE
      FOR N=0 TO P-1 DO
        IF Z[N]>Z[N+1] THEN SWAP(Z[N],Z[N+1]) FLIPS=TRUE
      END FOR
   END WHILE
END PROCEDURE

PROCEDURE CALC_MODE(Z[],P->MODES$)
   LOCAL I,OCCURRENCE,MAXOCCURRENCE,OLDVAL
   SORT(Z[],P->Z[])
   OCCURENCE=1
   MAXOCCURENCE=0
   OLDVAL=Z[0]
   MODES$=""
   FOR I=1 TO P DO
       IF Z[I]=OLDVAL THEN
           OCCURENCE=OCCURENCE+1
         ELSE
           IF OCCURENCE>MAXOCCURENCE THEN
                MAXOCCURENCE=OCCURENCE
                MODES$=STR$(OLDVAL)
             ELSIF OCCURENCE=MAXOCCURENCE THEN
                MODES$=MODES$+STR$(OLDVAL)
             ELSE
                !$NULL
           END IF
           OCCURENCE=1
       END IF
       OLDVAL=Z[I]
   END FOR
   !check after loop
   IF OCCURENCE>MAXOCCURENCE THEN
       MAXOCCURENCE=OCCURENCE
       MODES$=STR$(OLDVAL)
   ELSIF OCCURENCE=MAXOCCURENCE THEN
        MODES$=MODES$+STR$(OLDVAL)
   ELSE
       !$NULL
   END IF
END PROCEDURE

BEGIN
   A[]=(1,3,6,6,6,6,7,7,12,12,17)
   B[]=(1,2,4,4,1)
   PRINT("Modes for array A (1,3,6,6,6,6,7,7,12,12,17)";)
   CALC_MODE(A[],10->MODES$)
   PRINT(MODES$)
   PRINT("Modes for array B (1,2,4,4,1)";)
   CALC_MODE(B[],4->MODES$)
   PRINT(MODES$)
END PROGRAM

  

You may also check:How to resolve the algorithm Attractive numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Sort an integer array step by step in the Elena programming language
You may also check:How to resolve the algorithm Pell's equation step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Y combinator step by step in the Raku programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the FutureBasic programming language