1
h10
CS16 Su17
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu section
Optional: name you wish to be called
if different from name above.
Optional: name of "homework buddy"
(leaving this blank signifies "I worked alone"

h10: Chapter 10: Pointers

ready? assigned due points
true Tue 08/01 02:00PM Tue 08/08 02:00PM

You may collaborate on this homework with AT MOST one person, an optional "homework buddy".

MAY ONLY BE TURNED IN IN THE LECTURE/LAB LISTED ABOVE AS THE DUE DATE,
OR IF APPLICABLE, SUBMITTED ON GRADESCOPE. There is NO MAKEUP for missed assignments;
in place of that, we drop the three lowest scores (if you have zeros, those are the three lowest scores.)


Reading: Read Chapter 10.1. Also read this tutorial on pointers and memory from Stanford CS library (pages 1-8, 11-23) http://cslibrary.stanford.edu/102/PointersAndMemory.pdf. PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

1.(4 pts) List two reasons why you might choose to pass parameters to a function by address or reference, rather than by value in your C++ programs.

2.(6 pts) Draw a pointer diagram to demonstrate how the state of memory changes as the following code is executed. Cross out old values/arrows and draw new ones. Is this program likely to result in a segmentation fault? If so, why?

   int num = 10, *ptr1 = &num, *ptr2=0;
   if (ptr2) ptr1 = ptr2;
   else if(ptr1) ptr2 = ptr1;
   (*ptr1)++;

3.(8 pts) On page 14 of the “Pointers and Memory” handout there is a reference to the “Amperand (&)” Bug in the TAB() function. Draw a trace of the local variables of the functions Victim() and TAB() on the run-time stack as these functions are executed. Your diagram should be similar to the one provided on page 13. Your diagram should depict exactly three instances in program execution - T1: Right after the first statement of Victim(), T2: right before the return statement in TAB(), T3: Right after the call to TAB(). Use your diagram to explain the “Amperand (&)” bug and why it is likely to lead to a runtime error when the last line of the Victim() is executed?

4.(8 pts) Write the definition of a structure type called UndergradStudents. This structure should contain student ID numbers, first and last names, major, and GPA scores for each undergraduate year.

5.(14 pts) Write a program that uses the definition of the structure UndergradStudents from the previous question to declare and then initialize an array of 3 objects of this structure (hint: you can do this with the same approach you define/initialize an array of any other type). You must initialize the values in the program, not by user input. The initial values are shown in the table below. Then write the definition of a function with the signature void printRecords(struct UndergradStudents *records, int numrecords); The function should print out the values of the array of objects passed to it as shown in the sample below, along with each student’s AVERAGE GPA score (calculated to a precision of 2 decimal places). You must use a loop to print the output. Your program should appropriately call the printRecords() function to print the student records and the average GPA.

ID First name Last Name Major GPA Yr1 GPA Yr2 GPA Yr3 GPA Yr4
1 Joe Shmoe EE 3.8 3.3 3.4 3.9
2 Macy Chen CS 3.9 3.9 4.0 4.0
3 Peter Patrick ME 3.8 3.0 2.4 1.9

OUTPUT:

These are the student records:
ID# 1, Shmoe, Joe, Major: EE, Average GPA: 3.60
ID# 2, Chen, Macy, Major: CS, Average GPA: 3.95
ID# 3, Pan, Patrick, Major: ME, Average GPA: 2.77