Lecture 16 Log into Windows/ACENET. Use a web browser to download today's exercise program. Go to course webpage. Right-click on RandomNumberProgram.zip link. Save target/link to Documents -> Visual Studio 2010 -> Projects folder. Browse to RandomNumberProgram.zip, right-click on it and select Extract All... and extract project folder Double-click into the RandomNumberProgram folders to the solution file, then double-click it to start up VS. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 1 Outline Reminder: Programming Assignment 4 due today by 4:30pm Array parameters and arguments Finish last class' in-class exercise, start MSVS again to open TestStatsProgram Random numbers Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 2 In-class Exercise, Last Class Modify the Main method of the TestStats program as follows: Replace the score variables with an array of 11 elements. Replace the calls to GetTestScore with a for-loop that calls GetTestScore for each array element. Modify the computation of total to use the array elements. (We will fix this later.) Replace the calls to DisplayResultRow with a forloop that calls DisplayResultRow for each array element. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 3 Array Parameters Just like other data items, arrays can be passed to a method as a parameter. Unlike other data items, arrays are always passed by reference, and there is no special syntax for doing so. The syntax for an array parameter is: ... <methodname> (<type> [ ] <arrayname>, ...) For example, a method ComputeAverage that receives an array and returns the average of the values in the array would have header: static double ComputeAverage (double [ ] array) Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 4 Array Arguments To call a method that has an array parameter, the corresponding argument simply is the array name. (That is, there are no square brackets or size.) In the example, to call ComputeAverage, we would write: average = ComputeAverage (score); Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 5 In-class Exercise, Last Class Modify the TestStats program as follows: Add a method ComputeAverage that receives an array of real numbers and returns the average of the numbers in the array. The average should be computed by using a for-loop to sum the elements of the array together, then dividing by the number of elements in the array. At the beginning of the Main method, write code to get the number of test scores from the user; change the array creation to use this number for its size. Replace the average-computing code in the Main method with a call to ComputeAverage. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 6 In-class Exercise Today's exercise is to write several methods to compute various statistical quantities. The program will do the following: Ask the user for the number of values needed Create an array of integers of that size Fill the array with random integers in range [0..99] Compute the minimum value in the array, the maximum value in the array, the average of the values in the array, and the standard deviation of the values in the array Displays these statistical quantities Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 7 RandomFillArray A method RandomFillArray that fills an array with random integers is already written. This method uses a variable num that is of type Random. Like arrays, Random objects need to be created using the new operator. A Random object generates (pseudo)random integers. These integers are returned by the Next method that takes a lower and an upper bound as arguments. The range of the returned value is [lower..upper), i.e., the upper end is open. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 8 RandomFillArray This program declares the Random variable num as a static class variable by putting its declaration outside any method (but inside the class) and prefixing the declaration with the static modifier. This is done so that there is only one Random number generator in the program that is accessible to all methods in the class. Also, the random number generator will retain state between method calls that use it, rather than starting over each time it is created. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 9 DisplayArray A method DisplayArray is already written. This method displays an array of integers by displaying 10 numbers on each line. Note this is done by displaying each array element using Console.Write( ) and a format specifier with a field width of 4, and using the modulus operator (%) to determine when a new line needs to be started. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 10 ComputeMinimum A method ComputeMinimum is already written. It has an array of integer parameter and returns the minimum value in the array. This is the way it is computed: 1. Declare an integer variable minimum and initialize it to the first element of the array. 2. For each element after the first one 2.1 If the element is less than the minimum 2.1.1 The element becomes the new minimum 3. After the loop is finished, minimum is the smallest value seen in the array, so return it. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 11 In-class Exercise Copy the ComputeAverage method from TestStats.cs and paste it into this program. Modify it to use an integer array parameter. Note that the sum should remain a double variable to ensure real division in computing the average. In the Main method, do the following: Declare and create an array of integers of the size input by the user. Call the RandomFillArray method to fill the array with random numbers and DisplayArray to display it. Compute the mininum value and the average value by calling ComputeMinimum and ComputeAverage, respectively, and display these results. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 12 In-class Exercise At this point, make sure your program compiles and runs correctly. Use small values for the user input so that you can hand compute whether the minimum and average are computed correctly. The rest of the exercise is to write methods ComputeMaximum, and ComputeStdDev, which are described on the next slides. After completing each method, you should make sure your program compiles and runs correctly. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 13 ComputeMaximum ComputeMaximum basically is the same as ComputeMinimum except that we are looking for elements greater than the current maximum: 1. Declare an integer variable maximum and initialize it to the first element of the array. 2. For each element after the first one 2.1 If the element is greater than the minimum 2.1.1 The element becomes the new maximum 3. After the loop is finished, maximum is the largest value seen in the array, so return it. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 14 ComputeStdDev ComputeStdDev has an array of integers parameter and also receives the average of the values in the array. It computes and returns the standard deviation of the values in the array. The formula for standard deviation is: S= n ∑ x i−avg2 i =1 n Note that the summation is 1 to n, but that the array indexes will be 0 to n-1. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 15 ComputeStdDev In the Main method, call this method and display the result. Note that ComputeStdDev must be called after the average has been computed. Wednesday, February 16 CS 205 Programming for the Sciences - Lecture 16 16