CSSE 120 -- Intro. to Software Development

Homework 24

  1. Take a look at the Python vs C comparison document.  You may find this helpful as you try to do things in C that you already know how to do in Python.  Feel free to suggest things that we might add to this document.
  2. Complete the assigned reading for the next session: listed on the schedule page.
  3. (18 pts) Complete the Angel quiz over the reading assignment. You'll find the quiz on the course Angel page, under Lessons → Homework → Homework 24 → Arrays and Pointers
  4. Finish the in-class quiz relating to the PointersInClass project if you didn't do so in class. Bring your completed quiz to your next class session.

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 pointers 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 class 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 to the everybodyUp() function and the code in it.
    9. (3 pts) Predict the output of everybodyUp() 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.