DM560, Introduction to Programming in C++

Sheet 7

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?

  • When would you prefer line-oriented input to type-specific input?
    string name;
    getline(cin,name);
    
  • Type stringstream

  • What does isalnum(c) do?

Task 1

This is the Drill on pages 407:

  1. Start a program called Test_output.cpp. Declare an integer birth_year and assign it the year you were born.
  2. Output your birth_year in decimal, hexadecimal, and octal form.
  3. Label each value with the name of the base used.
  4. Did you line up your output in columns using the tab character? If not, do it.
  5. Now output your age.
  6. Was there a problem? What happened? Fix your output to decimal.
  7. Go back to 2 and cause your output to show the base for each output.
  8. Try reading as octal, hexadecimal, etc.:
     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.

  9. Write some code to print the number 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.
  10. Make a simple table including last name, first name, telephone number, and email address for yourself and at least five of your friends. Experi- ment with different field widths until you are satisfied that the table is well presented.

Task 2

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. ]

Task 3

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

Task 4

Review the code on the Punctuation example from the book.

Task 5

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.

Task 6

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.

Task 7

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.

Task 8

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.

Task 9

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.