< Back

Lecture 12- Pointers and Hand Tracing


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab12 
$ cd ~/cs170/labs/lab12

Hand Tracing

We've done a lot of informal hand tracing over the past semester. Every one of your tests has always contained some number of tracing questions for you to solve. However, most of those were relatively straightforward. One of the biggest benefits of hand tracing, however, is the ability to help you find errors and bugs in your code. To accomplish that, however, we need to take a more formalized approach to what is going on.


Lab Activity 1
Practicing Hand Tracing

The way to get better at hand tracing is via practice. So we are going to spent ~20-30 minutes letting you guys get practice with this new, formalized hand tracing we will be doing for the rest of the semester (and probably the rest of your time coding). There are 3 programs here, ranging from short and simple, to long and relatively complex. Practice with these, and I'll go over at least one of them afterwards.

Details

  1. handtracing1.cc
  2. handtracing2.cc
  3. handtracing3.cc

Pointers

If you ask a C++ programmer what the scariest part of the C++ language is, I'd be willing to be ~70% of them will tell you it's pointers. However, pointers do not need to be scary. We've been working with pointers for a while now, even though they weren't really called that. They've been hidden from us, being handled behind the scenes. Pointers give use some tremendous power in our programs, which results in much stronger capabilities in our programs.


Lab Activity 2
Practicing Hand Tracing

The way to get better at hand tracing is via practice. So we are going to spent ~20-30 minutes letting you guys get practice with this new, formalized hand tracing we will be doing for the rest of the semester (and probably the rest of your time coding). There are 3 programs here, ranging from short and simple, to long and relatively complex. Practice with these, and I'll go over at least one of them afterwards.

Details

  1. pointerTracing1.cc
  2. pointerTracing2.cc
  3. pointerTracing3.cc

Why We Need Pointers

Let's re-explore our QT stuff to see why we needed pointers in our QT projects, and how we can leverage what we saw on Monday towards our projects due on Monday.


Lab Activity 3

By default, doing form based interactions with QT is very straight forward. However, it's very limiting to handling button presses, etc. QT has capabilities, behind the scenes, to give you much more control. These come from being able to modify the mouse click event handlers, and the keyboard key press handlers. To start off, we will make an incomplete Sudoku program which will allow you to interact with a grid of labels!

Details

Using QTCreator, create a C++ program that displays a 9 x 9 grid of labels, all evenly spaced and sized. Your grid of labels should fill as much of the window as possible. They should appear blank by default. Whenever the user clicks on a label, it should be "selected," and a border should appear around it. Once a label is selected, the user should be able to press one of the keyboard keys and display a number in the selected label. If the user presses a key which is not a number, nothing should appear in the label.

In addition, your program should identify any locations which produce conflicts with one another on a row or a column level. You can ignore the "block" level conflicts.

Example

 

Challenge

Your game is only partially functional right now. It doesn't even check to see if you've got a solved puzzle! Add checks to your program that highlights block level conflicts.

If you are feeling ambitious, you use file redirection in order to load a text file via standard input: