CSSE 120 -- Intro. to Software Development

Homework 24

Using Eclipse, checkout the PointersHomework project from your individual SVN repository into your Eclipse C workspace. You will need to do more analysis than coding. As such, you will type out short answers to many questions in homework.txt and save your answers in your repository, and will draw two box-and-pointer diagrams to hand in next class. (6 pts for diagrams and 29 points total for code and typed answers)

  1. Open homework.txt and add your name to it.
  2. Pointers and Output
    1. Look at main.c's basicPointers() function, but don't run it yet.
    2. (3 pts) On paper, draw a box-and-pointer diagram, like we did in the videos on an early slide, for the code in the basicPointers() function. Make sure you diagram everything in the function. YOU WILL HAND THIS PAPER IN NEXT CLASS.
    3. (3 pts) Predict the output of basicPointers() BEFORE RUNNING IT and write it in homework.txt.
    4. We will give you full credit for any reasonable guess, so don't go back and change your answer after you run it!
    5. (2 pts) Now uncomment the call to basicPointers() and run it to see the actual output. Were you right? If not, explain what you learned from this.
    6. If you think your original box-and-pointer diagram was incorrect, please correct it.
    7. Look at main.c's everybodyUp() function, but don't run it yet.
    8. (3 pts) On paper, draw a box-and-pointer diagram, like we did in class on an early slide, for the call (in everybodyUpTest) to the everybodyUp() function and the code in it.
    9. (3 pts) Predict the output of everybodyUpTest() BEFORE RUNNING IT.
    10. We will give you full credit for any reasonable guess, so don't go back and change your answer after you run it!
    11. (2 pts) Now uncomment the call to everybodyUpTest() and run it to see the actual output. Were you right? If not, explain what you learned from this.
    12. If you think your original box-and-pointer diagram was incorrect, please correct it.
  3. Writing Functions
    1. (6 pts) In main.c, write and test a function called doubleMe that doubles the integer value passed to it. The doubleMe function had been called using doubleMe(num); you'll need to change it to pass the address instead to make it work correctly with your function.
    2. (6 pts) In main.c, write and test a swap() function that swaps the values of two floating point numbers passed to it: after x=10.0, y=20.3; and swap(...); then we should have x==20.3 and y==10.0. You will need a temporary variable in your function. You will also have to change how swap is called.
  4. Scanf
    1. (2 pts) Now that you have worked with pointers, you can understand scanf better. Recall that we need to pass to scanf the addresses of the variables we are getting from the user. Why do we need to do this? (Hint: what would happen if scanf didn't use pointers?)
    2. (2 pts) Try omitting the & when you call scanf. What happens?
  5. Pointer Pitfalls
    1. (3 pts) Explain what's wrong with this code. (You may run it to check after you think about it.)

      float* ptr = 0;
      printf("%4.2f\n", *ptr);
      
  6. Commit your modified versions of main.c and homework.txt to the repository. Bring the paper with your two box-and-pointer diagrams to your next class.

Preparation for Next Time

Watch the set of videos assigned for next time and complete the take-home quiz. The main schedule page contains links to the videos, slides, and quiz. Paper copies of the quiz are due at the start of next class.