Lab07, CISC181, Spring 2004

Background

Your Deitel/Deitel text sometimes functions like a textbook, but other times, it functions more like a reference manual. For this lab, the "actual" reading assignment is Chapter 7, which continues the coverage of classes from Chapter 6. However, the following sections may also be useful as "reference" material.

Part 0: Copying files and checking your disk quota

Note: There is stuff to turn in from part 0 this week!

Throughout this lab, you'l be using files from the lab07 subdirectory. You might want to just copy all the files from that subdirectory into your current directory now. Here's a command to do it:

cp -r ~pconrad/public_html/cisc181/04S/labs/lab07 .

This will copy the entire lab07 subdirectory, along with all its contents, into the current directory. You'll then have to cd into lab07 to see all the files. The -r stands for recursive.

(Note: that command was in both labs 6 and 7, so you can certainly expect that it might show up on Midterm 2!)

In addition, you should take this opportunity to check your disk quota on strauss.

Type "quota -v" at the Unix prompt. You'll see some numbers such as the following:

> quota -v
Disk quotas for pconrad (uid 53430):
Filesystem     usage  quota  limit    timeleft  files  quota  limit    timeleft
/scratch           0      1      1                  0      0      0            
/home/usra     12944  20240  20240               3417  76800  76800            
> 

You need to compare the number listed under "usage" with the number listed under "quota". For example, in the listing above, the usage is 12944 (somewhere been 12MB and 13MB) and the quota is 20240 (20MB). This is good; this is about where you want to be.

If on the other hand, your usage is very close to your quota, you may run into lots of difficulties. Compling, editing, logging on and off of the Sun Ray servers—all of these operations may begin to fail or produce strange errors.

In this case, you need to clean up your disk space usage. Here are some useful commands which you should learn (and know for the next exam!)

> cd
> du -sh *

This sequence of commands changes directory to your main directory, and then gives you a list of all your files and directories in your main directory, along with a summary of the disk usage for each file or directory. The -s flag indicates that you will get a summary of all the disk space under a particular directory, while the -h flag indicates that you will get results reported in K or M, standing for Kilobytes (units of 1024 bytes) or Megabytes (units of a bit over a million bytes, 1024 x 1024 to be exact).

A good candidate for deleting is your old "executable" files; a.out files, and any file you create with the -o flag from the compiler. They tend to be large, and once you've produced your script, you don't need them anymore; you can always bring them back any time you like by recompiling. But DON'T get rid of the source files (.cpp, .cc, .h. .dat, etc.) for your old labs; you may need them for future labs this semester! Only delete the exectuable files, and the .o files (ask your TA if you are not sure.)

To turn in: a script file lab07p0.txt in which you run the "quota -v" command and show that your disk usage is at least 3-4MB less than your quota. That's the amount of "headroom" you need in order to effectively do programming work. If your usage is higher than that, use the "du -sh *" command to do some deleting. You do NOT have to script the deleting part; only the "quota -v" command showing the end result of your clean up needs to be in your script.

Part 1: Function Overloading

The topic of "function overloading" is crucial to a discussion of classes, especially when it comes to understanding constructors. When we covered functions, we glossed over section 3.20, but we need to revisit that now.

The idea of function overloading is similar to the idea of "multiple senses of a word" in the English language. Consider the uses of the word "rose" in the following three sentences:

In one sentence, "rose" refers to a flower. In another "rose" is the past tense of "rise", and in the third, "rose" refers to a color. The word "rose" by itself is ambiguous—capable of having multiple meanings. We "disambiguate" the word by looking at the surrounding context.

In the same way, a given "name" in C++ can refer to different functions. We can "disambiguate" the word by looking at context. The main idea of "function overloading" is that in C++ (unlike in plain C), a programmer may include more than one function with exactly the same name in the same program. This is called "overloading" the function name. So, for example, you can define two (or more) different functions that are both named "squared". However, each of the versions of the function must have different parameters. This is how the compiler can distinguish between the different functions when you make a function call. Again, the technical term is "disambiguate"; the compiler is "removing the ambiguity" that is created by having multiple functions with the same name, by using context: in this case, the number and type of parameters.

If possible, review section 3.20 of your textbook before proceeding (if you don't have a textbook handy, you can still proceed with the lab, but you'll find it easier if you get a chance to look over section 3.20). You can find the source code for the program from Figure 3.25 of your text in the lab07 subdirectory mentioned in Part 0 of this lab (filename: fig03_25.cpp). Copy this file into your account and run it.

Then, write your own program that illustrates the principle of function overloading. Call your source code lab07p1.cpp or lab07p1.cc. Your program should include a main function, and three functions, each of which is named readInteger. Each of the functions should return an integer, but they will differ in the number of types of parameters:

  1. The first one should take no parameters at all (i.e. it should have the following prototype:
    int readInteger(void);
    

    Inside that function, print a prompt on cout that says "Please enter an integer >". Read a value in, and return that value as the result of the function.
  2. The second version of the readInteger function should take one integer parameter min, for example:
    int readInteger(int min);
    

    This function should read an integer from cin that is greater than or equal to min and return that value. It should prompt the user with a message like the following: "Please enter an integer >= 2", except that instead of 2, you should print out the value of min that was passed in.

    This function should check the value entered by the user; if it is less than min, print an error message, and ask the user to enter the value again. Repeat this process until the user enters a valid value, and then return that value as the result of the function.

  3. The third version of the function will take two integer parameters: min and max. The idea is the same as for the second version of the function, except this time, you are checking that the value is between min and max, inclusive. This time, its up to you to come up with the function prototype. Your function should provide an appropriate prompt, issue an appropriate error message if needed, and return only after the user enters a legal value.
  4. Once you have written the three functions, write a main program that will test these three functions, and demonstrate that each of them works properly. If you aren't sure what is expected, you can use Figure 3.25 from Deitel/Deitel as a model.
  5. Now, before you script, you are going to split your work into three separate files:

Then make a script lab07p1.txt where you cat your source files, compile them and run the executable to show that it works.

Part 2: Writing to an output file with ofstream

In the same way that you can read from an input file in a program with ifstream, there is also a constructor for an "ofstream" object that allows you to write to an output file directly from inside your program.

Copy the file ofstreamDemo.cc to your account, compile it and run it. For Part2 of this lab, do the following steps. The result will be a script file lab07p2.txt, which you will submit.

  1. Copy the ofstreamDemo.cc file into your directory.
  2. Compile it, but don't run it yet.
  3. Before you run it, do an ls command, and note that there is no file called output.txt in your directory.
  4. Now run the program. Note the output you get on your screen. Then do another ls command and note that there should now be a file called output.txt in your directory. Use cat output.txt to list the output of this file.
  5. Now edit the ofStreamDemo.cc file to personalize the message that is going to the file. Make it print your name to the file as part of the message, along with any other pithy comments you want to pass along to your TA and/or instructor.
  6. Use the rm command to delete output.txt from your directory. Then, make a script file lab07p2.txt in which you
    1. cat and then compile your new (personalized) version of ofStreamDemo.cc,
    2. use ls to show that there is no output.txt file in your directory,
    3. run the program,
    4. redo the ls command to show that output.txt was created,
    5. and cat the output of your new personalized output.txt.

Part 3: Lab 4, revisited

By the time you start this lab, your lab04 should be graded and returned to you. (If not, please contact your TA and your instructor by email.) Look at the feedback from your TA (check either the comments on WebCT, or if your TA returned you a printed copy, check there). Consider this feedback, and then write a new version of lab04, that incorporates the "NumberSource" class that Prof. Conrad has been developing in lecture. Prof. Conrad will talk (or did talk) more about this in lecture today. You can find the source code for the NumberSource class in the "code/lect" directories under the main web site for the course; check the recent lecture dates, and the appropriate directory for your lecture section.

The adapted program should have the following features:

Call your program finddups.cpp (find duplicates). Call the executable "finddup". So for example, you'll run it with:
> ./finddup 10 100 5 1 F

That will run the program with min=10, max=100, n=5, option=1, and F for full output. That is, the program will run algorithm 1, asking the user for 5 numbers, all between 10 and 100, tell the user new or duplicate for each of the 5 numbers, then it will run algorithm 2, asking the user for 10 more numbers all between 10 and 100, and tell the user new or duplicate for those 5 numbers. (This is similar to Part 1 of lab04, except we are only inputting 5 numbers instead of 20 numbers.)

Suppose instead, the user used the command line:

> ./finddup 1 50 1000 2 S

In this case, the program would run the first algorithm on 1000 random numbers between 1 and 50, then run the second algorithm on the same 1000 random numbers between 1 and 50, showing only summary output. (This is similar to what we did in part 2 of lab04.) To hand in: script your program, with a script called lab07p3.txt, where you cat, compile, and run your source code. Include the NumberSource.cpp and NumberSource.h files in your script file along with your main. Be sure to show the output of at least the two example runs listed above, plus one additional run with values you choose yourself (to show that the values are not hard coded, but are in fact coming from the command line.)

Grading


Phillip T Conrad