21multiDarrays_3014

21multiDarrays_3014 - Lecture File 21 COP 3014 January 17,...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture File 21 COP 3014 January 17, 2008 A. Ford Tyson 1 COP 3014 Lecture File 21 ± Multi-Dimensional Arrays 1 – our focus: 2-Dimensional Arrays Copyright 1997-present, Ann Ford Tyson Two-Dimensional Arrays 1 -D Array -l ist - vector 2 2-D Array - table, chart -game board - matrix, photographic image - spreadsheet etc. Sudoku Game: www.sudoku.com 3 " Fill in the grid so that every row, every column, and every 3x3 box contains the digits 1 through 9. "
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 21 COP 3014 January 17, 2008 A. Ford Tyson 2 scores [ 0 ] scores [ 1 ] review: const int NUMSCORES = 50; int scores a 1-D array of int 4 scores [ 49 ] scores int scores [NUMSCORES]; a 2-D array of char (tic-tac-toe) O X X X X O 5 O O X example: a 2-D array of double ± idea: record amount spent each day of the week on food, school supplies, and entertainment 6 ± const int NUMDAYS = 7; const int NUMCATEGORIES = 3; typedef double BudgetTable [NUMDAYS] [NUMCATEGORIES]; BudgetTable budget ; int row, column ;
Background image of page 2
Lecture File 21 COP 3014 January 17, 2008 A. Ford Tyson 3 budget indices 0 ( food ) 1 ( supplies ) 2 ( entertain. ) 0 ( Sun ) 1 ( Mon ) 2 ( Tue ) 25.00 3 ( Wed ) 4 ( Thur ) 7 5 ( Fri ) 6 ( Sat ) • there are 7 X 3 == 21 cells • budget[row][column] accesses 1 element • budget[2][0] = 25.00; would set the cell shown accessing the 2-D budget array indices 0 ( food ) 1 ( supplies ) 2 ( entertain. ) 0 ( Sun ) 0,0 0,1 0,2 1 ( Mon ) 1,0 1,1 1,2 2 ( Tue ) 2,0 2,1 2,2 3 ( Wed ) 3,0 3,1 3,2 4 ( Thur ) 4,0 4,1 4,2 8 5 ( Fri ) 5,0 5,1 5,2 6 ( Sat ) 6,0 6,1 6,2 • these are the indices to each cell, shown in the form row, column some options p.1 ± initialize all values to zero in declaration BudgetTable budget = { 0.0 }; 9
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lecture File 21 COP 3014 January 17, 2008 A. Ford Tyson 4 some options p.2 ± declare same structure as an array of arrays // declare a type representing one row typedef double Categories [NUMCATEGORIES]; 10 // declare a type for the entire 2D array typedef Categories BudgetTable2 [NUMDAYS]; // declare a variable which is a 2D array BudgetTable2 budget2; some options p.3
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

21multiDarrays_3014 - Lecture File 21 COP 3014 January 17,...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online