CMSC 202 Spring 2003
Lab Assignment 2
Name:_____________________________________ Checked:__________________________________
SSN:______________________________________ Date:_____________________________________
Purpose
- Introduction to the use of strings, vectors and
streams.
- Formatting of output.
The Assignment
- 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.)
- Create a directory called lab2 for this week's lab. This should
most conveniently be ~/cs202/labs/lab2.
- 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.
- Write a program in the file floats.C that does the following:
- Prompt the user for a filename using cout.
- Input the filename into a string using cin.
- Open the file.
- Input the values from the file one by one into a float
variable using >>.
- Store the values into a vector using
push_back().
- Print the number of values using size().
- Print the values in reverse order in a column with decimal
points aligned.
- Print each value with three (3) decimal places.
- Compile floats.C. Do NOT use printf and/or
scanf.
- Execute the program.
- After completing the program, have the TA verify your work so that you
receive credit for today's lab.
Note
- Make sure you #include all the files needed to correctly use
streams, strings and vectors, and to format the output.
- 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
- 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):______