Programming Projects Using Arrays

Not long ago someone posted a query to the AP CS list serve looking for projects that use arrays for them to use in their Visual basic programming course. The response was impressive. I decided to collect them all (with attribution to to people who sent them to the list) because I think it is a useful resource. These projects can pretty much be used with any programming language and range from very easy to a little more advanced. So if you are looking for some additional projects here’s a list. (BTW I played with the formatting a little to make it work on the web and fixed a typo or two but otherwise cut/pasted. All errors should be blamed on me though.)

I do the project Alfred suggested below after I do a simple Cypher project. I never liked the fact that character arrays are not in the AP curriculum and I've always taught them. So, given an array of characters, you can encrypt that array in one of two ways (or you could use StringBuilder setCharAt method):

  1. 1. Add or subtract k to the characters that are letters (use Character.isLetter) and loop around so that nothing falls off the alphabet.
  2. 2. Create an array of characters of size 26 that defines which letters get replaced with which ones. For instance, if char[] code = {'c', 'q', t', ..., 'a'} then 'a' gets changed to c, 'b' gets changed to q, ..., 'z' gets changed to 'a'.

A nice aspect of this method is the challenge of decrypting. Once we encrypt some text files, the question of how to crack a code is natural and the assignment below is excellent.

L. LaSpina, Math & Computer Science, Bethpage High School

The project I suggested and Leon referred to is one I have used for years. If fact I am pretty sure I wrote a project like this as an undergraduate so I don’t claim it is original with me by any means:

I like to use arrays to count the occurrences of individual letters in text input. An array of 26 integers will do for case insensitive counting (in English anyway). After counting the letters they can display things like the most common letters, letters that do not appear, counts for each letter, etc.

Games suggest a lot of ideas like these:

I provide kids with a word list (Scrabble) and ask them more questions than they can answer about the English language. (How many words, longest words, list of palindromes, etc.) I can send the lab if you want.

Also, I wrote some graphics classes to model the Lights Out Game and I have the kids write the Board class, which is a 2-D array. They like this because they can play the game when they are done. Again, I am happy to send it if you contact me.

Jake Tawney, Columbus, Ohio

This game from Matthew Harbinger is also based on a game. I have used it myself in Visual basic courses. Kids have fun testing it:

Because you said VB, I'll share my VB array project. Students place 20 pictureboxes on the form and declare an array to hold 20 images. They shuffle the array, and a separate picturebox on the screen shows the first image in the array. They have to find its match on the form. When they click its match, the separate picturebox then shows the next image in the array... They must have a time limit and it resembles the kids game Perfection. They enjoy coming up with a theme and having a game we can play on the Smartboard.

Loops and arrays go together like hand in glove as you can see from these two examples.

Well, I happen to be smack dab in the middle of arrays in my Intro (Java) class right now. A couple things I've done:

  • Linear Search & Adding two arrays (both integer and String)
  • And for the next few days I'm showing them nested loops and thus, 2-D arrays, which they will search, and use nested loops to create things like:

*
**
***
****
*****

Gary Gongwer, Moreau Catholic High School, Hayward CA

A couple of more involved examples from Christian Day including some talk of Big O notation and search efficiencies.

You could start setting them up for binary search and more efficient data structures by having them perform something relatively complex (e.g. which word/number appears most frequently in this array of words/numbers? - a O(n^2) algorithm with array) and display a count of the number of times each individual array item is looked at.

Other ideas:

  • Find the last index of a value in the array
  • Store a coded message in an array by storing the letters/words/numbers you are interested in in some series (e.g. the Fibonacci series, primes only, etc.), then write methods to extract the data.

Of course, if they've hit all of the topics you mention, they may be ready for insertion and deletion. Good luck. Have fun

Dice and randomness often adds to the fun as in these ideas from my good friend Debbie Carter. The addition of graphing is a great interest builder and allows for some more complex projects later on in a course.

Use the elements of an array to tally a series of events. (element number = numeric result of event)

For example, when rolling two dice, increment the element whose number is the sum of the values on the dice (so your tallies will be stored in elements numbered 2 through 12). Students can then display the results in a bar graph (or histogram).

Debbie Carter, Secretary, CSTA - Northern NJ Chapter, Teacher of Computer Science & Mathematics Mt. Olive High School Flanders, NJ

Brian Spotts also suggested a dice related project that honestly I think could use more detail but maybe it makes sense to you.

Have them roll a die a number of times and count them.

for (x = number of rolls)

count[randnum]++;

The “random walk” is actually a very common and useful concept for computer science students to learn. Here is one such idea:

I have an exercise where students set up a simple int array and then simulate a "random" walk using a coin flip. The initial position is the middle of the array and if the coin flip is heads (0) move 1 step to the right, tails (1) move one cell to the left. Each time they move into a cell the cell is incremented. After flipping the coin a given number of times they then need to print out the total number of times the cell was occupied. I often make them print out "*" for each visit so that they can see the random walk graphically. I use this exercise to get at a number of topics: random numbers, how a random process can deviate from the expected normal distribution, moving from cell to cell in an array, incrementing values in an array, how to deal with moving out of bounds of the array, initializing and manipulating arrays, using the values in an array to control what is printed. It's fairly easy to set up and run and the results are always though provoking.

George Murkowski, Upper School Technology Facilitator, The Wheeler School

Mathematical challenges are often good as are exercises that requires any kind of searching for thing;

Another project might be something along the lines of "Given an array of integers (maybe using Math.random()), find the greatest common factor (GCF), calculate the median, or average, or something along those lines.

For something a bit more challenging, you could implement a simple "lookup code" array, where a sample string can be encoded by the use of a secondary String array as a lookup table.

Sample: Encode the String "hello" by using the nth letter "above" each letter as an encryption scheme...e.g. five letters above "h" is "m". Repeat this for all letters in the array to encode the String.

Michael Lew, Loyola High School, Los Angeles, CA 90006

Leigh Ann Sudol-DeLyser, Carnegie Mellon University, posted some of her favorite ideas on her blog - Additional Activities with Arrays She goes into some helpful detail so rather than cut/past from her blog I recommend you go read them there. And follow her blog – she has lots of great ideas.

Lockers! Now there is a subject most high school students can relate to. I like that this project as a sort of puzzle aspect to it as well.

Locker Problem (arrays) – Write a program to simulate the following experiment. You have 100closed lockers. Start with the first locker and for every locker open it. Now, starting with the second locker, for this locker and every second locker after that, if the locker is open, close it; if it is closed, open it. Then, starting with the third locker, for this locker and every third locker after that, if the locker is open, close it; if it is closed, open it. Continue with the nth locker changing the “position” of every nth locker until n = 100. Print out the locker numbers that remain open. What do you notice about the lockers that remain open? Can you explain your results?

Evelyn Torres-Rangel

Duck, Duck, Goose is a cute little kids game but I think high school kids secretly miss it. This project lets them “play” with the game on the computer a bit.

I have my students do a version of "Duck Duck Goose". I don't remember where I got it from. The user enters the number of players (>= 1) (I have them do an input-check loop here to be sure the number is >=0). A Boolean array is set up with all elements being initialized to true. Every third player is eliminated, only counting players that are still true. They need to keep track of the number of "trues" until it gets to 3, and change that player to false. Also need to skip over falses. Also need to deal with reaching the end of the array and going back to the beginning. (FYI - start tracking variable at -1) Eliminate players one less time than the size of the array - the element that is still true is the winner, but don't forget to add one to the subscript! (Ideas for extensions: redo with ArrayLists - can eliminate element all together, and I also have my Data Structures class redo the lab with a hard-coded circular linked list. Our student files are not cleaned out year to year so they can go back to the original.)

After I wrote the program, I put it in a loop 1 - 100 players and printed a chart of number of players and the winner # so they (and I) have test data. Before assigning this lab, I have several students come to the front of the class and use post-its to put numbers on them and run a simulation, turning a student away from the class when they are false.

Bonnie Sturm, Parsippany Hills HS, Parsippany, NJ

I wrote a grading program for my project books and here Adam Michlin gives a quick version of one.

I like to have students create a grading program. First project is storing and retrieving one grade for students (1D array... studentGrades[studentNumber]). After that, I expand to 2D arrays and have them store and retrieve grades for multiple assignments/grades (studentGrade[studentNumber][assignmentNumber]).

-Adam Michlin

Baker Franke suggested a project that was recently presented on the Nifty Assignments talk at SIGCSE called Evil Hangman. You can read the official explanation at the Nifty Assignments archive. Baker’s explanation is:

The computer suggests a word for a simple game of hangman, and the player guesses letters and is only allowed a certain number of incorrect guesses...

BUT...the computer is EVIL. The program just finds words of the given length that don't have any of the characters suggested by the

player. The computer can almost always win.

So, To START:

The player suggests a word length and a number of guesses allowed

Then the program should read a list of words of the given length from a file into an array (though other structures might be more appropriate in a big-oh sense).

Each time the player suggests a letter, the program should remove any words from the list that have that letter in it.

With enough guesses, the you'd eliminate all words, so make sure that at least one word is left in the list, and from that point forward it's just regular hangman.

Well there is a start. Note that you can get my Visual Basic Project for the Classroom (PDF) and my Visual C# Projects for the Classroom books with a number of classroom tested projects. I also wrote an article on creating and using arrays of controls for both C# and Visual Basic that you can find on my web site – Creating and Using Control Arrays