Arrays in QBasic

Codynn
5 Min Read

Hello fellow programmers and enthusiasts!

Welcome back to our programming journey! Today, we’ll learn about arrays in QBasic, which are powerful tools to store and manage data effectively. Arrays are like containers that can hold multiple pieces of information, making them essential for various programming tasks. Let’s explore how to use arrays in QBasic and make the most out of their capabilities!

What is an Array?

An array is like a bunch of boxes where you can put similar items together. Each box has a number or label to identify it. It helps you handle a lot of related data in a more organized and efficient way. Instead of having many separate variables, you group them together under one name and access them using their unique numbers or labels. This makes it much easier to work with large amounts of similar data.

Declaring and Initializing Arrays

In QBasic, arrays can be declared and initialized in various ways. The general syntax for declaring an array is as follows:

DIM arrayName(dimension1Size [, dimension2Size, ...])

You can have arrays with one or multiple dimensions. Let’s see some examples of array declarations and initialization:

DIM numbers(10) ' One-dimensional array with 11 elements (0 to 10)

DIM matrix(3, 3) ' Two-dimensional array with 16 elements (4 rows, 4 columns)

DIM studentNames$(50) ' One-dimensional array of strings with 51 elements (0 to 50)

DIM temperature(7, 4, 3) ' Three-dimensional array with 96 elements (8x5x4)

Accessing Array Elements

Array elements are accessed using their index. In QBasic, the array indices start from 0 and go up to the specified dimension size minus one. Let’s see how we can access elements from the arrays we declared above:

' Accessing elements in a one-dimensional array
numbers(0) = 10
numbers(5) = 42
PRINT numbers(0) ' Output: 10
PRINT numbers(5) ' Output: 42

' Accessing elements in a two-dimensional array
matrix(1, 2) = 7
matrix(3, 1) = 20
PRINT matrix(1, 2) ' Output: 7
PRINT matrix(3, 1) ' Output: 20

' Accessing elements in an array of strings
studentNames$(3) = "John Doe"
PRINT studentNames$(3) ' Output: John Doe

' Accessing elements in a three-dimensional array
temperature(2, 3, 1) = 25
PRINT temperature(2, 3, 1) ' Output: 25

Looping through Arrays

One of the most common tasks with arrays is to iterate through their elements. We can achieve this using loops, such as the FOR-NEXT loop. Here’s an example of how we can use a loop to initialize a one-dimensional array:

DIM squares(9)
FOR i = 0 TO 9
    squares(i) = i * i
NEXT i

Dynamic Arrays

In QBasic, you can also use dynamic arrays whose size is determined at runtime. To create a dynamic array, you can use the REDIM statement:

REDIM dynamicArray(5) ' Create a one-dimensional array with six elements

You can resize dynamic arrays during program execution, allowing you to adapt to changing data requirements.

Multi-dimensional Arrays

Multi-dimensional arrays are particularly useful for representing tables or grids of data. You can use nested loops to access elements in multi-dimensional arrays efficiently:

DIM grid(3, 3)
FOR row = 0 TO 3
    FOR col = 0 TO 3
        grid(row, col) = row * col
    NEXT col
NEXT row

Conclusion

Arrays are an essential tool in QBasic programming that opens up many exciting possibilities. They help us handle big sets of data effectively and provide a strong way to organize and get the information we need. We’ve only covered the basics of arrays in QBasic, but there’s so much more you can do with them. So, take what you’ve learned, try things out, and discover the limitless potential of using arrays with indexed data!

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *