Lab Exercise: Arrays This exercise involves manipulating data in a one dimensional decimal array. In particular, Moving items between positions in the array Sorting the array Displaying the array in a ListBox Calculating an average of the items in the array Searching the array Starter Application The starter application includes lstResult, a ListBox used to display the contents of the array txtItem, a TextBox used for data entry txtResult, a TextBox used to display the average Buttons to Add items to the array, search, delete, Sort, calculate the average, and exit. items, a decimal array of length 50 --- with 4 locations initialized itemsCount, an int containing the current count of items in the items array. The Starter application also includes the following methods: void displayArray(decimal[], int, ListBox) Clears and fills a ListBox from the contents of a decimal array. The decimal numbers are displayed in the ListBox as currency. There are three parameters: The decimal array An int specifying the number of data items in the array The ListBox btnAddItem_Click(object, EventArgs) Responds to the click event of the Add Item button. Converts the item in the txtItem TextBox to decimal and adds it to the items array if there is room. The item must be numeric. If it is not, a MessageBox is displayed and the item is ignored. Updates lstResult from the items array. Part 1: Simple Array Changes Form1() in the starter program is a special method known as a Constructor which is called when the form is first created. In the Form1() method, just above the call to displayArray, try the following: Change items[1] to 23m Copy items[2] to items[3] Use a for loop to add 1m to each of the first 4 items. Use an if statement to see if items[0] is equal to items[1] and popup a MessageBox if they are equal. Part2: Sorting the Array Respond to the click event of btnSort (The Sort button). Use the Array.Sort method to sort the items array. When you call Array.Sort --- use itemsCount to determine how much of the array to sort. Call the displayArray method to display the sorted array. Part 3: Calculate the average Respond to the click event of btnAverage (the Average button). Inside the btnAverageClick method, do the following: o o o o o Create a decimal variable called total and set it to 0. Use a for loop to add each of the items in the items array to total. The for loop should use itemsCount to determine when to stop since the average should only be calculated using the members of the array which contain valid data. After the loop, create another decimal variable called average. Divide total by itemsCount and put the result in avg. Convert avg to a String and put the result in txtResult.Text. Try pressing the Average button with no data in the items array. If the program fails, you have probably tried to divide by 0 when calculating the average. Add a test to the program to handle this situation. Part 4: Binary Search Respond to the Search button using the Array.BinarySearch method. The Array.BinarySearch method only works if an array is sorted. So, when you test this part of the program, be sure to sort the array first. In the method responding to the click event of the button, o o o o o Create a decimal variable called searchItem. Create an int variable called searchResult. Convert txtItem.Text to a decimal and store the result in searchItem. Be sure to test for nonnumeric data using try/catch. Call Array.BinarySearch to search for searchItem and place the location of the result in searchResult. If there is a match, highlight the line in the listBox containing the matching item. Use the SetSelected method (described below). Part 5: Delete o Respond to the click event of the Delete button. o Create a decimal variable called locationToDelete. Set it to the location of the item selected in the ListBox. Use the SelectedIndex method of the ListBox. o Since an array cannot actually delete items, you need to copy each item in the array beyond the deleted item to the item before it. For example, if you want to delete item 3, you need to copy item 4 to item 3, item 5 to item 4, etc, until you come to the end of the filled part of the array. Use a loop to do this. o Subtract 1 from itemsCount (as long as it is greater than 0) o Call the displayArray method to display the array. If you have time . . . Try adding a button to find the lowest item in the items array and highlight that item. You may need the following information decimal Convert.ToDecimal(String) converts the string you provide (for example the contents Text in a TextBox to a decimal number and returns the decimal number. For example, income = Convert.ToDecimal(txtIncome.Text); Converts the text in the txtIncome TextBox to a decimal and stores the converted number in income. String decimalvariable.toString(“c”); converts the data in decimalvariable to a String formatted as dollars and cents. boxname.Text The text contained within boxname, a TextBox void listboxname.Items.Add(item); Appends the item you provide (a String or variable) to listboxname, a ListBox. void listboxname.Items.Clear(); Clears the ListBox. try { . . . } catch (FormatException) { . . . } executes the statements in the catch block if any of the statements in the try block result in a format error (for example --- they have non-numeric data) MessageBox.Show(“text”); Displays a message box. item1 + item2 If item1 and item2 are numeric, performs addition If either item1 or item2 is a String, joins the two strings For example: Assume 3m is currently stored in a decimal variable called test. MessageBox.Show(“Hello” + test); Displays Hello3. void Array.Sort(decimal[],int,int) Sorts the array specified in the first parameter. The second parameter is the starting index. The third parameter is the count of items to sort. For example: Array.Sort(streets,0,5); Sorts the first 5 items in an array named streets void listBoxName.setSelected(int,true) Highlights (selects) an item in a list box. The item position is specified by the first parameter. The second parameter must be set to true. For example, lstTest.setSelected(3,true); Highlights item 3 in the lstTest int Array.BinarySearch(decimal[],0,int,decimal) Searches a decimal array for a decimal number. If there is a match, the returns an int with the location. Otherwise returns a negative number. There are 4 parameters. o The first parameter is the array o The second parameter is the starting position to search. (0 for this exercise) o The third parameter is the count of items to search. o The fourth parameter is the item to search for. For example: int p; p = Array.BinarySearch(streets,0,12,63m); Searches the first 12 items in a decimal array named streets for 63. If 63 is found, p will be set to its position. int listBoxName.SelectedIndex Returns the index of the current item selected in a ListBox. If nothing is selected, returns a negative number.