arrayfunction

arrayfunction - Programming Passing Arrays to Functions ....

This preview shows pages 1–8. Sign up to view the full content.

Passing Arrays to Functions Programming

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
g. Fundamentals I: Passing Arrays to Function / Slide 2 Passing Arrays as Parameters Arrays are always passed by reference. The “[ ]” in the formal parameter specification indicates that the variable is an array. It is a good practice to pass the dimension of the array as another parameter. If the function must not change any element of the array then const should be used in the formal parameter specification of that array.
g. Fundamentals I: Passing Arrays to Function / Slide 3 Smallest Value Problem Find the smallest value in a list of integers Input A list of integers and a value indicating the number of integers Output Smallest value in the list Note List remains unchanged after finding the smallest value!

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
g. Fundamentals I: Passing Arrays to Function / Slide 4 Preliminary Design Realizations When looking for value with distinguishing characteristics, need a way of remembering best candidate found so far Best written as a function - likely to be used often Design Search array looking for smallest value Use a loop to consider each element in turn If current element is smallest so far, then update smallest value so far candidate When done examining all of the elements, the smallest value seen so far is the smallest value
g. Fundamentals I: Passing Arrays to Function / Slide 5 Necessary Information Information to be maintained Array with values to be inspected for smallest value Number of values in array Index of current element being considered Smallest value so far

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
g. Fundamentals I: Passing Arrays to Function / Slide 6 A More Detailed Design Solution: Function that takes two parameters: an integer array and the array size; returns smallest value Initialize smallest value to first element For each of the other elements in the array If it is smaller than the smallest value so far, update the value of the smallest value so far to current element Quit at end of array and return smallest value seen as value of the function
g. Fundamentals I: Passing Arrays to Function / Slide 7 int ListMinimum( const int Ar[], int asize) { int SmallestValueSoFar = Ar[0]; for ( int i = 1; i < asize; ++i) {

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/07/2011 for the course COMP 102 taught by Professor Daniel during the Spring '08 term at HKUST.

Page1 / 25

arrayfunction - Programming Passing Arrays to Functions ....

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online