CMSC 202 Spring 2003
Lab Assignment 2

Name:_____________________________________ Checked:__________________________________
SSN:______________________________________ Date:_____________________________________

Purpose

  1. Introduction to the use of strings, vectors and streams.
  2. Formatting of output.

The Assignment

  1. Logon on to your GL account.
    (Note: Please log on to the Linux machines, i.e., when you telnet or ssh, make sure the machine name is linux.gl.umbc.edu. Do not log on to the Irix machines.)
  2. Create a directory called lab2 for this week's lab. This should most conveniently be ~/cs202/labs/lab2.
  3. Copy the file /afs/umbc.edu/users/a/n/aniket1/pub/202/labs/lab2/floats.dat to this directory. This file contains a series of floating-point numbers, one per line, to be used for this lab.
  4. Write a program in the file floats.C that does the following:
    1. Prompt the user for a filename using cout.
    2. Input the filename into a string using cin.
    3. Open the file.
    4. Input the values from the file one by one into a float variable using >>.
    5. Store the values into a vector using push_back().
    6. Print the number of values using size().
    7. Print the values in reverse order in a column with decimal points aligned.
    8. Print each value with three (3) decimal places.
  5. Compile floats.C. Do NOT use printf and/or scanf.
  6. Execute the program.
  7. After completing the program, have the TA verify your work so that you receive credit for today's lab.

Note

  1. Make sure you #include all the files needed to correctly use streams, strings and vectors, and to format the output.
  2. When asked to input a filename, type floats.dat.

Sample Output

[linux1:/afs/umbc.edu/users/a/n/aniket1/home/202/labs/lab2] ./lab2 
Enter a filename: floats.dat
Original values:
3.454
0.0032
-45.44
-1.33
-0.0004
1
89983

Number of values: 7

Values formatted in reverse order with decimal points aligned:
  89983.00
      1.00
     -0.00
     -1.33
    -45.44
      0.00
      3.45

Values formatted to 3 decimal places:
3.454
0.003
-45.440
-1.330
-0.000
1.000
89983.000
[linux1:/afs/umbc.edu/users/a/n/aniket1/home/202/labs/lab2]

Tips

  1. To get the decimal points aligned in a column, set the width of the output to some value. Then set the precision as desired.

Feedback

Please tell us how helpful this lab was to you on a scale of 1 (waste of time) to 10 (very helpful):______