lab10, CISC106, Fall 2006

Overview

This lab provides more practice with C++

This lab will be very similar to lab07, but we'll do it in C++ instead of in MATLAB.

You may want to review the idea of a "Design Recipe" introduced in lecture of Monday October 16:

Here is also a Wiki Page for the "Design Recipe" idea, but written in C++

Prerequisites

Before starting this lab:

Step-by-Step Instructions

Step 1: Preparing your lab10 directory, and copying files you will need

Step 1a: Create a new subdirectory for lab10

Create a new subdirectory ~/cisc106/lab10, and make that your working directory.

If you are not sure how to do this, then review the instructions from lab03, steps 1a through 1d.

Step 1b: Copy the files needed for this week's lab

There are two ways to copy the files for this week's lab. Read over both methods before deciding which one to use.

  1. Download them from the web page http://www.udel.edu/CIS/106/pconrad/06F/labs/lab10
  2. If you are working on strauss, copy them directly from the directory /www/htdocs/CIS/106/pconrad/06F/labs/lab10

Consult step1b of lab07 if you need a refresher about how to do this.

Step 2: Examining the example programs

As in past weeks, there is nothing to turn in for Step 2

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

But don't skip this step.

By now, I'm guessing you already know why. (Look back at lab04 step 2, lab05 step 2, and lab06 step 2 if you still need this pounded into your head.)

Step 2a: A C++ files that defines a function: areaOfRect.cc

Previously, we've worked with M-files that define MATLAB functions.

In the lab10 directory, there is an example of a C++ files that defines a function: areaOfRect.cc

Take a look at this C++ source file. To test it out, we need to take several steps. We'll talk more about these steps in lecture, but you have enough here to get started:

The steps involved in defining functions in their own files in C++
  1. write one or more driver program—for example:
  2. compile the areaOfRect.cc files into an object file areaOfRect.o
    CC -c areaOfRect.cc
  3. compile each of our drivers into a .o file
    CC -c testAreaOfRect.cc
    CC -c rectPizza.cc
  4. link the object files for our function definition and the object file for our driver into a single executable, with the following commands:
    CC areaOfRect.o testAreaOfRect.o -o testAreaOfRect		
    CC areaOfRect.o rectPizza.o -o rectPizza
  5. run the executable

You can also use g++, but you have to use either all CC or all g++. Within a given directory or project, you have to choose one compiler and stick with it. (You switch compilers half-way, but if you do, you need to use the rm command to delete all the .o files, and recompile everything from the beginning.)

Now you try it

Look at the code inside all three files:

Then try the steps outlined above, to see if you can produce the following output:

strauss.udel.edu% CC -c areaOfRect.cc
strauss.udel.edu% CC -c testAreaOfRect.cc
strauss.udel.edu% CC areaOfRect.o testAreaOfRect.o -o testAreaOfRect
strauss.udel.edu% ./testAreaOfRect
Test 1...passed
Test 2...passed
strauss.udel.edu% CC -c rectPizza.cc
strauss.udel.edu% CC areaOfRect.o rectPizza.o -o rectPizza
strauss.udel.edu% ./rectPizza
Please enter length of pizza: 10
Please enter width of pizza: 5
Please enter price of pizza: 7.99
Price per square inch is 0.16
strauss.udel.edu%   

 

 

Step 2b: Looking at the myMin.cc and testMyMin.cc files

Look at the files myMin.cc and testMyMin.cc:

In a later stage, you'll be asked to develop your own files myMax.cc and testMyMax.cc which are similar to these files, except that myMax.cc computes the maximum element in an array.

If you have questions, Your TA and I will freely explain how these files work, and how one might come up with these from scratch.

However, we'll be a lot less free with the explanation of how to do Steps 3 and 4! That is for you to figure out on your own. So ask questions at step 2 if you don't understand. Once you do, you are ready to tackle some problems on your own.

How to compile and run myMin.cc and testMyMin.cc

Hopefully, you can do this on your own—it is a good test of whether you understood what you did at the earlier step. But just in case you need help, you can click the link below to reveal the answer:

http://www.udel.edu/CIS/106/pconrad/06F/labs/lab10/compileMyMin.txt

Step 3: A function to compute price per square unit of a disc

Step 3a: Design a file areaOfDisc.cc

Write an C++ file, using the first three steps of the "Design Recipe" steps, that defines a function that computes the area of a disc (e.g. a pizza).

  1. identify the contract (what type of data the function consumes, and what it produces) and write the function header and the comment preceding the function definition
  2. come up with at least two examples, and document them in the comment preceding the function definition
  3. develop the body of the function

It may be useful to consult the Wiki Page about Design_Recipes in C++ for additional help on how to proceed.

When your function is complete and it tests continue to the next step to test it, thus completing step 4 of the design recipe.

Step 3b: Write a regression test driver program: testAreaOfDisc.cc

Using the example programs testMyMin.cc and testAreaOfRect.cc as a model, write a regression test script for areaOfDisc.cc. Use your examples that you came up with in step 2 of the design recipe.

Refine your C++ source files areaOfDisc.cc and testAreaOfDisc.cc until all the tests pass.

Step 3c: Write an interactive driver roundPizza.cc

Using the example program rectPizza.cc as a model, write an interactive driver called roundPizza.cc that uses your areaOfDisc.cc function to compute the price per square inch for a round pizza.

Important Instructions (deductions will be made for failing to follow these instructions)

Step 4: Create C++ files to define and test a function myMax

Step 4a: Design an C++ function myMax (stored in myMax.cc)

Use the Design Recipe to design a function myMax() and store it in a file called myMax.cc. This function should take an array of integers, and the number of integers in that array, and return the maximum number in that vector. It should operate in a manner similar to the myMin.cc function provided in the lab10 directory, which you may use as a model.

Step 4b: Design an C++ main program testMyMax.cc to do regression testing for myMax.cc

Following the steps outlined previously develop a regression testing file called testMyMax.cc to do regression testing on the function you developed in step 4a.

If you aren't sure what to do, review step 2

Step 5: Create a script file lab10.txt to show your work.

Script file vs. Diary File

A special note should be made about the use of the words script file in this lab.

When working with MATLAB, the word script file refers to a special kind of M-file, namely one that just contains a sequence of MATLAB commands to be carried out in order to solve a problem. We distinguish script files from the M-files that define functions.

We talk about diary files as the files where we keep a record of what we did in a particular MATLAB session

However, when we work with C++, when we refer to a script file, we are talking about a record of our work, i.e. the same thing we call a diary file when working with MATLAB.

Please make a note of this!

Making the script file lab10.txt

Once the programs developed for this lab work, create a script file called lab10.txt in which you type out each of the C++ files you developed in steps 3 and 4, and also demonstrate that they work properly.

Order for Script file

Step 6: Creating a zip file of your work

The following example shows how to create a zip file called lab10.zip containing all the .cc files in the directory lab10.

The example is followed by an explanation. The part you type is in bold.


strauss.udel.edu% pwd /www/htdocs/CIS/106/pconrad/06F/labs/lab10 strauss.udel.edu% cd .. strauss.udel.edu% pwd /www/htdocs/CIS/106/pconrad/06F/labs strauss.udel.edu% zip lab10.zip lab10/*.cc adding: lab10/areaOfRect.cc (deflated 48%) adding: lab10/rectPizza.cc (deflated 53%) adding: lab10/testAreaOfRect.cc (deflated 54%) strauss.udel.edu%

Explanation

In your case, you should have five .cc files in all

Step 7: Upload and Submit your work on WebCT

For lab07, you had to submit seven files in all—the six M-files, and then the one diary file lab07.txt.

But this week, since we are using a zip file, you only have to submit two files:

A few important DOs and DON'Ts


That's it for lab10!


Grading

  1. areaOfDisc.cc (20 pts)
  2. testAreaOfDisc.cc (10 pts)
  3. roundPizza.cc (10 pts)
  4. myMax.cc (20 pts)
  5. testMyMax.cc (10 pts)

End of lab10 for CISC106, Fall 2006 (100 pts)