19.04.2017 Views

Learn to Program with Small Basic

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

TextWindow.WriteLine("Enter 5 numbers. Press Enter after each one.")<br />

For N = 1 To 5<br />

thunderCat[N] = TextWindow.ReadNumber()<br />

EndFor<br />

This technique is very useful for s<strong>to</strong>ring lots of data from a user. What<br />

other collections of data might you ask for? How about breakfastMenu,<br />

favoriteGames, bestPasswords, funnyJokes, or frozenNames?<br />

TRY IT OUT 15-2<br />

Write a program that fills an array called skele<strong>to</strong>r <strong>with</strong> even integers from 20 <strong>to</strong><br />

40 (for example, skele<strong>to</strong>r[1] = 20, skele<strong>to</strong>r[2] = 22, . . . ).<br />

Displaying Arrays<br />

Let’s say we have an array named age that holds the ages of three brothers,<br />

like this:<br />

age[1] = 14<br />

age[2] = 15<br />

age[3] = 16<br />

You can display the contents of this array in two ways. The first and easiest<br />

way is <strong>to</strong> pass the array’s name <strong>to</strong> the WriteLine() method, like this:<br />

TextWindow.WriteLine(age)<br />

Here’s the output of this statement:<br />

1=14;2=15;3=16;<br />

This statement displays the elements of the array on a single line separated<br />

by semicolons. Each <strong>to</strong>ken in this string shows the index and the value<br />

of the array’s element at that index. Can you see now where the array’s string<br />

initializer syntax came from?<br />

If you want <strong>to</strong> display the array in an easier-<strong>to</strong>-read format, you can use<br />

a For loop <strong>to</strong> display each element of the array in its own row:<br />

For N = 1 To 3<br />

TextWindow.WriteLine("age[" + N + "] = " + age[N])<br />

EndFor<br />

Grouping Data in One-Dimensional Arrays 213

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!