Unformatted text preview:

COSC 181 Foundations of Computer Programming Class 33 1 7 4 Examples Using Arrays Cont static local arrays and automatic local arrays A static local variable in a function Exists for the duration of the program But is visible only in the function body A static local array Exists for the duration of the program Is initialized when its declaration is first encountered All elements are initialized to zero if not explicitly initialized This does not happen for automatic local arrays 2 1 Fig 7 13 f ig07 13 cpp 2 3 inc lude iostream 4 us ing std cout 5 us ing std endl Stat ic arrays are in i t ia l i z e d to zero 6 7 void staticArrayInit void function prototype 8 void automaticArrayInit void function prototype 9 10 int main 11 12 cout F ir s t ca l l 13 staticArrayInit 14 automaticArrayInit to each funct ion n Outline fig07 1 3 cpp 1 15 16 cout n nSecond ca l l 17 staticArrayInit 18 automaticArrayInit 19 cout endl to each funct ion n 20 21 return0 indicates successful termination 22 end main 3 of 3 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 funct ion to demonstrate a stat ic loca l array vo id staticArrayInit void initializes elements to 0 first time function is called static int array1 3 static local array cout nVa lues on enter ing stat icA r ray In it n output contents of array1 for int i 0 i 3 i cout array1 i array1 i cout nValues on exiting staticArrayInit n Outline fig07 1 Create a static array using keyword static 3 cpp 2 of 3 modify and output contents of array1 for int j 0 j 3 j cout array1 j array1 j 5 end function staticArrayInit function to demonstrate an automatic local array void automaticArrayInit void initializes elements each time function is called int array2 3 1 2 3 automatic local array cout n nValues on entering automaticArrayInit n Create an automatic local array 4 50 51 output contents of array2 52 for int i 0 i 3 i Outline cout array2 i array2 i 53 54 55 cout nValues on exiting automaticArrayInit n 56 57 modify and output contents of array2 fig07 1 58 for int j 0 j 3 j 59 cout array2 j array2 j 5 60 end function automaticArrayInit First call to each function 3 cpp 3 of 3 Values on entering staticArrayInit array1 0 0 array1 1 0 array1 2 0 Values on ex iting staticArrayInit array1 0 5 array1 1 5 array1 2 5 Values on entering automaticArrayInit array2 0 1 array2 1 2 array2 2 3 Values on ex iting automaticArrayInit array2 0 6 array2 1 7 array2 2 8 Second call to each function Values on entering staticArrayInit array1 0 5 array1 1 5 array1 2 5 Values on ex iting staticArrayInit array1 0 10 array1 1 10 array1 2 10 Values on entering automaticArrayInit array2 0 1 array2 1 2 array2 2 3 Values on ex iting automaticArrayInit array2 0 6 array2 1 7 array2 2 8 Values reflect changes from the previous function call the array was not reinitialized 5 7 5 Passing Arrays to Functions To pass an array argument to a function Specify array name without brackets Array hourlyTemperatures is declared as int hourlyTemperatures 24 The function call modifyArray hourlyTemperatures 24 passes array hourlyTemperatures and its size to function modifyArray Array size is normally passed as another argument so the function can process the specific number of elements in the array 6 7 5 Passing Arrays to Functions Cont Arrays are passed by reference Function call actually passes starting address of array So function knows where array is located in memory Caller gives called function direct access to caller s data Called function can manipulate this data 7 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 Fig 7 14 f ig07 14 cpp Pass ing arrays and ind iv idua l array elements to funct ions inc lude iostream us ing std cout us ing std endl Function takes an Outline array as argument fig07 1 inc lude iomanip us ing std setw 4 cpp vo id modifyArray int int appears strange void modifyElement int 1 of 3 Declare 5 int array array with initializer list int main const int arraySize 5 size of array a int a arraySize 0 1 2 3 4 initialize array a cout Effects of passing entire array by reference n nThe values of the original array are n output original array elements for int i 0 i arraySize i cout setw 3 a i cout endl pass array a to modifyArray by reference modifyArray a arraySize cout The values of the modified array are n Pass entire array to function modifyArray 8 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 Outline output modif ied array elements for int j 0 j arraySize j cout setw 3 a j cout n n nEffects of passing array element by value n na 3 before modifyElement a 3 endl Pass array element a 3 to function modifyElement modifyElement a 3 pass array element a 3 by value cout a 3 after modifyElement a 3 endl return 0 ind ica t es success fu l term inat ion end main in funct ion modifyArray b point s to the or ig ina l void modifyArray int b int sizeOfArray multiply each array element by 2 for int k 0 k sizeOfArray k b k 2 end function modifyArray fig07 1 4 cpp 2 array a in memory Function modifyArray manipulates the array directly 9 of 3 51 52 in funct ion modifyE lement e is a loca l copy of 53 array element a 3 passed from main 54 vo id modifyElement int e 55 56 multiply parameter by 2 57 cout Va lue of element in modifyE lement e 2 endl 58 end function modifyElement Effects of passing entire array by reference The values of 0 1 2 3 The values of 0 2 4 6 the original array are 4 the modified array are 8 Outline Function modifyElement manipulates array element s copy fig07 1 4 cpp 3 Effects of passing array element by value a 3 before modifyElement 6 Value of element in modifyElement 12 a 3 after modifyElement 6 10 of 3 7 6 Case Study Class GradeBook Using an Array to Store Grades Class GradeBook Represent a grade book that stores and analyzes grades Can now store grades in an array static data members Also called class variables Variables for which each object of a class does not have a separate copy One copy is shared among all objects of the class Can be accessed even when no objects of the class exist Use the class name followed by the …


View Full Document

UVa-Wise COSC 181 - Foundations of Computer Programming

Download Foundations of Computer Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Foundations of Computer Programming and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?