190 likes | 304 Views
16.216 ECE Application Programming. Instructor: Dr. Michael Geiger Fall 2011 Lecture 28: More on arrays. Lecture outline. Announcements/reminders Program 6 due Monday, 11/21 Clarification: epsilon always equals 0.0001 Program 7 posted; due Friday, 12/2
E N D
16.216ECE Application Programming Instructor: Dr. Michael Geiger Fall 2011 Lecture 28: More on arrays
Lecture outline • Announcements/reminders • Program 6 due Monday, 11/21 • Clarification: epsilon always equals 0.0001 • Program 7 posted; due Friday, 12/2 • Exam 2 regrade requests due in writing by 11/21 • Make sure small error wasn’t penalized excessively • No PE Monday; just character arrays/strings • Today • Review array basics • Working with arrays • Arrays and functions • Arrays and pointers ECE Application Programming: Lecture 28
Review: arrays • Arrays: groups of data with same type • x[10] has 10 elements, x[0] through x[9] • Can also define with initial values • e.g. double list[] = {1.2, 0.75, -3.233}; • Arrays must be declared with constant size • Must be sure to access inside bounds ECE Application Programming: Lecture 28
Passing arrays to functions • Do not need to specify array size (for reasons I’ll explain shortly) • Compiler will actually ignore array size, even if you put it in prototype • Therefore cannot check array size inside function • Prototype typically has array name and brackets to indicate you’re dealing with array • e.g. int findAvg(int arr[ ]); ECE Application Programming: Lecture 28
Example • Write a function for each of the following • Given an array of doubles (arr) and the # of elements in the array (n), find the average of all array elements • Given an array of ints (arr) and the # of elements (n), find the largest element in the array • Given an array of test scores (tests), the # of elements in the array (n), and an amount to scale those scores by (s), add s to every element in tests • Do not print scores in function; we’ll print in main program ECE Application Programming: Lecture 28
Passing Arrays to functions (findAvg) //*******************************************// function findAvg// On Entry:// arr[] - array with values to avg// n - number of values to avg// On Exit:// returns avg of first n elements of test[]double findAvg(double arr[], int n){ int i; double sum=0; double avg; for (i=0; i<n; i++) sum+=arr[i]; avg = sum / n; return avg;} ECE Application Programming: Lecture 28
Working with Arrays (find biggest) // WAY 1-initialize biggest one so// far to zerobig=0;for (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ??? What happens if there are only negative values in the array ??? ECE Application Programming: Lecture 28
Working with Arrays (find biggest) // WAY 2-initialize biggest one so// far to smallest possible valuebig= -2147483648; for (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ???What happens if program is ported to another system?????? (where integers are stored in 2 or 8 bytes)??? ECE Application Programming: Lecture 28
Working with Arrays (find biggest) // WAY 3-initialize biggest one so// far to smallest possible valuebig= INT_MIN; // BUT use symbolfor (i=0; i<8; i++){ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 The above works, but there is one more (slightly better) way ECE Application Programming: Lecture 28
Working with Arrays (find biggest) // WAY 4-initialize biggest one so// far to the first array elementbig= x[0];for (i=1; i<8; i++) // start at 1{ // if x[i] > biggest one so far if (x[i]>big) { // make that the "new" biggest big=x[i]; } }printf("Biggest is %d\n",big); x[0] 80 x[1] 75 x[2] 90 x[3] 100 x[4] 65 x[5] 88 x[6] 40 x[7] 90 ----- ECE Application Programming: Lecture 28
Passing Arrays to functions (findBig) //*******************************************// function findBig// On Entry:// arr[] - array with values// n - number of elements to examine// On Exit:// returns biggest value in the first n // elements of test[]int findBig(int arr[], int n){ int i, big; big = arr[0]; for (i=1; i<n; i++) if (arr[i]>big) big = arr[i]; return big;} ECE Application Programming: Lecture 28
Passing Arrays to functions (SclAry) //*******************************************// function SclAry// On Entry:// tests[] - array with values to scale// n - number of values to scale// s - number of points to scale// On Exit:// The first n values of tests[] are// scaled by s pointsvoid SclAry(int tests[], int n, int s){ int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } ECE Application Programming: Lecture 28
Passing Arrays to functions (SclAry) #include <stdio.h>void SclAry(int tests[], int n, int s);void main(void){ int i; int x[]={ 51,62,73,84,95,100,66,57,48,79 }; int N=sizeof(x)/sizeof(int); SclAry(x,N,10); for (i=0; i<N; i++) printf("%4d",x[i]); printf("\n");} void SclAry(int tests[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } ECE Application Programming: Lecture 28
Passing Arrays to functions (SclAry) Output of program: 61 72 83 94 105 110 76 67 58 89 For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ??? ECE Application Programming: Lecture 28
Passing Arrays to functions (SclAry) Output of program: 61 72 83 94 105 110 76 67 58 89 For reference: int x[]={ 51,62,73,84,95,100,66,57,48,79 }; ??? What's wrong with this picture ??? The array in the main program was UPDATED ... (say "Hmmmm")Does this seem contrary to all we know about functions? (say "Yes")Is this how it really works? (Yep, it is)Is your head getting ready to explode? (say "Almost") ??? SO WHAT IS GOING ON ??? ECE Application Programming: Lecture 28
Passing Arrays to functions Before call to SclAry After call to SclAry test[0] 51 3044 test[0] 61 3044 test[1] 62 3048 test[1] 72 3048 test[2] 73 304C test[2] 83 304C test[3] 84 3050 test[3] 94 3050 test[4] 95 3054 test[4] 105 3054 test[5] 100 3058 test[5] 110 3058 test[6] 66 305C test[6] 76 305C test[7] 57 3060 test[7] 67 3060 test[8] 48 3064 test[8] 58 3064 test[9] 79 3068 test[9] 89 3068 Passing the name only (i.e. test vs. test[4]) passes the ADDRESS of element zero of the array. Put another way: myfunc(ary) same as myfunc (&ary[0]) ECE Application Programming: Lecture 28
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 28
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 28
Next time • Character arrays and strings ECE Application Programming: Lecture 28