120 likes | 205 Views
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.
E N D
16.216ECE 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 ECE Application Programming: Lecture 31
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
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
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
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
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
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
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
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
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
Next time • File I/O ECE Application Programming: Lecture 31