1 |
h10 |
CS16 F16 |
Name: | ||||
---|---|---|---|---|
(as it would appear on official course roster) | ||||
Umail address: | @umail.ucsb.edu | section 9am or 10:30am |
||
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: Homework 10
ready? | assigned | due | points |
---|---|---|---|
true | Thu 10/27 02:00PM | Tue 11/01 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.)
Please:
- No Staples.
- No Paperclips.
- No folded down corners.
Read Chapter 5.4 thru 6.1. (If you do not have a copy of the textbook yet, there is one on reserve at the library under “COMP000-STAFF - Permanent Reserve”).
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
FOR BEST RESULTS, PRINT THIS PAGE AS A PDF, THEN PRINT THE PDF
- (6 pts) Why would a programmer use a driver program? What about a stub?
- (4 pts) What is the “Fundamental Rule for Testing Functions” actually telling us?
- (4 pts) When debugging a program, explain why randomly changing code is generally not a good idea.
4.(2 pts) Show where you would insert an assert statement in this code in order to make sure that you would not get a run error:
#include <iostream>
#include <cassert>
using namespace std;
void foo(int x, int y) {
y = 5 / x;
cout << "Result = " << y << endl;
}
int main() {
int a, b;
cin >> a >> b;
foo(a, b);
return 0;
}
5.(12 pts) What member functions do the following in C++11? Show the usual parameters/arguments, if any:
Opens a file in ofstream classes:
Opens a file in ifstream classes:
Closes a file in ofstream classes:
Checks to see if opening an input file failed in ifstream classes:
Returns the position in the input file where you want to start reading in ifstream classes:
Returns the position in the output sequence in ofstream classes:
6.(12pts) When users run the following program: (a) What do they see on their screen? and (b) What do they see in the file myData.txt?
#include <iostream>
#include <fstream>
int main () {
using namespace std;
ofstream out_stream;
int x = 55, y =66;
out_stream.open("myData.txt");
for (int i = 0; i < 5; i++)
out_stream << x << y << endl;
out_stream.close();
cout << "File is written";
return 0;
}