CS16, Winter 2010

lab06: ("lab six")
Reusing functions, more work on arrays


This lab is a little shorter than usual—
but you also have a quiz today!

If you want to study for the quiz, see my number systems practice web page.

Goals for this lab

By the time you have completed this lab, you should be able to:

This lab builds on lab05

This lab is an extension of lab05—so if you run into anything unfamiliar, review the material from lab05.

Continue with your same pair partner—
unless he/she is unavailable, or you failed to switch last week.

At the start of lab05, you were supposed to switch to a new pair partner. If you did so, and your partner is available again this week, please continue working with the same person.

Also keep in mind:

 

Step by Step Instructions

Step 0: Get together with your assigned lab partner and do the set up steps.

Choose a pilot and a navigator, and remember to switch often.

Check your PC—if you are in Cooper, there might be two mice attached. The navigator might be able to use the second mouse to help point things out, and feel a bit more connected to what is going on.

The driver should:

Refer back to lab04 for clues about how to turn that directory name into a Unix command that will copy the files in to your lab06 directory. (This is the last week I'll tell you where to find that.)

Step 1: Reviewing what we did in lab05—and what we are going to do next

Assuming you successfully completed lab05, you did the following steps:

Did you have the sense that you were working too hard—that it was inefficient to have to write the same function twice? I hope so—because if that thought occured to you, then you have what Larry Wall (author of Perl) calls one of the Three Virtues of a Programmer: Laziness.

Wall writes that Laziness is "the quality that makes you go to great effort to reduce overall energy expenditure."

Wouldn't it be better if the functions that we wrote and tested in the countOddWithTests.c program could be directly used in the countOddInFile.c program?

Well it turns out that we CAN do this.

Your first step this week will be to illustrate that by re-doing a step from lab05—yes, we are going to implement isOdd and countOdd one more time—but this time, we'll see that its the last time, because we'll implement it once and then reuse it in as many programs as we want.

Step 2: Reviewing the files oddFunctions.c, oddTester.c, countOddInFile.c

Three of this weeks files are a re-mix of the files we saw in lab05. Together with your lab partner, take a look at each one of them:

Since testOddFunctions.c does NOT contain the function definitions for isOdd and countOdd, if we try to compile it the way we've compiled programs so far, it doesn't work. Try this yourself—you should see output like the following:

-bash-3.2$ make testOddFunctions
cc testOddFunctions.c -o testOddFunctions
/tmp/ccOzuKZF.o: In function `main':
testOddFunctions.c:(.text+0xc4): undefined reference to `isOdd'
testOddFunctions.c:(.text+0xeb): undefined reference to `isOdd'
testOddFunctions.c:(.text+0x112): undefined reference to `isOdd'
testOddFunctions.c:(.text+0x139): undefined reference to `isOdd'
testOddFunctions.c:(.text+0x160): undefined reference to `isOdd'
/tmp/ccOzuKZF.o:testOddFunctions.c:(.text+0x187): more undefined references to `isOdd' follow
/tmp/ccOzuKZF.o: In function `main':
testOddFunctions.c:(.text+0x1b5): undefined reference to `countOdd'
testOddFunctions.c:(.text+0x1e3): undefined reference to `countOdd'
testOddFunctions.c:(.text+0x211): undefined reference to `countOdd'
collect2: ld returned 1 exit status
make: *** [testOddFunctions] Error 1
-bash-3.2$
But if instead, we take the compile command that make generates for us, namely:
cc testOddFunctions.c -o testOddFunctions

and add to it the file oddFunctions.c, like this, it works!

-bash-3.2$ cc oddFunctions.c testOddFunctions.c -o testOddFunctions
-bash-3.2$
We can then run the program with ./testOddFunctions

We can also compile the countOddInFile.c program the same way:

-bash-3.2$ cc oddFunctions.c countOddInFile.c -o countOddInFile


We can then run the program with
./countOddInFile
or with
./countOddInFile TEST
to run the tests

Try compiling each of these programs using the command lines shown above, and running it.

What you should see is that the programs run but the tests fail.

 

Step 3: Adding the isOdd and countOdd functions one last time

Now, one last time, enter the correct code for isOdd and countOdd in the file oddFunctions.c in place of the stubs.

Change the code at the top of the file too, to have your name(s) (you and your pair partner(s)) and the date.

Compile and run again, using the commands from Step 2 above. Now all the tests should pass!

And, also you should be able to run the countOddInFile program on a data file, e.g.:

-bash-3.2$ ./countOddInFile nums.dat
(number of odd numbers in file appears here)
-bash-3.2$

 

Once you've done that, you are finished... and ready to submit—except that you need to take the quiz on numbers systems.

Step 7: Scripting your assignment

Before doing the transcript—look over the grading rubric at the bottom of this page. Make sure you've done everything properly to maximize your grade.

In this step, we create a "transcript" of your work. We only do this after everything else is finished, and you are sure you have a good working product.

Now, to create your script:

  1. Use the proper form of the cd command to put yourself in your ~/cs16/lab06 directory (if you aren't already there)
  2. Type script lab06.txt
    (By now you know what that does.)
  3. Type pwd to show what directory you are in.
  4. Type ls to show what files you have.
  5. Now for each of the programs you worked on this week—testOddFunctions and countOddInFile—do the following steps:
    1. Carefully—type rm filename to remove the executable for each of the programs that you worked on this this week
    2. Now, for each of the programs you worked on this week, type the command needed to compile your program. (This may be a "make" command, or it may be something more complex if your program is split across multiple files.)
      • There should be no warnings and no errors—and since you did the rm command, it should NOT say:
        filename is up to date
    3. Next, for each of the programs you worked on this week, run the progrto show that it works properly. In some cases you can do this with a single run. In other cases, you may need to run it several times with different input each time. If applicable, include command lines that show the "Usage" message, as well as several command lines that show it working properly with good input.

  6. Once you've done steps 5a,5b,5c for both testOddFunctions and countOddInFile, type exit to stop the recording of commands and responses into lab06.txt

When finished, type ls one more time, and you should see a new file in your lab06 directory called lab06.txt.

Use this command to list out the contents of that file:

cat lab06.txt

If it all looks ok, you are ready to submit!

Step 8. Submit your assignment using the turnin program on CSIL

To submit your assignment, you need to be in the ~/cs16 directory—one level higher than the previous step (use cd ..)

When you are in inside your cs16 directory, you are ready for the turnin step.

Type the following at the prompt:

turnin lab06@cs16a lab06

If you need detailed information about how turnin works, consult the instructions in lab00 through lab02.

 


Evaluation and Grading (200 pts total)

Due Date: You should try to complete this assignment by the end of the discussion section in which it was assigned.

If you are unable to complete it by the end of your discussion section you may continue to work on it through the week—ideally before the start of next week's discussion section.

It will be accepted without late penalty until 5pm on Friday Feburary 19.

Late assignments will only be accepted (with 20 point penalty) through 5PM on Wednesday Feb 24th

After 5PM Wednesday Feb 24th, a zero will be recorded, and the only option is to make up the points via extra credit.


Copyright 2010, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.