15.04.2018 Views

programming-for-dummies

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

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

322<br />

Working with Multi-Dimensional Arrays<br />

The most common multi-dimensional array is a two-dimensional array,<br />

which looks like a grid, as shown in Figure 1-8.<br />

Dim BigArray (4,2) as String<br />

BigArray(1,1)<br />

BigArray(1,2)<br />

Figure 1-8:<br />

A twodimensional<br />

array lets<br />

you store<br />

data in a<br />

grid.<br />

BigArray(2,1)<br />

BigArray(3,1)<br />

BigArray(4,1)<br />

BigArray(2,2)<br />

BigArray(3,2)<br />

BigArray(4,2)<br />

You can create 3-, 4-, or even 19-dimensional arrays. However, after you get<br />

past a three-dimensional array, understanding how that array works can be<br />

too confusing, so most programmers stick to two-dimensional or threedimensional<br />

arrays.<br />

Creating a multi-dimensional array<br />

To create a multi-dimensional array, you have to define another upper bound<br />

<strong>for</strong> an array. So if you wanted to create a 4 x 2 two-dimensional array, you<br />

could use the following BASIC code:<br />

Dim BigArray(4,2) as String<br />

To create the same two-dimensional array in C++, you could use the following<br />

code:<br />

string bigarray[4][2];<br />

To create three or more dimensional arrays, keep adding on additional<br />

bounds, such as<br />

Dim BigArray(2,4,3,8) as String

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

Saved successfully!

Ooh no, something went wrong!