1
h07
CS16 M18
Name:
(as it would appear on official course roster)
Umail address: @umail.ucsb.edu
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: File IO, Dynamic Memory Allocation

ready? assigned due points
true Tue 08/07 08:00AM Tue 08/14 11:00AM

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

MAY ONLY BE TURNED IN IN THE LECTURE 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 four lowest scores (if you have zeros, those are the four lowest scores).


Reading: Chapter 6, 9.1 - 9.2

  1. (10 pts) Fill in the information in the header. The following are required to get the 10 "participation" points.
    • Filling in your name and umail address.
    • For paper submission PLEASE submit on ONE SHEET OF PAPER, double-sided if at all possible. If you must submit on two printed sheets write name on BOTH sheets and no staples, paperclips, or folded corners.
  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 *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;
    
  3. (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;
    
  4. (6 pts) Write the definition of a structure type called UndergradStudent. This structure should contain a PERM number (int), first and last names (string), major (string), and cummulative GPA score (double).
  5. (8 pts) Fill in the blanks in the program below to make it output the expected output:
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    int main () {
    	string data;
    
    	_________________ outfile;
    	outfile.open("file.dat");
    	cout << "Writing to the file" << endl;
    	cout << "Enter class name: ";
    
    	__________________________________________________________
    	outfile << data << endl;
    	cout << "Enter your id: ";
    	cin >> data;
    	cin.ignore();
    	outfile << data << endl;
    	outfile.close();
    	ifstream infile;
    	cout << "Reading from the file" << endl;
    	infile.open("file.dat");
    
    	__________________________________________________________
    	cout << data << endl;
    
    	__________________________________________________________
    	cout << data << endl;
    	infile.close();
    }
    
    Output
    Writing to the file
    Enter class name: name
    Enter your id: 123
    Reading from the file
    name
    123
    
  6. (6 pts) Write a main function that writes numbers 1 to 100 to a data file named NOTES.TXT in C++. Each number should be written in its own line.