CPS 6: Lab #3

Summer 1999

Parameters

6 points

Due: Tuesday, June 1 - 11:59pm

Change into your "cps6" directory using the "cd" command and create another directory called "lab3" using the "mkdir" command. Change into the "lab3" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/lab3" If you don't see this, then raise your hand.

In order to do this lab, you need to copy some files using the following "cp" (for "copy") command (don't forget the trailing period, or dot):

       cp  ~dr/cps6/lab3/*  .

This command will copy the files in the directory ~dr/cps6/lab3 into your directory for you to use.

If you type "ls" to list the files now in your lab3 directory, you should see the following files: Makefile, domath.cc and oldmac.cc

For each of the programming problems that follow, you should use the style rules discussed in class, which includes indentation, meaningful variable names and comments at the top of the file and for each function.

Use the "make" command to compile a C++ program. In order to use this command, there must be a file called "Makefile" in the same directory as the program. This file should already be in your lab3 directory. For example, to compile the program called oldmac.cc type: make oldmac

This will automatically use the g++ command and link to libraries that you'll need for this assignment. If your program compiles correctly, the executable file called "oldmac" will be created. Then you can type "oldmac" to run your program.

Problem 1: Debug (2 pts)

The program domath.cc is a program that is supposed to read in three numbers, print the three numbers, print the sum of the three numbers, and print the product of the three numbers. However, the program doesn't compile because it is missing function prototypes and it has some logic (or semantic) errors (it doesn't do what it is suppose to).

First start up emacs by typing "emacs &" and open the file "domath.cc". Compile this program and note the error messages. Modify domath.cc by adding the missing function prototypes. Once the correct function prototypes have been added, domath.cc should compile with no errors.

Now you should fix the logic errors. Below is the correct output for a run of the program:

       Enter three numbers: 
       3.0 2.0 4.1
       The sum of 3, 2, and 4.1 is 9.1
       The product of 3, 2, and 4.1 is 24.6

Correct this program with a few changes (do not completely rewrite or make major changes) to produce the correct output, recompile it and run it.

Problem 2: Removing Files (1 pt)

Don't remove any of your CPS 6 C++ programs until the end of the semester. However, the executable files created when you compile a program take up lots of space, so they should be removed.

List the files in your lab3 directory, by typing the UNIX command "ls". To see their size, type the same command with a flag: "ls -lsa". You'll see something like:

    640 -rwxr-xr-x   1 dr   prof      315752 Aug 27 15:33 domath
      2 -rw-r-----   1 dr   prof         796 Aug 27 15:32 domath.cc

At the right end of the line is the name of the file. Moving to the left from this end, you'll see the time and date the file was created. The number to the left of the month (796 for domath.cc above) is the number of characters in the file. Note that your C++ program "domath.cc" is much smaller (has fewer characters) than the executable file "domath". Another size indicator is the first number on each line. This is the number of "blocks" of space a file is taking up. 1 block is approximately 512 characters. (domath.cc above takes up 2 blocks)

Type the command "quota" and you will see the number of blocks you are allowed to have (your quota), and the number and percentage you are currently using. You'll also see how much space is left on the disk (or partition) where your files are stored. If the partition fills up, then you can't create any new files.

Let's remove some executable files. To remove a file, type "rm filename". For example, now type rm domath to remove the domath executable file. Once you remove a file it is gone, so be careful deleting files.

For this part of the assignment, raise your hand and show one of the lab tas the following.

  1. Move to your lab2 directory.
  2. List the files in your lab2 directory.
  3. Remove all the executable files in your lab2 directory.
  4. Show how to "recreate" the file "parts"
  5. Remove the file "parts"
  6. Move back to your lab3 directory.

Note: You don't need to leave your executable files around since they take up a lot of space, and you can always recreate them by compiling. You should make it a habit to delete an executable after you have a program working correctly.

Problem 3: The Hen says CLUCK, CLUCK (3 pts)

The file oldmac.cc contains a program for printing out a verse of the Old Macdonald song. It currently prints out the verse for a pig.

Start up emacs by typing "emacs &" and open the file "oldmac.cc". Fill in the comments at the top of the file (Name, etc.). Compile the program and run it to see what it does.

Make the following changes in the main function. You might want to make each change and test it first (compile and run the program), instead of making all the changes and trying to debug everything all at once.

  1. Ask the user to type in the name of a farm animal and read in this name.

  2. Let's make it easy on the user. If the user enters one of the animals listed below, instead of asking the user to type in the sound, use the sound listed below corresponding to that animal. If the user enters an animal that is not listed below, then ask the user for the sound the animal makes.

    Animal Sound
    cow moo
    duck quack
    goat baa
    hen cluck
    pig oink

  3. Print a blank line followed by the corresponding verse of the old macdonald song.

Below is sample input and output for two runs of the program. Note that in the first run, the user is not asked for the sound of the cow.

> oldmac
Enter an animal: cow

Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
And on his farm he had a cow, Ee-igh, Ee-igh, oh!
With a moo moo here
And a moo moo there
Here a moo, there a moo,  everywhere a moo moo
Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
> oldmac
Enter an animal: horse
Enter the sound a horse makes: neigh

Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
And on his farm he had a horse, Ee-igh, Ee-igh, oh!
With a neigh neigh here
And a neigh neigh there
Here a neigh, there a neigh,  everywhere a neigh neigh
Old MacDonald had a farm, Ee-igh, Ee-igh, oh!
> 

  • Extra Credit (1 pt)

    Make a copy of your oldmac.cc program and name it oldmacX.cc. This part should produce the same output as above, but is structured differently.

    Create a function called AnimalToSound that takes an animal as a parameter and returns the sound the animal makes. This function should work the same as in step 2 above, but is now a separate function. (See Chapter 4.5 on functions that return values).

    Modify the main function to ask the user to type in an animal and then call the function AnimalToSound to obtain the sound the animal makes. Then print out the corresponding verse. NOTE: Extra credit must be submitted at the same time as your other programs.

    Submission of programs:

    When your programs for problems 1 and 3 compile and produce the correct output, create a README file. This file should include your name, section number, the date, how long you worked on these programs, and anyone you received significant help from. You can then turn everything in by typing ( oldmacX.cc is left off if you did not do the extra credit):

                ~dr/bin/submit6 lab3 README domath.cc oldmac.cc oldmacX.cc
    

    You should receive a message telling you that the programs were submitted correctly.