1
h06
CS16 F17
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")

h06: File I/O and String Manipulation

ready? assigned due points
true Thu 11/02 02:00PM Thu 11/09 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 LISTED ABOVE AS THE DUE DATE. There is NO MAKEUP for missed assignments.


PLEASE MARK YOUR HOMEWORK CLEARLY, REGARDLESS OF IF YOU WRITE IT OUT IN INK OR PENCIL!

    1.(2 pts) What is the most important rule to remember if I want to use a function to read or write a file?
    2.(3 pts) If I wanted my double type variables to be displayed in scientific notation, with a precision of 4 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?
    3.(5 pts) Show the output produced - be precise! - when the following line is executed (assume library is included in the program):

    cout << "*" << setw(3) << 12345 << setw(3) << "*" << endl;

    4.(10 pts) Show the output produced - be precise! - when the following line is executed (assume library is included in the program):
        int pr_number(8);
        for (int i = 0; i < 5; i++)
        {
            cout << "Project" << setw(2);
            cout << pr_number++ << endl;
        }
    
    5.(5 pts) When testing for end of file, we talked about two methods. What are they and when is each used?
    6.(10 pts) I have a text file called "t.txt" that contains two entries: "University of California" on one line, and "Computer Science Rules!" on the other line. Show the output produced when the following code (entire program not shown - so assume all the necessary set ups are done) is executed. You are encouraged to also try to compile this in a program to verify your results.
      ifstream tin;
      char c;
      tin.open("t.txt");
      tin.get(c);
      while (!tin.eof()) 
      {
        if ( (c != 'e') && (c != 'C') ) cout << c;
        tin.get(c); 
      }  // end while
    
    7.(15 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 = "Porcupine Tree";
    
        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("cupi") << endl;
    
        for (int i = name.length(); i > 20; i--)
            cout << name[i-1];
        cout << endl;
    
    8.(20 pts) Using the examples shown in class (non_numbers_char.cpp and non_numbers_string.cpp - look for them in the demo code section of our class website), extend BOTH programs to check not just for numbers, but for *all* alphanumeric characters. Attach your modified programs in a separate sheet to this homework assignment. Please optimize your printout to **not exceed 1 page for this answer**.
    9.(30 pts) Write an entire program that will generate 1000 random numbers between 0 and 99 and place them, each on their own line, in a text file. The program will them read the text file and present the user with the squarre root of the sum of all the numbers in the text file on the display (standard output). Attach your program on a separate sheet to this homework assignment. Please optimize your printout to **not exceed 1 page for this answer**. </ol>