lab09, CISC105, Fall 2007

Overview

This lab provides more practice with C++

You may want to review the idea of a "Design Recipe" from the following wiki pages:

Prerequisites

Before starting this lab:

Step-by-Step Instructions

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

Step 1a: Create a new subdirectory for lab09

Create a new subdirectory ~/cisc105/lab09, and make that your working directory.

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

The files for this lab are in the directory /www/htdocs/CIS/105/haggerty/07F/labs/lab09

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.

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 2a: A C++ files that defines a function: areaOfRect.cc

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

In the lab09 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/haggerty/07F/labs/lab09/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 lab09 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 lab09.txt to show your work.

Script file vs. Diary File

As a reminder—when working with C++, we refer to a script file to mean what we called a diary file when working with MATLAB. Review the information about this in lab10.html if you need further clarification on this point.

Making the script file lab09.txt

Once the programs developed for this lab work, create a script file called lab09.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 lab09.zip containing all the .cc files in the directory lab09.

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


strauss.udel.edu% pwd /www/htdocs/CIS/105/haggerty/07F/labs/lab09 strauss.udel.edu% cd .. strauss.udel.edu% pwd /www/htdocs/CIS/105/haggerty/07F/labs strauss.udel.edu% zip lab09.zip lab09/*.cc adding: lab09/areaOfRect.cc (deflated 48%) adding: lab09/rectPizza.cc (deflated 53%) adding: lab09/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

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 lab09!


Due Date: Tue, 11/27/2007 11:55pm

Grading: 100 pts

  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 lab09 for CISC105, Fall 2007