110 likes | 214 Vues
Learn about 2D arrays, functions, pointers, and pointer arithmetic in application programming. Understand how to manipulate arrays and pointers interchangeably. Example solutions for calculating student exam averages. Prepare for upcoming exam.
E N D
16.216ECE Application Programming Instructor: Dr. Michael Geiger Spring 2013 Lecture 20: 2D arrays and functions
Lecture outline • Announcements/reminders • Program 6 due today • Program 4, 5 regrades due 3/25 • Exam 2 Wednesday, 3/27 • Program 7 posted; due 4/1 • Today’s class • Review • Two dimensional arrays • Arrays and functions • Pointer arithmetic • Two dimensional arrays and functions 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 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 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! 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; printf(“%d\n”, *p); 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(intarr[][4], int n); int main() { int x[3][4]; f(x, 3); ... } 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 ECE Application Programming: Lecture 21
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] = grades[i][0]; // Initialize sum for (j = 1; j < 3; j++) { averages[i] += grades[i][j]; } averages[i] /= 3; } } ECE Application Programming: Lecture 21
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] = grades[0][j]; // Initialize sum for (i = 1; i < nStudents; i++) { averages[j] += grades[i][j]; } averages[j] /= nStudents; } } ECE Application Programming: Lecture 21
Next time • Exam 1 Preview • Reminders: • Program 6 due today • Program 4, 5 regrades due 3/25 • Exam 2 Wednesday, 3/27 • Program 7 posted; due 4/1 ECE Application Programming: Lecture 21