Lab4

**CS 215 Summer 2012** **Lab 04** //**"C++ Tools"**//

Electronic submission to the CS portal: []
=== Put your program and your answers to the exercises in a .zip file and upload it to the CS Portal. ===

**Goals and Objectives**

This laboratory is intended to familiarize you with a few important C++ programming tools and techniques. Some programming languages are intended to create a very abstract programming environment that completely obscures the underlying computer hardware (//e.g.,// Java, Lisp, Prolog, SNOBOL, //etc.//). Not so C++. In C++, there is a close relationship between every data type and the underlying memory structure. Data must ultimately be mapped in a predictable way to a fixed number of linearly numbered memory bytes. The **sizeof** function will report the size (in bytes) of built-in or built-up (aggregate) types based on their type name, or it will compute and report the size of declared data items (//e.g.,// variables). Find the documentation for the **sizeof** and review its syntax and usage.

**Exercise 1** Use the **sizeof** function to write a program that computes and outputs the size of each of the following fundamental types: char, bool, short, int, long, unsigned int, unsigned long, float, double The fact that data types map to a fixed number of bytes implies that every data type is limited in its range and/or precision. These limits are known //a priori// for any given compiler implementation, and their values are stored in the implementation-specific header files “ LIMITS.H ” and “ FLOAT.H <span style="font-family: LiberationSerif,serif;">”.

<span style="font-family: LiberationSerif,serif;">**Exercise 2** <span style="font-family: LiberationSerif,serif;">Find these header files (" <span style="font-family: Courier New,serif;">LIMITS.H <span style="font-family: LiberationSerif,serif;">" and " <span style="font-family: Courier New,serif;">FLOAT.H <span style="font-family: LiberationSerif,serif;">") in the your system. According to the header files, what are the smallest (//i.e.,// most negative) and largest (//i.e.,// most positive) values that can be represented as <span style="font-family: Courier New,serif;">int <span style="font-family: LiberationSerif,serif;">, <span style="font-family: Courier New,serif;">unsigned int, long, unsigned long, float <span style="font-family: LiberationSerif,serif;">, and <span style="font-family: Courier New,serif;">double <span style="font-family: LiberationSerif,serif;">?

<span style="font-family: LiberationSerif,serif;">**Exercise 3** <span style="font-family: LiberationSerif,serif;">Chapter 4.9 of the textbook introduces the idea of random number generation. You will find that random number generation is very useful for analyzing the behavior and run-time of an algorithm and to construct a range of test cases that will reveal bugs in programs. This often requires us to generate random data with a known distribution. Typically this will be based on uniformly distributed random integers.

<span style="font-family: LiberationSerif,serif;">Find the documentation for the **rand** and **srand** functions declared in the **cstdlib** package and review their syntax and usage. <span style="font-family: LiberationSerif,serif;">Now declare a vector of <span style="font-family: Courier New,serif;">int <span style="font-family: LiberationSerif,serif;">values, and use a simple loop to fill it with random integers using **rand**. Calculate the average value, and output the contents of the vector and the average value. <span style="font-family: LiberationSerif,serif;">**Exercise 4** <span style="font-family: LiberationSerif,serif;">Use **srand ** with the seed value set to 904 and repeat exercise 3. Observe how the values and the mean have changed. Run this several times and observe any changes. Finally, change your use of **srand ** to mimic what is done in the example in the text (ch04/dice.cpp on page 170) – that is, include <span style="font-family: Courier New,serif;"> <span style="font-family: LiberationSerif,serif;"> and use the call **srand (time(0)).** <span style="font-family: LiberationSerif,serif;">How is this different from using a constant, like 904, in the call to the function srand?

<span style="font-family: LiberationSerif,serif;">Put your program and your answers to the exercises in a .zip file and upload it to the CS Portal.