DIM: Your Array Architect
Need to store a bunch of data in an organized way? Want to create a list, table, or even a multi-dimensional grid? Look no further than the DIM
command! This handy tool lets you define arrays in your Commodore 64 BASIC programs, giving you a structured way to store multiple values under a single name. Think of it as your blueprint for building data structures!
Syntax
DIM <array name>(<dimension1>[, <dimension2>, ...])
Where:
- <array name>
: A single letter (A-Z) followed by a dollar sign ($) for string arrays, or just a single letter for numeric arrays.
- <dimension1>[, <dimension2>, ...]
: One or more numbers specifying the size of each dimension of the array.
Applications
The DIM
command is your key to creating:
- Lists: Store a sequence of related items, like high scores, names, or inventory items.
- Tables: Organize data with rows and columns, like a game board or a character map.
- Matrices: Create multi-dimensional arrays for complex data structures, like 3D coordinates or image data.
Code Examples
1. Simple One-Dimensional Array:
10 DIM A(5) :rem Create a numeric array with 6 elements (0-5)
20 FOR I=0 TO 5
30 A(I) = I * 10 :rem multiply element position by 10
40 NEXT I
50 PRINT A(3) :rem Output: 30
This creates a numeric array A
and fills it with multiples of 10.
2. String Array for Names:
10 DIM N$(3) :rem String array for 4 names
20 N$(0)="Alice"
30 N$(1)="Bob"
40 N$(2)="Carol"
50 N$(3)="David"
60 PRINT N$(2) :rem Output: Carol
This demonstrates how to create a string array N$
and store names in it.
3. Two-Dimensional Array (Table):
10 DIM T(2,3) :rem Create a table with 3 rows and 4 columns
20 FOR R=0 TO 2
30 FOR C=0 TO 3
40 T(R,C) = R*10 + C
50 NEXT C
60 NEXT R
70 PRINT T(1,2) :rem Output: 12
This showcases the creation and use of a two-dimensional array T
to represent a table.
DIM in the Wild: The Pixel Art Powerhouse
Imagine you're designing a character editor for your Commodore 64. You could use a two-dimensional array to store the pixel data for each character, allowing you to easily manipulate and display them on the screen.
Don't let your data become a chaotic mess! With DIM
at your disposal, you can build structured arrays to organize your information, making your programs more efficient, elegant, and easy to manage. It's like having a master architect for your data, helping you design robust and adaptable structures that will elevate your Commodore 64 programming to new heights!