Chapter 11b Pointers

46
CHAPTER 11B POINTERS Tutorial: Sorting with a Pointer Array

description

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 - PowerPoint PPT Presentation

Transcript of Chapter 11b Pointers

Page 1: Chapter 11b Pointers

CHAPTER 11B POINTERS

Tutorial: Sorting with a Pointer Array

Page 2: Chapter 11b Pointers

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
Page 4: Chapter 11b Pointers
Page 5: Chapter 11b Pointers
Page 6: Chapter 11b Pointers

Design

Interface sketch Control table Instance variables

Data table Drawing objects

Page 7: Chapter 11b Pointers
Page 8: Chapter 11b Pointers
Page 9: Chapter 11b Pointers
Page 10: Chapter 11b Pointers

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
Page 12: Chapter 11b Pointers

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
Page 14: Chapter 11b Pointers

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
Page 16: Chapter 11b Pointers

Design (continued)

Algorithm for DrawLines() Uses the following textbox properties

Location.X Location.Y Width Height

Page 17: Chapter 11b Pointers
Page 18: Chapter 11b Pointers
Page 19: Chapter 11b Pointers

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
Page 21: Chapter 11b Pointers
Page 22: Chapter 11b Pointers

Design (continued)

Algorithm for DrawLines() In this example, the pointer textboxes are

separated by 32 pixels vertically

Page 23: Chapter 11b Pointers
Page 24: Chapter 11b Pointers
Page 25: Chapter 11b Pointers

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
Page 27: Chapter 11b Pointers
Page 28: Chapter 11b Pointers

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
Page 30: Chapter 11b Pointers

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
Page 32: Chapter 11b Pointers

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
Page 34: Chapter 11b Pointers

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
Page 36: Chapter 11b Pointers
Page 37: Chapter 11b Pointers
Page 38: Chapter 11b Pointers
Page 39: Chapter 11b Pointers
Page 40: Chapter 11b Pointers

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
Page 42: Chapter 11b Pointers

Development (continued)

The DisplayPointer() method Displays pointer values on the interface

Page 43: Chapter 11b Pointers
Page 44: Chapter 11b Pointers

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
Page 46: Chapter 11b Pointers

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