Multidimensional Array Sample Projects
Grading Program Objective: write a program that grades multiple-choice test
Grading Program Patterns: – Iterate 2D array – Sum up rows Modifications: – Break into functions – Record student scores in arrays – Get rid of i/j?
Nearest Points Rows : Points Cols : x, y
Nearest Points Patterns: – Not all array dimensions going to be looped over – Iterate through one array twice Compare item i to item i+1…end – Find smallest pattern
Nearest Points Modification: – Clean up indices – Constants for x and y instead of magic numbers: VS:
Sudoku Each number must be unique in Row/Col/Square
Sudoku Each number must be unique in Row/Col/Square
Sudoku Each number must be unique in Row/Col/Square
Map column to square 0 / 3 = 0 1 / 3 = 0 2 / 3 = 0 3 / 3 = 1 4 / 3 = 1 5 / 3 = 1 6 / 3 = 2 7 / 3 = 2 8 / 3 = 2 Sudoku
Sudoku Squares start at 0, 3, 6 0 / 3 * 3 = 0 1 / 3 * 3 = 0 2 / 3 * 3 = 0 3 / 3 * 3 = 3 4 / 3 * 3 = 3 5 / 3 * 3 = 3 6 / 3 * 3 = 6 7 / 3 * 3 = 6 8 / 3 * 3 =
Sudoku Patterns – Flag… look for reason to say false – Avoid self check
Sudoku Critique – row vs currentRow – i as parameter name
Temperature/Humidity File has day / hour / temperature / humidity Find daily averages Uses 3-D array: data[DAY][HOUR][TEMP/HUMIDITY]
Temp Patterns: – Change human numbers to array index:
Temperature/Humidity Critiques: – Don't need array for job as stated – Would two 2D arrays be better? – No
Find Local Maxima Objective : print all cells that are strictly greater than 4 direct neighbors Something is missing!!
Find Local Maxima Sanity check indices when appropriate: