1 / 12

16.216 ECE Application Programming

16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 31: Two-dimensional arrays. Lecture outline. Announcements/reminders Program 6 regrades due 11:59 PM today Program 7 due Friday, 12/2 Program 8 posted; due Friday, 12/9 Today Two-dimensional arrays.

zalman
Download Presentation

16.216 ECE Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 31: Two-dimensional arrays

  2. Lecture outline • Announcements/reminders • Program 6 regrades due 11:59 PM today • Program 7 due Friday, 12/2 • Program 8 posted; due Friday, 12/9 • Today • Two-dimensional arrays ECE Application Programming: Lecture 31

  3. Two-dimensional arrays • Two-dimensional arrays: can be used to represent tabular data • Declaration: <type> <name>[<rows>][<cols>] • Example (see below): int x[3][4]; • Index elements similarly to 1-D arrays ECE Application Programming: Lecture 31

  4. Initializing 2D arrays • Can initialize similarly to 1D arrays, but must specify dimensions • Each row treated like a 1D array; rows separated by commas: • int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; ECE Application Programming: Lecture 31

  5. 2D arrays and loops • Typically use nested loops to work with 2-D arrays • One loop inside another: for (i = 0; i < 3; i++) { for (j = 0; j < 4; j++) { x[i][j] = y[i][j] * 2; } } • Be careful in loop body—switching your loop indices will cause trouble • Using x[j][i] would take you outside of the array! ECE Application Programming: Lecture 31

  6. Example: Working with 2-D arrays Complete this program, which counts the # of negative values in each row of a 2-D array (assume the necessary #includes are done): #define NRows 3 // # of rows #define NCols 4 // # of columns int main() { double x[NRows][NCols] = // 2-D array { { 10, 2.5, 0, 1.5}, {-2.3, -1.1, -0.2, 0}, {10.5, -6.1, 23.4, -9.2} }; intnegCnt[NRows] = {0}; // Initialize entire row count array to 0 inti, j; // Row and column indices /* INSERT CODE HERE--Visit every element in array x and count the number of negative values in each row */ // Now print the row counts for (i = 0; i < NRows; i++) printf(“Row %d has %d negative values.\n”, i, negCnt[i]); return 0; } ECE Application Programming: Lecture 31

  7. Example solution /* Code to be added to visit every element in array x and count the number of negative values in each row */ for (i = 0; i < NRows; i++) for (j = 0; j < NCols; j++) if (x[i][j] < 0) negCnt[i]++; ECE Application Programming: Lecture 31

  8. 2-D arrays and functions • When passing 2-D array to function, can omit first dimension (rows) but must list columns • Example: // Assume n = # of rows int f(intarr[][4], int n); int main() { int x[3][4]; f(x, 3); ... } ECE Application Programming: Lecture 31

  9. Example: 2-D arrays and functions • Say we have a program that stores student exam scores in a 2-D array: • Each row represents an individual student • Each column represents one of the 3 exams • Write functions to: • Calculate the exam average for each student and store it in a 1-D array that is accessible in the main program • Assume all exams have equal weight • Calculate the average for each exam and store it in a 1-D array that is accessible in the main program • Each function takes the same arguments: • The 2-D array • The # of students in the class • The 1-D array that will be used to hold the averages ECE Application Programming: Lecture 31

  10. Example solution void studentAvg(double grades[][3], intnStudents, double averages[]) { inti, j; // Row/column # /* Go through each row, sum all columns, and divide by 3 to get each student’s avg */ for (i = 0; i < nStudents; i++) { averages[i] = 0; // Initialize sum for (j = 0; j < 3; j++) { averages[i] += grades[i][j]; } averages[i] /= 3; } } ECE Application Programming: Lecture 31

  11. Example solution (cont.) void examAvg(double grades[][3], intnStudents, double averages[]) { inti, j; // Row/column # /* Go through each column, sum all rows, and divide by nStudents to get each exam avg */ for (j = 0; j < 3; j++) { averages[j] = 0; // Initialize sum for (i = 0; i < nStudents; i++) { averages[j] += grades[i][j]; } averages[j] /= nStudents; } } ECE Application Programming: Lecture 31

  12. Next time • File I/O ECE Application Programming: Lecture 31

More Related