1 |
h11 |
CS16 S17 |
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") |
h11: Homework 11: File I/O and String Manipulation
ready? | assigned | due | points |
---|---|---|---|
true | Thu 05/11 12:30PM | Tue 05/16 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 LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.
In place of that, we drop the two lowest scores (if you have zeros, those are the two lowest scores.)
PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!
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 example shown in class in file non_numbers.cpp, extend the program (in the 2 relevant places) to check, not just for numbers, but for all alphanumeric characters (that is, numbers and letters). Test your program out and make sure it works. Staple your modified program in a separate sheet to this homework assignment.
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;