{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture10-feb11

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

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

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 fill in the diagonal with 1’s; 0’s otherwise */ #define ROWS 10 #define 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 specific elements as energyTable [i, j], where i is the row index and j is the column index

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

View Full Document
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 */ . . .
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern