CISC181h, Lab03, Spring 2007

 

Introduction

We'll first investigate some variable types

In this lab, we'll first explore the concept of type, and look at some of the different types of variables that you can declare in C++.

You may already familiar with int, float, double, and char. Those are the types we examine in this lab. In future labs, we will look a bit at some other types that may be more unfamiliar, or may need special explanation:

This is by no means a complete list of all the types in C++, but it is a "good start".

A future lab will also explore concepts of addresses, pointers and how memory is organized in C++.

We'll then do more with user-defined functions, and nested loops

We'll then look at a program that creates a multiplication table using a nested loop. This program is already written for you—your task is to understand how it works.

Once you understand it, your job is to write a program to generate a table of wind-chill factors, following the same model, i.e. using a nested loop.

Along the way, you'll

Step-by-Step Instructions

Step 0: Copying Files

Files for this lab come from the subdirectory:

/www/htdocs/CIS/181h/pconrad/07S/work/labs/lab03

These files are also available on the web at:

http://www.udel.edu/CIS/181h/pconrad/07S/work/labs/lab03

To get started, you should copy all the files from that subdirectory into your own ~/cisc181/lab03 directory.


Note: Previous labs cover the commands for creating a new ~/cisc181/lab03 subdirectory and copying all the files from my lab03 subdirectory into your subdirectory, so refer to those labs if you are not sure what to do at this step. Next lab I'll just give you the name of the directory where can find the files and tell you to copy them into your own directory.

Step 1: Exploring the concept of type

First: there is nothing to turn in for Step 1.

The work you do in Step 1 of this lab is only for practice and learning. There is nothing to turn in from this step of the lab.

So you could skip it if you wanted to, and neither your instructor nor your TA would know.

Well, actually that's not entirely true. We'd know if,

So, don't skip this step.

Step 1a: Using a Makefile to compile some code

When you copied the files in Step 0, one of the files you copied was called Makefile. As discussed in lecture, this file is used to automate the process of compiling and linking code.

Do the following steps. (Example output for these steps appears right after the list of steps.)

  1. Use the Unix cd command (change directory) to make your current directory be the one where you copied these files, i.e. your ~/cisc181/lab03 directory,
  2. Type an ls command to list your directory contents.
  3. Then type make
    Notice that the system automatically performs a sequence of commands to compile the programs in your current directory
  4. Type another ls command to see the new executable programs that are now present
    (e.g. multTable, types1, types2, etc.)
  5. Type the name of one of the programs, e.g. types1 to run that program and see the output.

Example output for these five steps:

strauss.udel.edu% cd ~/cisc181/lab03
strauss.udel.edu% ls
Makefile  multTable.cpp  types1.cc  types2.cc  types3.cc  types4.cc
strauss.udel.edu% make 
g++ multTable.cpp -o multTable
g++ types1.cc -o types1
g++ types2.cc -o types2
g++ types3.cc -o types3
g++ types4.cc -o types4
strauss.udel.edu% ls
Makefile  multTable  multTable.cpp  types1  types1.cc  types2  types2.cc  types3  types3.cc  types4  types4.cc
strauss.udel.edu% ./types1
i=2
f=2.6
d=2.6
c=A
strauss.udel.edu% 

Step 1b: Take a look at the Makefile, and try to understand it

I am hopeful that by this point in the semester, we will have covered Makefiles in lecture enough that you will be able to somewhat understand the contents of the Makefile—though I realize that until you start working with Makefiles on your own, most of what I say in lecture may not "stick".

So in this lab, I want to get you used to the idea of using a Makefile to compile your code. You'll also add some lines of code to this Makefile so that you can use it to compile the program that you write this week (namely lines of code to compile lab03.cc)

If you have questions about how the Makefile works,

Step 1c: Now look at the source code for program types1.cc

The program type1.cc is very simple. It declares four simple scalar variables with types int, float, double and char, and then assigns them each vaules. It then prints them out using cout and the stream insertion operator (<<)

There is nothing earth shattering here—this is just our departure point. Make sure you understand the code. Also, run the program with the command ./types1, as shown below. Your output should look similar:

strauss.udel.edu% ./types1
i=2
f=2.6
d=2.6
c=A
strauss.udel.edu% 
 

One thing that may strike you is that f and d (declared with types float and double) may seem to be storing the same value, i.e. 2.6. As we will see, this is only an illusion. We are going to shatter that illusion in the three programs which follow.

Step 1d: Now consider program types2.cc

Look at the source code, and also run the program and look at the output. This program introduces two new ideas: setw() and sizeof. Notice both of these in the code, and what the output is, and read the explanations below.

The setw() stuff

In this program, setw() is used as a stream manipulator that "sets the width" of something that is being printed.

When setw(5) appears after a stream insertion operator, it means that the very next thing inserted into the output stream should be printed so that it is 5 characters wide.

This topic is explained in more detail on pages 538—543 of your Savitch "Absolute C++", 2nd Edition text, along with examples of other useful stream manipulators.

It is a bit unfortunately that it takes so long to get to this topic in the text. You might be worried about reading something from Chapter 12, when we haven't even gotten all the way through Chapters 1 through 6 yet.

No worries—the material on pages 538-542 should be no problem for you to read, and I think you'll find it very useful.

There is also some information about setw on the CISC181 wiki page: HowToZeroFill

Note that when using setw() in this way, you must also put #include <iomanip> at the top of your C++ source file.

The sizeof stuff

The keyword sizeof can be used in C++ either as a built-in unary operator, or as a built-in function. In the program types2.cc, it is used as a unary operator. It operates on the variable that follows it. The result is an integer representing the number of bytes of memory that are taken up by that variable.

From the output, you can see how many bytes are consumed by an int, a float, a double, and char. It should be clear from the output where the double type gets its name—it is just like a float, except that it takes up "double" the amount of space.

To get the number of bits that a variable takes up, we need to multiply the sizeof output by 8, i.e. 8 bits per byte. So if sizeof i returns 4, then i takes up 32 bits of memory.

Step 1e: Now consider program types3.cc

Look at the source code, and also run the program and look at the output. This program introduces the setprecision() stream manipulator, which indicates how much precision should be used when printing floating point numbers (either float or double.)

But, it also shows something very important about floating point numbers—the idea that many numbers that can be expressed as decimal numbers (e.g. 3.6) have no exact representation in binary—thus a float or double value is only an approximation of that number

Look carefully at the output, and try to understand what causes the value 3.6 to come out looking so different as the setprecision values are changed.

Step 1f: Finally consider program types4.cc

Now look at the source code for types4.cc, and run the executable ./types4

Compare types4.cc with types3.cc. You should see that the only differences are in the comments, and in the values assigned to the variables d and f.

However, if you run the program, the result is very different! See if you can figure out why.

Hint: It has something to do with binary, and powers of two—especially negative powers such as 2-1, 2-2, etc.

The Unix diff command

To see that types3.cc and types4.cc differ only in the comments and the value assigned to the variables, d and f, you might try using the Unix diff command, as shown below.

Here's how to interpret the diff output:

strauss.udel.edu% diff types3.cc types4.cc
1c1
< // types3.cc  P. Conrad  CISC181 Spring 2007
---
> // types4.cc  P. Conrad  CISC181 Spring 2007
7a8,15
> // This program is the same as types3, but the value used is 3.75 instead
> // of 3.6.   Note the difference in program behavior.  
> // What is the explanation for this difference?  
> 
> // Hint 1: It have anything to do with "powers of two" and "binary"
> // Hint 2: It has something to do with "negative powers",
> //         For example, two raised to the -1 power.
> 
17,18c25,26
<   float f = 3.6;
<   double d = 3.6;
---
>   float f = 3.75;
>   double d = 3.75;
strauss.udel.edu% 

That's it for Step 1

What you should have gotten from Step 1 was:

In "Step 1" of lab04, we'll look a bit at pointers, addresses, and explore a few more data types.

Step 2: Windchill table

Files you will copy from my web page: multTable.cpp
Files you will create and submit via WebCT
(also print ones in bold)
lab03.cc, lab03.txt

Introduction

In Step 2, you'll start by looking at a program multTable.cpp, which uses nested loops to generate a multiplication table.

Then you'll write a similar program that uses nested loops to produce a table to convert windspeed and temperature into wind chill factors.

multTable.cpp? Why not multTable.cc?

There is no virtually no difference between calling a file multTable.cpp or calling it multTable.cc. Both .cpp and .cc are recognized file extensions for C++ source code.

The reason there are two different extensions is rooted in history. This is an oversimplification, but the general idea is that the Unix world mostly used .cc, while the PC world started with .cpp. Eventually, both Unix and PC compilers began accepting either file extension.

It is better, within the context of a given project, to stay consistent—use either all .cpp or all .cc. I will, however, sometimes switch it up between .cpp and .cc. You may encounter both in your later career, and I want you to be accustomed to the fact that there is really no difference.

The only signficant difference I'm aware of between using .cc and .cpp is this: the make program has "built in rules" for handling .cc, but not .cpp. However, if you write your own rules, which we normally do, this difference doesn't matter.

Step 2a: Understanding the multTable.cpp source

The file multTable.cpp illustrates an example of a program that uses nested for loops to produce a multiplication table similar to the one you might see hanging in a 3rd or 4th grade classroom.

Start by using "make" to compile this program (if you haven't already—it was probably already compiled as part of Step 1 ).

Once it's compiled, run this program with the command ./multTable

Then take a look at the source code. A few things you should look at in the source are

Step 2b: Write a program called lab03.cc to make a wind chill table

Your task is to write a program called lab03.cc, which will produce a table of wind chill factors. The formula that you should use is given on the web site: http://www.nws.noaa.gov/om/windchill/index.shtml (see the formula at the bottom of the table.)

You'll need the "pow function" to compute the exponents; you can read about it in Chapter 3 of your Savitch "Absolute C++" textbook.

You are only going to produce a subset of the table shown on the web site http://www.nws.noaa.gov/om/windchill/index.shtml . Here is an example of what your output should look like:

> CC lab03.cc -o windChill
> ./windChill
           Temperature (degrees F)        
------------+-------+-------+-------+-------+-------+-------+
 wind (mph) |    40 |    30 |    20 |    10 |     0 |   -10 | 
------------+-------+-------+-------+-------+-------+-------+
         10 |    34 |    21 |     9 |    -4 |   -16 |   -28 | 
         20 |    30 |    17 |     4 |    -9 |   -22 |   -35 | 
         30 |    28 |    15 |     1 |   -12 |   -26 |   -39 | 
         40 |    27 |    13 |    -1 |   -15 |   -29 |   -43 | 
         50 |    26 |    12 |    -3 |   -17 |   -31 |   -45 | 
> 

 

Your program should include a function as follows:

double windChill(int windMph, int tempF)
{
   return ... // you fill in the rest
}
 

You will call that function inside two nested for loops that print the table; one iterates over windMph, and the other over tempF. For example, for temperature, you might use a for loop such as:

for (w = 10;  w<=50; w+=10)

For this lab, it is ok to include the function definition in lab03.cc

By the time we get to this lab, I will probably have discussed in lecture the fact that function definitions often appear in a separate file from the main. This helps in many ways—here are three of them:

However, for this lab, we'll still keep things simple—please include the function definitions for both windChill() and main() in the same file, lab03.cc. That way, lab03.cc is a "standalone main", just like the other programs in this lab.

We'll introduce separate compilation in lab04.

Step 3c: Modifying the Makefile

To add your lab03 program into the Makefile, do the following:

  1. Add the name of your new executable to the list of BINARIES. That is, change this:

    BINARIES = multTable types1 types2
    

    to:

    BINARIES = multTable types1 types2 lab03
    
  2. Add a rule to produce the executable file lab03 from the source file lab03.cc , like this. Be sure that the second line starts with a TAB character—there should be no spaces between the beginning of that line, and the characters ${CCC}

    lab03: lab03.cc
           ${CCC} lab03.cc -o lab03
    
  3. That's it! Now, if you type make at the Unix command prompt, the system should try to compile your new lab03.cc program.
  4. To fully test the Makefile, type make clean, followed by make

Step 3d: Scripting

Now make a lab03.txt script in which you:

 

Grading

lab03.cc correctness of code 20
style (e.g. appropriate use of comments and indentation, appropriate variables names, etc.) 20
lab03.txt following instructions (included all required steps—e.g. used a Makefile to compile, listed contents of all files, etc.) 20
Makefile included the Makefile in the submission on WebCT, and made changes to the Makefile to compile lab03.cc 20

Following
Instructions

Followed instructions for submitting on WebCT, and if your TA asked for it, a paper copy. 20

 


Valid XHTML 1.1 Valid CSS!