XNA Game-Themed CS1 Examples (XGC1)

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

Topic: Topic.6.Arrays
Example: Ex_9.CopyAndParameter

Arrays: Passed as a Parameter; Copying arrays


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:

In this tutorial, the program that you can run closely resembles the program that you ran in the previous tutorial.  What's different is the message at the top of the screen.  The program displays the sum of all the hit counts as "total=341", followed by the sum of one half of all the hit counts (in this case, 168).  Similarly, the average is calculated on all the hits, and then calculated on the halved copy of the number of hits.  Likewise, in the above picture, the minimum number of hits is 6, and the minimum of the halved number of hits is 3, while the maximum is 116, and maximum of the halved numbers is 58.

The two major topics for this tutorial are

  1. Passing an array to a function, and

  2. Creating a copy of an array within a function, and returning that copy back to the calling function

A minor change to the game is that the blocks are now initialized so that they start with -1 hits (instead of zero hits).


2. Examining The Program: Passing An Array To A Function (As A Parameter)

Let's examine the C# source code that produces the behavior we see on-screen, starting with the code that's specific to passing arrays as parameters (to functions).


3. Examining The Program: Returning An Array From A Function

Let's examine the C# source code that produces the behavior we see on-screen, starting with the code that's specific to creating a copy of the hits array, and returning it.


You'll notice that when the program runs, the game displays (essentially) two sets of numbers across the top.  The second set of numbers is calculated using a copy of the the m_BlockHits array, except that in the copy, all the numbers are halved.  We accomplish this by creating another array that is a copy of the m_BlockHits array, going through, and halving the value of each element in the array.


You will also notice that because our four functions (SumOfArray, AverageOfArray, MinInArray, and MaxInArray) all accept an array as a parameter, this means that we can reuse those functions again here.  This is much better than creating a duplicate set of nearly-identical functions that do almost the same thing except on a different array.


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 referring to the tutorial's code.
  2. Examining The AvergeOfArray() Function
    For this exercise, you should use the same project that was explained in the above tutorial.
    Notice that the numbers stored in the intArray array are integers, and that SumOfArray returns an integer.  Notice also that we are specifically declaring total to be a float, not an int.  For the version of the function (which is subtly different from the AverageHits in the previous tutorial), is it strictly necessary to declare total to be a float?
  3. Examining The CopyArray() Function
    For this exercise, you should use the same project that was explained in the above tutorial.
    In UpdateWorld, when the program first calculates the sum, average, etc on the original array, and then goes back and calculates these values for the new, 'halved' array.   Why are all the values in the 'halved' array half of what they are in the original, rounded down?
  4. Creating a IncrementAll function
    For this exercise, you should use the same project that was explained in the above tutorial.
    For this exercise, you should create a function named IncrementAll, which will be given an integer array as a parameter, and will go through and increase the value of each element by one.  Try modifying your game so that UpdateWorld calls this function.

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