DOC PREVIEW
CSUN COMP 106 - Passing Whole Arrays to Functions

This preview shows page 1 out of 4 pages.

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

Unformatted text preview:

Passing whole arrays to functions April 27, 20061Passing Whole Arrays to Passing Whole Arrays to FunctionsFunctionsLarry CarettoComputer Science 106Computing in Engineering and ScienceApril 27, 20062Outline• Review introduction to arrays• Review writing code with arrays and for loops• Review data processing with arrays• Arrays and functions– Passing array elements to functions– Passing whole arrays to functions– Writing functions that have whole arrays as arguments3Representing DataDataRun14.113.212.511.814.412.3543210C++Mathx[5]x[4]x[3]x[2]x[1]x[0]x5x4x3x2x1x0• C++ array, x[k] used to represent data for which xkis used in mathe-maticalnotation4Using Arrays• Declare arrays in typical way, but add maximum elements, e.g. int v[100];• Refer to arrays as to any other variable using subscript v[3] or v[k]– Must assign value to k before using it as variable subscript– Major tool in arrays is using variable subscript that is for loop indexconst int N = 200; double a[N];for ( int j = 0; j < N; j++) a[j] = 0;5Maximum Array Subscript• Array subscripts start at zero• A declaration double y[N] declares a y array with N elements numbered from y[0] to y[N-1]• For loop to handle all elements isfor ( int k = 0; k < N; k++ )• C++ does not check to see if an array subscript is in bounds -- an incorrect subscript could affect some other memory locationN must be a const6General Array Processing• To process each element in an array with N elements, starting with the initial element, use a for loop with index k– k starts at zero– The continuation condition, k < N, will process elements 0, 1, 2, … , N-1– Increment k by 1• for ( int k = 0; k < N; k++)• Will process elements 0 to N – 1 of array regardless of array sizePassing whole arrays to functions April 27, 200627Passing Arrays to Functions• We can pass an array element to a function as we pass any variable• y = pow( x[k], 3);• Here the pow function returns the cube of element k of the x array• This is no different from passing a single variable to a function• We can also pass whole arrays, like x, to functions: getAverage( x, first, last)8getAverage• Computes the average of elements of the x array from x[first] to x[last] (inclusive)• Header: double getAverage ( double x[], int first, int last )• Prototypes: – double getAverage ( double x[], int first, int last );– double getAverage ( double [], int, int );• Note use of [] to specify an array as a function argument9getAveragedouble getAverage ( doublex[], int first, int last ){double sum = 0;for ( int i = first; i <= last; i++ ) sum += x[i];10Use of getAverage• double x[22], power[50], density[30];• // code to get input data on x and power• double mean = getAverage( x, 0, 10 );• double average = getAverage( power, 12, 24 );• How would you compute the average of all elements of the density array?getAverage( density, 0, 29);11Standard Deviation• Measure of spread around mean()()11112101022102102−⎟⎠⎞⎜⎝⎛−⎟⎠⎞⎜⎝⎛=−−⎟⎠⎞⎜⎝⎛=−−=∑∑∑∑−=−=−=−=NxNxNxNxNxxsNiiNiiNiiNii• First term is definition; others are computational forms• How would we write a function to compute s for all the elements in an N-element array?12Write Function getStdDev11210102−⎟⎟⎠⎞⎜⎜⎝⎛−⎟⎟⎠⎞⎜⎜⎝⎛=∑∑−=−=NxNxsNiiNii• What is function header?double getStdDev( double x[], int N)• What is prototype?double getStdDev( double x[], int N);• What call gives s for double power[100]?s = getStdDev( power, 100);• Want to find s for array, x, with size NPassing whole arrays to functions April 27, 2006313double getStdDev(double x[],int N){double sum = 0, sum2 = 0;for ( int k = 0; k < N; k++ ){sum += x[k];sum2 += x[k] * x[k];}return sqrt(( sum2 – sum * sum / N )/ ( N – 1 ) );}getStdDev14Arrays Passed by Referencevoid setArray( double x[], int N,double value ){for ( int k = 0, k < N; k++ )x[k] = value;}• A call, setArray( c, M ) would zero the elements 0 to M – 1 of the c array• For arrays, pass by reference occurs by default without the need for an &15Array Function Exercise• Write a function that finds the minimum value of a type double array• What information do you have to pass to the function?– The array name (double) and the number of elements in the array (int)• How would you return the minimum to the calling function– In the name of the function16Array Function Exercise II• Write a function that finds the minimum value of a type double array (continued)• Write the function headerdouble getMin( double x[], int N )• Write a statement that finds the minimum value of power: double power[250]double y = getMin( power, 250 )• What is getMin( power, 100 )?– The minimum value of elements 0 to 99 of an array named power17Array Function Exercise III• Write a function that finds the minimum value of a type double array (continued)• Write the prototype for getMin• Two possible prototypesdouble getMin( double x[], int N );double getMin( double [], int );• Write the complete code for the function18Array Function Exercise IV• The code for a function that finds the minimum value of a type double arraydouble getMin( double x[], int N ){double min = x[0];for ( int k = 1; k < N; k++ )if ( x[k] < min ) min = x[k];return min;}Passing whole arrays to functions April 27, 2006419Data Input Function for Array• If we know meaning of array parameters, we do not to know code• bool getInputOK ( double x[], int& n, intmax) is used to read data into an array x and return an value of true if there are no errors (false if there are errors)• Function returns the number of elements in the array, n• Error if attempt to read n > max20Data Input Function for Array II• Prototype for functionbool getInputOK ( double x[],int&, m int max );• Use of function in codeconst int MAX = 1000;double y[MAX]if ( !getInputOK( y, n, MAX ) ) {cout << “Program halt: “<< “Data input error”;return EXIT_FAILURE;}21Array Input Function Codebool getInputOK( double x[]int& n, int max ){ // open file; check statusifstream inFile("inputFile.dat" ); if ( !inFile.good() ) {cout << "Could not "<< "open input " << "data file.\n"; return false;} 22Array Input Function Code II// Get input datan = 0;do { double xin;inFile >> xin;if ( inFile.fail() ) break; x[n] = xin; n++;} while ( n < max );// can exit if last data read// or if >= max data


View Full Document

CSUN COMP 106 - Passing Whole Arrays to Functions

Download Passing Whole Arrays to Functions
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 Passing Whole Arrays to Functions 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 Passing Whole Arrays to Functions 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?