Download - Chapter 11b Pointers
![Page 1: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/1.jpg)
CHAPTER 11B POINTERS
Tutorial: Sorting with a Pointer Array
![Page 2: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/3.jpg)
![Page 4: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/4.jpg)
![Page 5: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/5.jpg)
![Page 6: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/6.jpg)
Design
Interface sketch Control table Instance variables
Data table Drawing objects
![Page 7: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/7.jpg)
![Page 8: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/8.jpg)
![Page 9: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/9.jpg)
![Page 10: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/10.jpg)
Design (continued)
Variables for DrawLines() Each line drawn from the pointer array
to the data array textboxes need starting and ending coordinates
![Page 11: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/11.jpg)
![Page 12: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/12.jpg)
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
![Page 13: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/13.jpg)
![Page 14: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/15.jpg)
![Page 16: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/16.jpg)
Design (continued)
Algorithm for DrawLines() Uses the following textbox properties
Location.X Location.Y Width Height
![Page 17: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/17.jpg)
![Page 18: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/18.jpg)
![Page 19: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/19.jpg)
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)
![Page 20: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/20.jpg)
![Page 21: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/21.jpg)
![Page 22: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/22.jpg)
Design (continued)
Algorithm for DrawLines() In this example, the pointer textboxes are
separated by 32 pixels vertically
![Page 23: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/23.jpg)
![Page 24: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/24.jpg)
![Page 25: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/25.jpg)
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
![Page 26: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/26.jpg)
![Page 27: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/27.jpg)
![Page 28: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/28.jpg)
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)
![Page 29: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/29.jpg)
![Page 30: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/30.jpg)
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]) )
![Page 31: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/31.jpg)
![Page 32: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/32.jpg)
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
![Page 33: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/33.jpg)
![Page 34: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/34.jpg)
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
![Page 35: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/35.jpg)
![Page 36: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/36.jpg)
![Page 37: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/37.jpg)
![Page 38: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/38.jpg)
![Page 39: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/39.jpg)
![Page 40: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/40.jpg)
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)
![Page 41: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/41.jpg)
![Page 42: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/42.jpg)
Development (continued)
The DisplayPointer() method Displays pointer values on the interface
![Page 43: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/43.jpg)
![Page 44: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/44.jpg)
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
![Page 45: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/45.jpg)
![Page 46: Chapter 11b Pointers](https://reader036.vdocuments.site/reader036/viewer/2022062423/5681465b550346895db37b3f/html5/thumbnails/46.jpg)
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