Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE Application Programming

Similar presentations


Presentation on theme: "ECE Application Programming"— Presentation transcript:

1 16.216 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2012 Lecture 32: 2-dimensional arrays

2 ECE Application Programming: Lecture 32
Lecture outline Announcements/reminders Program 7 due today Program 8 to be posted ASAP Grading on previous programs to be completed ASAP Today Two-dimensional arrays 6/12/2018 ECE Application Programming: Lecture 32

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 Col. 0 Col. 1 Col. 2 Col. 3 Row 0 x[0][0] x[0][1] x[0][2] x[0][3] Row 1 x[1][0] x[1][1] x[1][2] x[1][3] Row 2 x[2][0] x[2][1] x[2][2] x[2][3] 6/12/2018 ECE Application Programming: Lecture 32

4 ECE Application Programming: Lecture 32
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} }; 1 2 3 4 5 6 7 8 9 10 11 12 6/12/2018 ECE Application Programming: Lecture 32

5 ECE Application Programming: Lecture 32
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! 6/12/2018 ECE Application Programming: Lecture 32

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} }; int negCnt[NRows] = {0}; // Initialize entire row count array to 0 int i, 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; } 6/12/2018 ECE Application Programming: Lecture 32

7 ECE Application Programming: Lecture 32
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]++; 6/12/2018 ECE Application Programming: Lecture 32

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(int arr[][4], int n); int main() { int x[3][4]; f(x, 3); ... } 6/12/2018 ECE Application Programming: Lecture 32

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 6/12/2018 ECE Application Programming: Lecture 32

10 ECE Application Programming: Lecture 32
Example solution void studentAvg(double grades[][3], int nStudents, double averages[]) { int i, 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; 6/12/2018 ECE Application Programming: Lecture 32

11 Example solution (cont.)
void examAvg(double grades[][3], int nStudents, double averages[]) { int i, 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; 6/12/2018 ECE Application Programming: Lecture 32

12 ECE Application Programming: Lecture 32
Next time File I/O 6/12/2018 ECE Application Programming: Lecture 32


Download ppt "ECE Application Programming"

Similar presentations


Ads by Google