Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

47
Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Transcript of Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Page 1: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Visual C++ Programming: Concepts and Projects

Chapter 11B: Pointers (Tutorial)

Page 2: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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

2Programming with Visual C++

Page 3: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Problem Description

3Programming with Visual C++

Page 4: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Problem Description (continued)

4Programming with Visual C++

Page 5: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Problem Description (continued)

5Programming with Visual C++

Page 6: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design

• Interface sketch• Control table• Instance variables

– Data table– Drawing objects

6Programming with Visual C++

Page 7: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

7Programming with Visual C++

Page 8: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

8Programming with Visual C++

Page 9: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

9Programming with Visual C++

Page 10: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Variables for DrawLines()• Each line drawn from the pointer array to the

data array text boxes needs starting and ending coordinates

10Programming with Visual C++

Page 11: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

11Programming with Visual C++

Page 12: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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

12Programming with Visual C++

Page 13: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

13Programming with Visual C++

Page 14: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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

14Programming with Visual C++

Page 15: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

15Programming with Visual C++

Page 16: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines()– Uses the following textbox properties

• Location.X• Location.Y• Width• Height

16Programming with Visual C++

Page 17: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

17Programming with Visual C++

Page 18: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

18Programming with Visual C++

Page 19: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines() (continued)– Draws lines connecting pointer array text boxes to

data array text boxes– Lines begin at the midpoint of the right side of

each pointer text box (ptrX, ptrY)

19Programming with Visual C++

Page 20: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

20Programming with Visual C++

Page 21: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

21Programming with Visual C++

Page 22: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines()(continued)– In this example, the pointer text boxes are

separated by 32 pixels vertically

22Programming with Visual C++

Page 23: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

23Programming with Visual C++

Page 24: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

24Programming with Visual C++

Page 25: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines() (continued)– Lines end at the midpoint of the left side of the

appropriate data text box (arrX, arrY)– Variable startX stores the y coordinate of the first

data text box

25Programming with Visual C++

Page 26: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

26Programming with Visual C++

Page 27: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

27Programming with Visual C++

Page 28: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines() (continued)– 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)

28Programming with Visual C++

Page 29: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

29Programming with Visual C++

Page 30: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

• Algorithm for DrawLines() (continued)– 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]) )

30Programming with Visual C++

Page 31: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Design (continued)

31Programming with Visual C++

Page 32: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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

32Programming with Visual C++

Page 33: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

33Programming with Visual C++

Page 34: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

• The btnData_Click() event handler– Assigns random numbers to each array element– Displays the data values in arr to the data text boxes– Displays the addresses of each element of the data

array • To the data text box labels• To the pointer array elements

– Displays the pointer array values– Draws the lines connecting pointer and data text

boxes

34Programming with Visual C++

Page 35: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

35Programming with Visual C++

Page 36: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

36Programming with Visual C++

Page 37: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

37Programming with Visual C++

Page 38: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

38Programming with Visual C++

Page 39: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

39Programming with Visual C++

Page 40: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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)

40Programming with Visual C++

Page 41: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

41Programming with Visual C++

Page 42: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

• The DisplayPointer() method– Displays pointer values on the interface

42Programming with Visual C++

Page 43: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

43Programming with Visual C++

Page 44: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

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 pointer array

44Programming with Visual C++

Page 45: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Development (continued)

45Programming with Visual C++

Page 46: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

Testing

• Run your program several times – btnSort should be initially disabled– When btnData is clicked, random numbers are

generated and 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

46Programming with Visual C++

Page 47: Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

On Your Own

• Descending order– Rewrite your bubble sort method

• Removing the swap() method– Place the instructions back in their proper place in

the bubble sort

47Programming with Visual C++