1 / 11

16.216 ECE Application Programming

16.216 ECE 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

sharis
Télécharger la présentation

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 Spring 2013 Lecture 20: 2D arrays and functions

  2. 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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

More Related