# Computer Programming/Arrays

This lesson introduces arrays.

## Objectives and Skills

Objectives and skills for this lesson include:

• Understand single and multi-dimensional arrays
• Understand dynamic arrays
• Understand associative arrays
• Use arrays to implement program functionality

## Activities

1. Review MathsIsFun: Definition of Average. Create a program that asks the user to enter grade scores. Start by asking the user how many scores they would like to enter. Then use a loop to request each score and add it to a static (fixed-size) array. After the scores are entered, calculate and display the high, low, and average for the entered scores. Use separate subroutines/functions/methods to implement input, each type of processing, and output. Avoid global variables by passing parameters and returning results.
2. Review MathsIsFun: Leap Years. Create a program that asks the user for a year, and then calculate whether or not the given year is a leap year. Build an array where each entry is the number of days in the corresponding month (January = 31, February = 28 or 29 depending on year, March = 31, etc.). Build a parallel string array with the names of each month. Then ask the user to enter a month number, and look up the corresponding month name and number of days and display the information. Continue accepting input and displaying results until the user enters a number less than 1 or greater than 12. Use separate subroutines/functions/methods to implement input, each type of processing, and output. Avoid global variables by passing parameters and returning results.[1]
3. Review Wikipedia: Zeller's congruence. Create a program that asks the user for their birthday (year, month, and day) and then calculate and display the day of the week on which they were born. Use an array lookup to convert the numeric day of the week to the correct string representation (Monday, Tuesday, Wednesday, etc.). Use separate subroutines/functions/methods for input, each type of processing, and output. Avoid global variables by passing parameters and returning results.
4. Review Wikipedia: Monty Hall problem. Create a program that uses an array to simulate the three doors. Use 0 (zero) to indicate goats and 1 (one) to indicate the car. Clear each "door" and then use a random number function to put the number 1 in one of the array elements. Then use the random number function to randomly select one of the three elements. Run the simulation in a loop 100 times to confirm a 1/3 chance of winning. Then run the simulation again, this time switching the selection after a 0 (goat) is removed from the remaining choices. Run the simulation in a loop 100 times to confirm a 2/3 chance of winning by switching.
5. If your programming language supports it, update one or more of the programs above to replace the static array with a dynamic array, and extend the array as each item is added to the array.