1 |
h07 |
CS16 F19 |
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 6: File IO, Chapter 9: Dynamic memory allocation
ready? | assigned | due | points |
---|---|---|---|
true | Mon 11/11 11:00AM | Sat 11/16 11:59PM |
You may collaborate on this homework with AT MOST one person, an optional "homework buddy".
UPLOAD A PDF OF YOUR ANSWERS TO GRADESCOPE BEFORE THE DUE DATE. ASSOCIATE EACH QUESTION WITH A SPECIFIC PAGE IN YOUR HOMEWORK AT THE TIME OF SUBMISSION. There is NO MAKEUP for missed assignments;
Read Chapter 6, Chapter 9, sections 9.1-9.2 (pages 508 - 533).
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
1.(5 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.(5 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.(5 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.(10 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(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
5.(3 pts) In C++, all the files are opened in ______ mode:
(a) Binary
(b) Text
(c) Can't say
6.(5 pts)Please fill 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;
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();
return 0;
}
Output
Writing to the file
Enter class name: name
Enter your id: 123
Reading from the file
name
123
7.(2 pts) The <fstream> header provides three classes for operating file IO: ifstream, ofstream and fstream. What are these three used for respectively?
8.(5 pts) Write a C++ program to write the numbers 1 to 100 in a data file NOTES.TXT