1 / 47

Visual C++ Programming: Concepts and Projects

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

yaholo
Download Presentation

Visual C++ Programming: Concepts and Projects

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. Visual C++ Programming: Concepts and Projects Chapter 11B: Pointers (Tutorial)

  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 Programming with Visual C++

  3. Problem Description Programming with Visual C++

  4. Problem Description (continued) Programming with Visual C++

  5. Problem Description (continued) Programming with Visual C++

  6. Design • Interface sketch • Control table • Instance variables • Data table • Drawing objects Programming with Visual C++

  7. Design (continued) Programming with Visual C++

  8. Design (continued) Programming with Visual C++

  9. Design (continued) Programming with Visual C++

  10. Design (continued) • Variables for DrawLines() • Each line drawn from the pointer array to the data array text boxes needs starting and ending coordinates Programming with Visual C++

  11. Design (continued) Programming with Visual C++

  12. 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 Programming with Visual C++

  13. Design (continued) Programming with Visual C++

  14. 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 Programming with Visual C++

  15. Design (continued) Programming with Visual C++

  16. Design (continued) • Algorithm for DrawLines() • Uses the following textbox properties • Location.X • Location.Y • Width • Height Programming with Visual C++

  17. Design (continued) Programming with Visual C++

  18. Design (continued) Programming with Visual C++

  19. 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) Programming with Visual C++

  20. Design (continued) Programming with Visual C++

  21. Design (continued) Programming with Visual C++

  22. Design (continued) • Algorithm for DrawLines()(continued) • In this example, the pointer text boxes are separated by 32 pixels vertically Programming with Visual C++

  23. Design (continued) Programming with Visual C++

  24. Design (continued) Programming with Visual C++

  25. 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 Programming with Visual C++

  26. Design (continued) Programming with Visual C++

  27. Design (continued) Programming with Visual C++

  28. 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) Programming with Visual C++

  29. Design (continued) Programming with Visual C++

  30. 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]) ) Programming with Visual C++

  31. Design (continued) Programming with Visual C++

  32. 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 Programming with Visual C++

  33. Development (continued) Programming with Visual C++

  34. 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 Programming with Visual C++

  35. Development (continued) Programming with Visual C++

  36. Development (continued) Programming with Visual C++

  37. Development (continued) Programming with Visual C++

  38. Development (continued) Programming with Visual C++

  39. Development (continued) Programming with Visual C++

  40. 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) Programming with Visual C++

  41. Development (continued) Programming with Visual C++

  42. Development (continued) • The DisplayPointer() method • Displays pointer values on the interface Programming with Visual C++

  43. Development (continued) Programming with Visual C++

  44. 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 Programming with Visual C++

  45. Development (continued) Programming with Visual C++

  46. 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 Programming with Visual C++

  47. 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 Programming with Visual C++

More Related