This sheet is linked to the lecture I/O Formatting. Main concepts:
sticky manipulators for formatting integer output:
<< hex
, << oct
, << dec
What are hexadecimal numbers used for in computer science? Why?
<< showbase
What is the prefix for decimal? For octal? For hexadecimal?
Sticky manipulators:
<< general << setprecision(5)
<< fixed << setprecision(5)
<< scientific << setprecision(5)
Sticky manipulator:
<< setw(4)
Difference between character I/O and binary I/O.
Opening files for reading and writing (open modes)
Positioning in a file
What happens if you position a file position beyond the end of file?
string name;
getline(cin,name);
Type stringstream
isalnum(c)
do?This is the Drill on pages 407:
Test_output.cpp
. Declare an integer
birth_year
and assign it the year you were born.birth_year
in decimal, hexadecimal, and octal form. cin >> a >>oct >> b >> hex >> c >> d;
cout << a << ' \t ' << b << ' \t ' << c << ' \t ' << d << ' \n ' ;
Run this code with the input 1234 1234 1234 1234
.
Explain the results.
1234567.89
three times, first
using defaultfloat , then fixed , then scientific forms. Which output
form pre- sents the user with the most accurate representation?
Explain why.Write a well formatted table in which for each of the following types
int
unsigned int
short int
long int
float
double
char
the largest and smallest representable number is given. Use scientific notation and 10 digits precision.
[Tip: Use the “C++ reference” link of the course web page to find out how to accomplish this task with function std::numeric_limits<double>::lowest()
from the library limits
or with the system variables defined in climits
. ]
Using the system function sizeof
and the function as_bytes
defined
in std_lib_facilities
, write a small program that outputs the number
of bytes and the content of each of the bytes in hexadecimal numbers for
the number 1234 when represented as
int
short int
long int
float
double
string
Review the code on the Punctuation example from the book.
Decrypt the following message (use Punct_stream to eliminate punctuation):
SURJUDPV PXVW EH ZULWWHQ IRU SHRSOH WR UHDG, DQG RQOB LQFLGHQWDOOB IRU
PDFKLQHV WR HAHFXWH.
Download the FLTK library from fltk.org and
install it. Once uncompressed, go in the soruce directory and type make
.
During the installation you might need to install also the following packages:
sudo apt-get install g++
sudo apt-get install gdb
sudo apt-get install git
sudo apt-get install autoconf
sudo apt-get install libx11-dev
sudo apt-get install libglu1-mesa-dev
sudo apt-get install libxft-dev
To learn about FLTK you can see the official
documentation and the
examples in the directory examples/
from the installation.
Download the Graph_lib.tgz interface from the course web page.
Get an empty Simple_window
with the size 600 by 400 and a label My
window compiled, linked, and run.
Add to the code from the previous task the examples from the slides of the lecture of Nov 5 one by one, testing between each added subsection example.
Go through the code again and make one minor change (e.g., in color, in location, or in number of points) to each of the subsection examples.
Draw a 100-by-30 Rectangle and place the text “Howdy!” inside it.
Draw your initials 150 pixels high. Use a thick line. Draw each initial in a different color.
Draw the graph of Task 2 in Sheet 6. Make up some coordinates for the points. For example, place the points at equal distance in a circle.