DOC PREVIEW
UVa-Wise COSC 181 - Foundations of Computer Programming

This preview shows page 1-2-3-26-27-28 out of 28 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 28 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Slide 2Slide 3Slide 10Slide 11Slide 127.5 Passing Arrays to Functions7.5 Passing Arrays to Functions (Cont.)Slide 20Slide 21Slide 227.6 Case Study: Class GradeBook Using an Array to Store GradesSlide 6Slide 7Slide 8Slide 9Slide 17Slide 18Slide 19Slide 13Slide 14Swapping ElementsSlide 23Slide 24Slide 25Common Programming Error 7.11Software Engineering Observation 7.4Slide 281COSC 181 – Foundations of Computer ProgrammingClass 332Schedule Change – HW Due Dec. 3rd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 arrays37.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 arrays41 // Fig. 7.13: f ig07_13.cpp2 / / Stat ic arrays are in it ia l i z e d to zero.3 #inc lude <iostream>4 using std::cout;5 using std::endl;67 void staticArrayInit( void ); // function prototype8 void automaticArrayInit( void ); // function prototype910 int main()11 {12 cout << "F ir s t call to each funct ion: \n";13 staticArrayInit();14 automaticArrayInit();1516 cout << "\n\nSecond call to each funct ion: \n";17 staticArrayInit();18 automaticArrayInit();19 cout << endl;2021 return 0; // indicates successful termination22 } // end mainOutlinefig07_13.cpp (1 of 3)52324 // funct ion to demonstrate a stat ic loca l array25 void staticArrayInit( void )26 {27 // initializes elements to 0 first time function is called28 static int array1[ 3 ]; // static local array 2930 cout << "\nVa lues on enter ing stat icArray In i t : \ n ";3132 // output contents of array133 for ( int i = 0; i < 3; i++ )34 cout << "array1[" << i << "] = " << array1[ i ] << " ";3536 cout << "\nValues on exiting staticArrayInit:\n";3738 // modify and output contents of array139 for ( int j = 0; j < 3; j++ )40 cout << "array1[" << j << "] = " << ( array1[ j ] += 5 ) << " ";41 } // end function staticArrayInit4243 // function to demonstrate an automatic local array44 void automaticArrayInit( void )45 {46 // initializes elements each time function is called 47 int array2[ 3 ] = { 1, 2, 3 }; // automatic local array4849 cout << "\n\nValues on entering automaticArrayInit:\n";Outlinefig07_13.cpp (2 of 3)Create a static array using keyword staticCreate an automatic local array65051 // output contents of array252 for ( int i = 0; i < 3; i++ )53 cout << "array2[" << i << "] = " << array2[ i ] << " ";5455 cout << "\nValues on exiting automaticArrayInit:\n";5657 // modify and output contents of array258 for ( int j = 0; j < 3; j++ )59 cout << "array2[" << j << "] = " << ( array2[ j ] += 5 ) << " ";60 } // end function automaticArrayInitFirst call to each function:Values on entering staticArrayInit :array1[0] = 0 array1[1] = 0 array1[2] = 0Values on ex it ing staticArrayInit :array1[0] = 5 array1[1] = 5 array1[2] = 5Values on entering automaticArrayInit :array2[0] = 1 array2[1] = 2 array2[2] = 3Values on ex it ing automaticArrayInit:array2[0] = 6 array2[1] = 7 array2[2] = 8Second call to each function:Values on entering staticArrayInit :array1[0] = 5 array1[1] = 5 array1[2] = 5Values on ex it ing staticArrayInit :array1[0] = 10 array1[1] = 10 array1[2] = 10Values on entering automaticArrayInit :array2[0] = 1 array2[1] = 2 array2[2] = 3Values on ex it ing automaticArrayInit:array2[0] = 6 array2[1] = 7 array2[2] = 8Outlinefig07_13.cpp (3 of 3)Values reflect changes from the previous function call – the array was not reinitialized77.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 array87.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 data91 // Fig. 7.14: f ig07_14.cpp2 // Pass ing arrays and indiv idua l array elements to funct ions .3 #include <iostream>4 using std::cout;5 using std::endl;67 #include <iomanip>8 using std::setw;910 vo id modifyArray( int [], int ); // appears strange11 void modifyElement( int ); 1213 int main()14 {15 const int arraySize = 5; // size of array a16 int a[ arraySize ] = { 0, 1, 2, 3, 4 }; // initialize array a1718 cout << "Effects of passing entire array by reference:" 19 << "\n\nThe values of the original array are:\n";2021 // output original array elements22 for ( int i = 0; i < arraySize; i++ )23 cout << setw( 3 ) << a[ i ];2425 cout << endl;2627 // pass array a to modifyArray by reference28 modifyArray( a, arraySize ); 29 cout << "The values of the modified array are:\n";Outlinefig07_14.cpp (1 of 3)Declare 5-int array array with initializer list Function takes an array as argument Pass entire array to function modifyArray103031 / / output modif ied array elements32 for ( int j = 0; j < arraySize; j++ )33 cout << setw( 3 ) << a[ j ];3435 cout << "\n\n\nEffects of passing array element by value:"36 << "\n\na[3] before modifyElement: " << a[ 3 ] << endl;3738 modifyElement( a[ 3 ] ); // pass array element a[ 3 ] by value39 cout << "a[3] after modifyElement: " << a[ 3 ] <<


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