CSSE 120 -- Intro. to Software Development
Homework 24
-
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.
-
Complete the assigned reading for the next session: listed on
the
schedule page.
- (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
-
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)
- Open homework.txt and add your name to it.
- ----------POINTERS AND OUTPUT----------
- Look at main.c's basicPointers() function, but don't run it yet.
- (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 pts) Predict the output of basicPointers() BEFORE RUNNING IT
and write it in homework.txt.
- We will give you full credit for any reasonable guess, so don't go back and
change your answer after you run it!
- (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.
- If you think your original box-and-pointer diagram was
incorrect, please correct it.
- Look at main.c's everybodyUp() function, but don't run it yet.
- (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.
- (3 pts) Predict the output of everybodyUp() BEFORE RUNNING IT.
- We will give you full credit for any reasonable guess, so don't go back and
change your answer after you run it!
- (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.
- If you think your original box-and-pointer diagram was
incorrect, please correct it.
- ----------WRITING FUNCTIONS----------
- (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.
- (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.
- ----------SCANF----------
- (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 pts) Try omitting the & when you call scanf. What happens?
- ----------POINTER PITFALLS----------
- (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);
- 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.