460 likes | 596 Views
Chapter 11b Pointers. Tutorial: Sorting with a Pointer Array. Tutorial: Sorting with a Pointer Array. Problem Description Create an array of integers and display its values and the address of each element Create an array of pointers to integers
E N D
Chapter 11b Pointers Tutorial: Sorting with a Pointer Array
Tutorial: Sorting with a Pointer Array • Problem Description • Create an array of integers and display its values and the address of each element • Create an array of pointers to integers • Assign the address of each element in the integer array to the corresponding elements of the pointer array • Sort the data by swapping pointers • Display the sorted data by using a for loop to access each element of the pointer array
Design • Interface sketch • Control table • Instance variables • Data table • Drawing objects
Design (continued) • Variables for DrawLines() • Each line drawn from the pointer array to the data array textboxes need starting and ending coordinates
Design (continued) • Event handlers • btnData_Click() • Generates random numbers in the data array • Captures data array element addresses and assigns to the pointer array • Displays data and pointers • Draws lines connecting them
Design (continued) • Event handlers • btnSort_Click() • Sort the data by swapping pointers • Draw lines from each pointer to the appropriate data item • Display the sorted data in a MessageBox
Design (continued) • Algorithm for DrawLines() • Uses the following textbox properties • Location.X • Location.Y • Width • Height
Design (continued) • Algorithm for DrawLines() • Draws lines connecting pointer array textboxes to data array textboxes • Lines begin at the midpoint of the right side of each pointer textbox (ptrX, ptrY)
Design (continued) • Algorithm for DrawLines() • In this example, the pointer textboxes are separated by 32 pixels vertically
Design (continued) • Algorithm for DrawLines() • Lines end at the midpoint of the left side of the appropriate data textbox (arrX, arrY) • Variable startX stores the y coordinate of the first data textbox
Design (continued) • Algorithm for DrawLines() • The location of arrY is calculated by • Determining how many elements away from the first element you must go • Multiplying the number of elements by 32 (the number of pixels separating each element)
Design (continued) • Algorithm for DrawLines() • To determine how many elements away from the first element you must go, subtract the pointer array value from the address of the first data element ( &(arr[0]) )
Development • The Interface • Based on Figure 11-32 • Coding • Instance variable declarations and Form1_Load() • The btnData_Click() event handler • The btnSort_Click() event handler • The drawLines() method
Development (continued) • The btnData_Click() event handler • Assigns random numbers to each array element • Displays the data values in arr to the data textboxes • Displays the addresses of each element of the data array • To the data textbox labels • To the pointer array elements • Displays the pointer array values • Draws the lines connecting pointer and data textboxes
Development (continued) • The btnSort_Click() event handler • Sorts the pointer array according to the values in the data array • Displays the pointers • Creates output string • Draws the connecting lines (once before and once after the MessageBox is displayed)
Development (continued) • The DisplayPointer() method • Displays pointer values on the interface
Development (continued) • The drawLines() method • Calculates ptrX, ptrY • Calculates the corresponding arrX and arrY • Draws lines between (ptrX, ptrY) on the pointer array and (arrX, arrY) on the data array for each element of the poniter array
Testing • Run your program several times • btnSort should be initially disabled • When btnData is clicked random numbers are generated, displayed, and the memory cell addresses of the data array are displayed • When btnSort is clicked the pointer array elements are swapped until they point to the data array elements in sorted order