CSSE 120 -- Intro. to Software Development

Homework 25

  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 25 → Quiz 25: Arrays and Pointers
  4. Finish the in-class quiz if you did not finish it in class. Bring your completed quiz (including your box-and-pointer diargrams) to your next class session.

Use the Session25_Pointers project you checked out from your individual SVN repository into your Eclipse C workspace to complete the exercises you started in class.  In particular, you must finish the TODO's, beginning from TODO 4.  Type your answers to questions 8 and 9 in a file named homework.txt (that you will create and add to your src folder) and commit to your repository. (27 pointers total for code and typed answers)

  1. (6 points) Write a function called doubleMe that takes a single argument that is a pointer to a double. The function doubles the value of the pointee. It does not return a value.

    Write code in main to test your doubleMe function.
  2. (8 points) Write a function called swap that takes two pointers to int's and swaps the values of their pointees. The function does not return a value. Hint: you'll need to use a temporary (local) variable to accomplish the swap.

    Write code in main to test your swap function.
  3. (6 points )  Write a function called minAndMax that takes four arguments: two integers, x and y, and two pointers to integers, pMin and pMax. The function sets pMin's pointee to the smaller of x and y, and pMax's pointee to the larger of x and y. Note that this is an example of using pointers to "return" multiple values (here, the min and the max of two numbers).

    Write code in main to test your minAndMax function.
  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.