1
h07
CS16 F18
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"

h07: Chapter 9: Dynamic memory allocation

ready? assigned due points
true Tue 11/20 12:30PM Wed 11/28 12:30PM

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.)


Read Chapter 6, Chapter 9, sections 9.1-9.2 (pages 508 - 533). The key learning goals are dynamic memory allocation and its use in dynamic arrays and linked-lists. Please turn in the physical copy of your homework in person. Write in PEN or DARK PENCIL.

PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL! FOR BEST RESULTS, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.

1.(10 pts) What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory, clearly marking elements on the run-time stack and on the heap. Mark the size of all data objects. Assume the code is embedded in a correct and complete program.

int *p1, *p2, *p3;
p1 = new int;
p2 = new int;
p3 = p1;
*p1 = 20;
*p2 = 30;
cout<< *p1<< " "<< *p2<<" "<<*p3<< endl;
p1 = p2;
cout<< *p1<< " "<< *p2<<" "<<*p3<< endl;
*p3 = *p2;
cout<< *p1<< " "<< *p2<<" "<<*p3<< endl;

2.(10 pts) What is the output of the following program? Using a pointer diagram show the evolution of all data objects in memory, clearly marking elements on the run-time stack and on the heap. Mark the size of all data objects. Assume the code is embedded in a correct and complete program.

int array_size = 4, *a ;
a = new int[array_size];
int *p = a;
for(int i=0; i< array_size; i++)
    *(a+i) = 2*i;
p[0] = 10;
for(int i=0; i< array_size; i++)
    cout<<a[i]<<" ";
cout<<endl;

3.(10 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.

4.(20 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, Peter, Patrick, Major: ME, Average GPA: 2.77