EECE.2160 ECE Application Programming Instructor: Dr. Michael Geiger Fall 2016 Lecture 21 Arrays and functions
ECE Application Programming: Lecture 21 Lecture outline Announcements/reminders Program 6 due 11/2 Programs submitted between 11/3 and 11/7 1 day late Exam 2 in class 11/4 Will be allowed one double-sided 8.5” x 11” note sheet Review One-dimensional arrays Two-dimensional arrays Today’s lecture Arrays and functions 4/28/2019 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 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}; Compiler will determine size of array from list If initialization list has fewer values than size given, remaining values = 0 i.e. int list[5] = {1, 2, 3} same as int list[5] = {1, 2, 3, 0, 0} Must be sure to access inside bounds You can access x[12] or x[-1], for example Will access whatever’s at those locations 4/28/2019 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] 4/28/2019 ECE Application Programming: Lecture 21
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; } 4/28/2019 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 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]++; 4/28/2019 ECE Application Programming: Lecture 21
Passing arrays to functions Do not need to specify array size (for reasons I’ll explain shortly) Compiler will actually ignore 1-D 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[ ], int n); n = # elements in array 4/28/2019 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Example Write a function for each of the following findAvg(): Given an array of doubles (arr) and the # of elements in the array (n), find the average of all array elements findMax(): Given an array of ints (arr) and the # of elements (n), find the largest (i.e., most positive) 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 4/28/2019 ECE Application Programming: Lecture 21
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; } 4/28/2019 ECE Application Programming: Lecture 21
Passing Arrays to functions (findBig) //******************************************* // function findMax // On Entry: // arr[] - array with values // n - number of elements to examine // On Exit: // returns biggest (most positive value in // the first n elements of test[] int findMax(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; } 4/28/2019 ECE Application Programming: Lecture 21
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 points void SclAry(int test[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } 4/28/2019 ECE Application Programming: Lecture 21
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 test[], int n, int s) { int i; for (i=0; i<n; i++) test[i]=test[i]+s; // or use test[i]+=s; } 4/28/2019 ECE Application Programming: Lecture 21
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 ??? 4/28/2019 ECE Application Programming: Lecture 21
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]) 4/28/2019 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! Array arguments to functions are always passed by address 4/28/2019 ECE Application Programming: Lecture 21
ECE Application Programming: Lecture 21 Final notes Next time Character arrays and strings Reminders: Program 6 due 11/2 Programs submitted between 11/3 and 11/7 1 day late Exam 2 in class 11/4 Will be allowed one double-sided 8.5” x 11” note sheet 4/28/2019 ECE Application Programming: Lecture 21