lecture10-feb11

lecture10-feb11 - Announcements Lecture 10 Assignment 2 Q&A...

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

View Full Document Right Arrow Icon
1 Announcements Lecture 10 • Assignment 2 – Q&A – Keep track of the following assignment 2 stats • SLOCs • Hours spent • Logic defects encountered • Exam 1 next Friday in class (Ch. 1-9) • Topics for today – More Arrays, 2D, 3D… – Matrices – not in the book Two-Dimensional Arrays • Used for tables and matrices • Declaration similar to one dimensional arrays • Need to specify both the number of rows and columns during allocation • For example: int myMatrix [10] [10]; double energyTable [5] [6]; char conversionTable [26] [26]; 2D Arrays • Example: /* for a 10x10 matrix ±ll in the diagonal with 1’s; 0’s otherwise */ #de±ne ROWS 10 #de±ne COLS 10 . . . int m[ROWS][COLS], row, col; for (row = 0; row < ROWS; row++) {for (col = 0; col < COLS; col++) { if (row == col) m[row][col] = 1; else m[row][col] = 0; } } 1 1 1 1 1 1 1 1 1 1 row col 2D array m What’s the sizeof(m) ? A 2D Array in memory energyTable #define ROWS 5 #define COLS 6 . . . double energyTable [ROWS][COLS]; “pointer” Stored in row major order Energy Sources Years 1989 1990 1991 1992 1993 Coal Gas Oil Hydro Nuclear Other Energy Table Nested for Loops • Nested loops are frequently used to process two- dimensional arrays • Often the body of inner loop is where the main computation is done • Example idiom: for (i = 0; i < ROWS; i++) { /* done before the inner loop*/ for (j = 0; j < COLS; j++) { /* body of inner loop operates on the elements*/ } /* done after the inner loop */ } By convention we usually refer to the speci±c elements as energyTable [i, j], where i is the row index and j is the column index
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Reading in values for energyTable #define ROWS 5 #define COLS 6 . . . double energyTable [ROWS][COLS]; int i, j; /* reads in numbers needed to fill up the entries in the energyTable one row at a time */ for (i = 0; i < ROWS; i++) for (j = 0; j < COLS; j++) { scanf (“ %lf”, energyTable[i][j]); } Using a Nested Initializer List double energyTable [ROWS] [COLS] = { {18.9, 19.4, 34.2, 3.9, 5.7, 0.3}, {19.1, 19.3, 33.6, 3.0, 6.2, 0.2}, {18.8, 19.6, 32.9, 3.1, 6.6, 0.2}, {18.9, 20.3, 33.5, 2.8, 6.7, 0.2}, {19.6, 20.8, 33.8, 3.1, 6.5, 0.2} }; Computing Year (Row) Totals double yearTotals [ROWS]; /* holds row totals */ . . .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/24/2010 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas.

Page1 / 5

lecture10-feb11 - Announcements Lecture 10 Assignment 2 Q&A...

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

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