1 / 46

Chapter 11b Pointers

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

kayla
Download Presentation

Chapter 11b Pointers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11b Pointers Tutorial: Sorting with a Pointer Array

  2. 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

  3. Design • Interface sketch • Control table • Instance variables • Data table • Drawing objects

  4. Design (continued) • Variables for DrawLines() • Each line drawn from the pointer array to the data array textboxes need starting and ending coordinates

  5. 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

  6. 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

  7. Design (continued) • Algorithm for DrawLines() • Uses the following textbox properties • Location.X • Location.Y • Width • Height

  8. 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)

  9. Design (continued) • Algorithm for DrawLines() • In this example, the pointer textboxes are separated by 32 pixels vertically

  10. 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

  11. 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)

  12. 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]) )

  13. 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

  14. 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

  15. 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)

  16. Development (continued) • The DisplayPointer() method • Displays pointer values on the interface

  17. 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

  18. 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

More Related