1 |
h11 |
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" |
h11: Homework 11
ready? | assigned | due | points |
---|---|---|---|
true | Tue 11/01 02:00PM | Thu 11/03 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 6.1 thru 6.3, in addition to the extra material on string manipulation from the class lecture (#11). 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, SAVE THIS PAGE AS A PDF, THEN PRINT THE PDF.
1.(2 pts) If I wanted my double type variables to be displayed in scientific notation, with a precision of 3 decimal places, and always have a plus (+) sign in front of positive numbers, what code should I include in my program before issuing a cout statement?
2.(4 pts) Show the output produced when the following line is executed (assume library
cout << "*" << setw(3) << 12345 << "*" << endl;
3.(4 pts) When testing for end of file, we talked about two methods. What are they and when is each used?
4.(10 pts) I have a text file called “t.txt” that contains two entries: “UC Santa Barbara” on one line, and “Computer Science” on the other line. Show the output produced when the following code (entire program not shown) is executed. You are encouraged to also try to compile this in a program to verify your results.
ifstream tin;
tin.open("t.txt");
char c = ' ';
tin.get(c);
while (!tin.eof()) {
if ( (c != 'e') && (c != 'a') )
cout << c;
tin.get(c); } // end while
5.(10 pts) Using the examples shown in class (non_numbers_char.cpp and non_numbers_string.cpp), extend BOTH programs to check not just for numbers, but for all alphanumeric characters (that is, numbers and letters).
Attach your modified programs in a separate sheet to this homework assignment. Please optimize your printout to not exceed 1 page for this answer.
6.(10 pts) Show the output produced when the following code (entire program not shown) is executed. You are encouraged to also try to compile this in a program to verify your results.
string name = "Jeffery Tambor";
cout << "NAME = " + name << endl;
cout << name.length() << endl;
name.erase(8, 6);
cout << name << endl;
name.append("Dean WD Morgan");
cout << name << endl;
name.insert(22, "@TWD");
cout << name << endl;
name.replace(23, 3, "The WD");
cout << name << endl;
cout << name.find("WD") << endl;
cout << name.rfind("WD") << endl;
cout << name.rfind("fery") << endl;
for (int i = name.length(); i > 20; i--)
cout << name[i-1];
cout << endl;