Skip to content
Lesson 5 of 6

Step 1 of 5 · Reading · ~3 min

Learn

Subs, Functions, Arrays

Arrays

BASIC arrays use DIM with bounds:

DIM nums(10) AS INTEGER          ' 0..10 — 11 elements!
DIM names(1 TO 5) AS STRING      ' 1..5 — 5 elements
DIM matrix(3, 3) AS INTEGER      ' 4x4 (0..3, 0..3)

nums(0) = 100
nums(1) = 200
PRINT STR(nums(0))

Like Pascal, BASIC declares inclusive upper bounds. DIM nums(10) gives 11 elements, 0 through 10 — not 10. This is the first thing to unlearn coming from C.

Initialization

Elements default to 0 / 0.0 / "" depending on type. Initialize:

DIM nums(4) AS INTEGER
FOR i AS INTEGER = 0 TO 4
    nums(i) = (i + 1) * 10
NEXT i

FOR i AS INTEGER = ... declares the loop variable right in the header, and that variable exists only inside the loop. The alternative is a plain DIM i AS INTEGER above it. One of the two is required — FreeBASIC will not invent i for you, and "Variable not declared, i" is the compile error you get if you forget.

Or initialize inline (FreeBASIC):

DIM nums(4) AS INTEGER => {10, 20, 30, 40, 50}

Bounds

LBOUND(nums)        ' 0 — lower bound
UBOUND(nums)        ' 4 — upper bound: the last INDEX, not the length
UBOUND(nums) - LBOUND(nums) + 1   ' length

LEN(nums) does not give you a length — it reports the byte size of one element. Count with UBOUND.

REDIM (resize)

DIM nums() AS INTEGER       ' undimensioned
REDIM nums(9)               ' size 10 (0..9)
REDIM PRESERVE nums(19)     ' grow, keep contents

Without PRESERVE every element goes back to 0, including ones you had already filled in.

Iteration

FOR i AS INTEGER = LBOUND(nums) TO UBOUND(nums)
    PRINT STR(nums(i))
NEXT i

Using LBOUND/UBOUND adapts to any bound choice.

Multi-dim

DIM grid(2, 2) AS INTEGER       ' 3x3
grid(1, 1) = 5
PRINT STR(grid(1, 1))           ' 5

' 3D:
DIM cube(9, 9, 9) AS INTEGER    ' 1000 elements

String arrays

DIM names(0 TO 2) AS STRING
names(0) = "Ada"
names(1) = "Bob"
names(2) = "Carol"

FOR i AS INTEGER = 0 TO UBOUND(names)
    PRINT names(i)
NEXT i

ERASE arr clears an array back to zeros. FreeBASIC ships no sort — you write one or pull in a library.

Splitting strings

DIM text AS STRING
LINE INPUT "", text

DIM parts(0 TO 100) AS STRING
DIM count AS INTEGER = 0
DIM start AS INTEGER = 1
DIM idx AS INTEGER

DO
    idx = INSTR(start, text, " ")
    IF idx = 0 THEN
        parts(count) = MID(text, start)
        count += 1
        EXIT DO
    END IF
    parts(count) = MID(text, start, idx - start)
    count += 1
    start = idx + 1
LOOP

FOR i AS INTEGER = 0 TO count - 1
    PRINT parts(i)
NEXT i

Manual — FreeBASIC has no built-in SPLIT. INSTR and MID are your tools.

Your exercise

The starter reads n, fills nums(0) through nums(n - 1), and prints both answer lines for you, built with & so the numbers arrive without their sign column. What is missing is the single pass over the array that gives total and maxVal their values.

Two things decide whether it works:

  • Walk the indices that exist. The array runs from 0 to n - 1; LBOUND/UBOUND will tell you the same thing without you having to remember it.
  • A maximum is seeded, not assumed. Starting maxVal at 0 quietly answers 0 for an all-negative array. The starter seeds it from the first element instead, which is the habit worth keeping.
Up nextStrings and PRINT FormattingSubs, Functions, Arrays

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…