XNA Game-Themed CS1 Examples (XGC1)

Release 2.0 (XNA V3.1)
2/8/2010

Topic: Topic.6.Arrays
Example: Ex_1.ArrayOfIntegers

Arrays: Basic Array Of Integers


References:

Goals:



1. Obtain the example code

Download and unzip the zip file and you will see an ExampleProgram folder. Open the ExampleProgram folder, the EXE folder contains the compiled program and you can double click on the .sln file to work with the source code.

When the game starts, you'll see a screen that looks similar to this:


As you can see, this program draws five soccer balls across the screen.  The program also draws a basketball on the screen, and allows you to move the basketball using the right thumbstick.

What's new and interesting with this program is that there are five separate numbers across the bottom of the screen - 10, 20, 30, 40, and 50.  If you push the A button, you increment the first number, up to 11.  Push the A button again, and you're looking at 12, and so on.  Similarly, the B button will increment the second number (to 21, then 22, etc), the X button increments the third number, and Y increments the fourth number.  The fifth, and last, number cannot be incremented by the user.

What's going on here is that these integers are being stored in an array, which is the major topic for this chapter.  In C#, an array consists of a bunch of elements (a.k.a. locations, or slots, or spaces, etc);  all the individual elements are of the same type (in this case, all integers), but each element stores a separate, unique, individual value.  All the elements in a single array are stored together right next to each other in memory, without any breaks in memory (i.e., they're stored contiguously in memory).  The entire array is accessed using a single variable name.  In order to access individuals elements in the array, the first one  is numbered with zero - you can think of this as an "address", or part of it's "name".  The number zero is commonly referred to as the element's subscript, or the index of the element.  The second element is given the subscript (index) 1, the next element is 2, etc.  Here's a picture that represents the array that we're using in the above program, when the game starts:

The numbers in red, above the array, are labeled "Subscripts", while the numbers in black, inside the squares, are labeled "Elements".  Each element holds a single integer (since it's an array of integers).  You'll notice that the first element has the value "10", the second element has the value "20", etc. Before we look at the code, let's examine some syntactical details of arrays, first:

If the array (as a whole) is referred to use the variable named m_MyArray, then we can talk about the first element in our program using the "name" m_MyArray[0]Notice that the way we refer to an element is by using the name for the whole array, then an open bracket after the name (  [  ), then the subscript/index number for the element that we want to access, and then the close bracket (  ]  ).  Because each element holds a separate value, you can think of each element of the array as being a separate variable.  The only difference between a normal variable and an array element is that the array elements all share the same name (but each element has a unique subscript!)

Let's say that we wanted to store the number 10 into the first array element of the array named m_MyArray.  Just for comparison's sake, let's also say that we want to store the value 12 into the variable named myInteger.   (Let's also assume that both have been declared, created, and initialized, as appropriate).  In order to assign these values, our code would look like:

m_MyArray[0] = 10;

myInteger = 12;


Notice how similar the lines are - in effect, "m_MyArray[0]" is the name of the first element in the array, so we can use that exactly like we'd use the name of a normal, integer variable, like myInteger.

This means that any expression you could use in an assignment statement for a normal variable, you can use when assigning to an array element:

int x = 10;

myInteger = 10 + 2 * x - 7;    // assigns 23 to myInteger

m_MyArray[0] = 10 + 2 * x - 7; // assigns 23 to m_MyArray[0]


You can also write any expression that will evaluate to an integer number inside the brackets, as well.  This expression will first be evaluated, then the assignment will be done:

m_MyArray[2 - 1] = 10; // assigns 10 to slot number 1 ( 2 - 1 = 1 )


You can also get the value stored in an array element, and use it like you would any other integer:

myInteger = 3;

m_MyArray[0] = 3;


int x;

int y;


x = myInteger + 2; // assigns 5 to x

y = m_MyArray[0]+ 2; // assigns 5 to y


You can combine the 'getting data out of' syntax, and 'putting data into' syntax, on the same line, just like you would for a normal integer.  For example, one way to increment a normal integer, and an integer array element:

myInteger = myInteger + 1;

m_MyArray[0] = m_MyArray[0] + 1;


Another way to do the exact same thing, using the increment (++) operator:

myInteger++;

m_MyArray[0]++;


In addition to possible new syntax errors, using an array introduces the possibility of a new type of logical error: attempting to access an element that doesn't exist.  If you consider the 5 element array pictured above, you can clearly see that m_MyArray[0] has the value 10.  You can also try to access the element -1, like so: m_MyArray[-1].  However, there is no element with the number -1, so you'll see an error message that says something about the index being out of range.  Likewise, if you try to access m_MyArray[10], despite the fact that the array has only 5 elements, you'll get the same error message.

One very common error is to access m_MyArray[5], when has only 5 elements.  This is an error because the first element is numbered 0, the second 1, and so on, until the fifth element, which is numbered 4, not 5!  Take a close look at the above picture, and you'll see this in detail.

Having looked at some of the syntax issues around arrays, let's examine the code for this tutorial!


2. Examining The Program:

Let's examine the C# source code that produces the behavior we see on-screen

First, note that the program draws 5 soccer balls, using a loop, like you've seen in previous tutorials. We will ignore this for now, but we'll be leaving it in this program, since we will soon examine how loops & arrays naturally work together.


FURTHER EXERCISES:: 

  1. Start from a blank starter project (1000.201, if you need it), and re-do the code from memory as much as possible.  On your first try, do what you can, and keep the above code open so that when you get stuck, you can quickly look up what you forgot (and that after you finish a line, so that you can compare your line to the 'correct' line).  On the next try, do the same thing, but try to use the finished code less.  Repeat this until you can type everything, without refering the tutorial's code.
  2. Arrays and data types
    For this exercise, you should use the same project that was explained in the above tutorial.
    Go through the code, and change the data type of the array elements from being integers, to being floating-point numbers.
  3. Different Array Sizes
    For this exercise, you should use the same project that was explained in the above tutorial.
    1. Working off the code presented in the above tutorial, you should change the size of the array from 5 elements, to 8.  Make sure that you update the message at the bottom of the screen, so that it displays all the elements of the array.
    2. Working off the code presented in the above tutorial, you should change the size of the array to be exactly 4 elements, and update the program appropriately.
  4. IndexOutOfRangeException
    For this exercise, you should use the same project that was explained in the above tutorial.
    In the provided tutorial code, in the InitializeWorld method, try changing the following line:
        m_MyArray[0] = 10;

    So that it attempts to assign the value 10 to element -1, or to element 10, or to the element with the index of 5.  For each of these, note carefully what happens, as this is a very distinctive error message - being able to quickly recognize this, and fix this sort of error, is a valuable skill to have.


Project home page: The Game-Themed Introductory Programming Project.
Kelvin Sung
Computing and Software Systems
University of Washington, Bothell
ksung@u.washington.edu
Michael Panitz
Business And Information Technology
Cascadia Community College
mpanitz@cascadia.eduu

Microsoft Logo This work is supported in part by a grant from Microsoft Research under the Computer Gaming Curriculum in Computer Science RFP, Award Number 15871 and 16531.
2/8/2010