CSUN COMP 106 - Designing Software with Arrays and Functions

Unformatted text preview:

Array function exercises May 2, 20061Designing Software with Arrays Designing Software with Arrays and Functionsand FunctionsLarry CarettoComputer Science 106Computing in Engineering and ScienceMay 2, 20062Outline• Review passing arrays to functions– Use array notation x[] in header and prototype for arrays– By default, arrays are always passed by reference• No ampersand (&) required• Constructing functions with arrays• Restructuring arrays3Passing 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)4getAverage• When we pass a whole array to a function, – the function can use any element of the array– the array is always passed by reference• 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 argument5getAveragedouble getAverage ( double x[],int first, int last ){double sum = 0;for ( int i = first; i <= last; i++ )sum += x[i];return sum / ( last – first + 1 );}6Use 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);Array function exercises May 2, 200627Standard Deviation• Measure of spread around mean()()11112101022102102−⎟⎠⎞⎜⎝⎛−⎟⎠⎞⎜⎝⎛=−−⎟⎠⎞⎜⎝⎛=−−=∑∑∑∑−=−=−=−=NxNxNxNxNxxsNiiNiiNiiNii• First term is definition; others are computational forms• How would we write a function to com-pute s for all the elements in an N-element array?8double 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 ) );}getStdDev9Arrays 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 first M elements of the c array• For arrays, pass by reference occurs by default without the need for an &10Array Function Exercise• Write a function to compute a power array from current and voltage arrays • What information do you pass to the function?– The array names for current and voltage and the number of elements (same for both arrays)• How would you return the power array to the calling function– As a function parameter (arrays are always passed by reference)11Array Function Exercise II• The function uses the arrays power[k], amps[k], and volts[k], for power, current, and voltage, respectively• Write the function headervoid getPower( double amps[], double volts[], int N, double power[] )• Write two prototypes for this functionvoid getPower( double amps[], double volts[], int N, double power[]);12Array Function Exercise IIIvoid getPower( double [],double [], int , double [] );• Write a statement that calls getPower from a function with the declaration: double curr[250], volt[250], pow[250];getPower( curr, volt, 250, pow )Array function exercises May 2, 2006313Array Function Exercise IV• For the same arrays, double curr[250], volt[250], pow[250]; What would the result of the following statement be? getPower( curr, volt, 100, pow )• It would compute 100 elements of the power array, from power[0] to power[99]14Array Function Exercise V• For the same arrays, double curr[250], volt[250], pow[250]; What would the result of the following statement be? getPower( curr, volt, 300, pow )• It would use 50 storage locations at the ends of the curr and volt arrays to compute 50 extra values of “power” that would replace 50 storage locations following the power array15Array Function Exercise VI• Write the code for the getPower functionvoid getPower( double amps[],double volts[], int N,double power[] ){for ( int k = 1; k < N; k++ )power[k] = amps[k] *volts[k];}16Regression Function• Calculation function from exercise eight– pass arrays x[i] and y[i] and size n into function• n is number of elements in array not necessarily the maximum possible number of elements– return yHat array and computed variables of slope, intercept, sxy, and R_squared to calling function by reference• Shows typical array function calculations17Regression Calculations• Evaluate the following equations for slope, b, and intercept, a21012101010⎟⎟⎠⎞⎜⎜⎝⎛−⎟⎟⎠⎞⎜⎜⎝⎛⎟⎟⎠⎞⎜⎜⎝⎛−=∑∑∑∑∑−==−=−=−=NiiNiiNiiNiiNiiixxNyxyxNb⎥⎥⎦⎤⎢⎢⎣⎡⎟⎟⎠⎞⎜⎜⎝⎛−⎟⎟⎠⎞⎜⎜⎝⎛=∑∑−=−=10101NiiNiixbyNa18Regression Calculations II• After a and b are found we find1,0ˆ−=+=NibxayiiL2)ˆ(102−−∑−=NyyNiii2101021022101022|21)ˆ(11)2(1⎟⎟⎠⎞⎜⎜⎝⎛−−−=⎟⎟⎠⎞⎜⎜⎝⎛−−−=∑∑∑∑∑−=−=−=−=−=NiiNiiNiiiNiiNiixyyNyyyyNysNR=xys|Array function exercises May 2, 2006419Function Design• Write a function to do these calculations• Must pass xiand yiarrays and N to function• Function returns array and slope, b, intercept, a, sy|x, and R2– Non-array return values must use pass by reference– Arrays always use pass by reference as defaultiyˆ20Function Design II• Function code must compute all of the following sums from input data• Calculation of R2and sy|xrequires calculation of another sum that can only be done after a and b are found∑∑∑∑∑−=−=−=−=−=102102101010NiiNiiNiiNiiNiiiyxyxyx1,0ˆ−=+=NibxayiiL2)ˆ(102−−∑−=NyyNiii=xys|21Function Design III• Steps for regression function– Compute sums below in a single loop– Compute b and a– Compute and• This can be done in the same loop– Compute sy|xand R2∑∑∑∑∑−=−=−=−=−=102102101010NiiNiiNiiNiiNiiiyxyxyxiibxay +=ˆ∑−=−102)ˆ(Niiiyy22Regression Codevoid doCalculations( double x[],double y[], double y_hat[],int n, double& slope, double& intercept, double& syx,double& R_squared ) { double sumx = 0, // sum of xsumxx = 0, // sum of x^2sumxy = 0, // sum of x *


View Full Document

CSUN COMP 106 - Designing Software with Arrays and Functions

Download Designing Software with Arrays and 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 Designing Software with Arrays and 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 Designing Software with Arrays and 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?