16.216 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2012 Lecture 21: 2D arrays and functions
ECE Application Programming: Lecture 21 Lecture outline Announcements/reminders Program 5 due today Program 6 to be posted, due 11/2 Exam 2: Wednesday, 11/7 Advising: Monday, 10/29 through Tuesday, 11/13 Today’s class Review Two dimensional arrays Arrays and functions Pointer arithmetic Two dimensional arrays and functions 9/13/2018 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Review 2D arrays: declared similarly to 1D arrays Example (see below): int x[3][4]; Index elements similarly to 1-D arrays Initialize: int y[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; Typically used with nested for loops 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] 9/13/2018 ECE Application Programming: Lecture 21
Review: arrays and pointers Array name is a pointer Arrays are always passed by address to functions Can use pointer to access array Can use pointer arithmetic to move pointer through array 9/13/2018 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Arrays and pointers Array name is a pointer to first array element Can use pointers and arrays interchangeably You can use [] to “index” a pointer Example: int myArr[] = {1, 3, 5, 7, 9}; int *aPtr; aPtr = myArr; for(int i =0; i < 5; i++) printf(“%d”, aPtr[i]); What does this print? 1 3 5 7 9 contents of array! 9/13/2018 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Pointer arithmetic When using pointers/arrays interchangeably, can make use of pointer arithmetic Can’t change where array name points, but you can change pointer If p is a pointer, p++ means “point to next element” “Next element” determined by base type Can compare pointers p == NULL pointer points nowhere p == q p and q point to same location Example int num[4] = {1,2,3,4}, *p; p = num; //same as p = &num[0]; printf(“%d\n”, *p); ++p; 9/13/2018 ECE Application Programming: Lecture 21
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); ... } 9/13/2018 ECE Application Programming: Lecture 21
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 9/13/2018 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 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; 9/13/2018 ECE Application Programming: Lecture 21
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; 9/13/2018 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Next time Character arrays and strings Reminders: Program 5 due 10/26 Program 6 to be posted; due 11/2 Exam 2: Wednesday, 11/7 Advising: Monday, 10/29 through Tuesday, 11/13 9/13/2018 ECE Application Programming: Lecture 21