CIS105 Fall 2003 Lab04

Lab04, CIS105, Fall 2003    Instructor: P. Conrad
Alternative to Chapter 4 in the lab manual.

Goals
=====

In this lab, you will learn how to use the "-o" option of the "cc" command
to produce an executable file other than "a.out".

The programs in this lab will familiarize you with the following C topics,
covered in Chapters 2 and 3 of Deitel.

* simple conditionals (if) and two-way conditionals (if ... else )
* relational operators ( <=, <, !=, ==, >=, > )
* while loops
* finding maximum, count, sum, average of a sequence of values

Background: A new command form for the compiler
===============================================

After you use vi (or emacs) to edit a C program in a file such as
lab4a.c, up until now you have used the following command to
compile your code:

      cc lab4a.c

This would produce an executable file "a.out", which you can run with the
command:

      ./a.out

But the "a.out" technique is typically used only by beginners, or for
short informal test programs.  The problem with always using "a.out"
as the output from the compiler, is that you can only have one
executable program at a time in your directory.  

We are now ready to move on to the compiling technique used by more
experienced programmers.

In this lab, your first program will be called "lab4a.c".  To compile
it, we are going to give the cc command an option to tell it to give
the executable file the name "lab4a" instead of "a.out"; namely:

      cc -o lab4a lab4a.c

When you type this in, you will not get an a.out file.  Instead you will
get the file "lab4a".   You run this file with the command:
    
    ./lab4a

(You might or might not need the "./" at the beginning depending on how
your account is set up, but it never hurts to have it.  It tells Unix to
look for the program in your current directory.  Sometimes Unix doesn't
look in your current directory for programs, but only in system directories.
The "./" helps Unix find your program in that case.)

Take another look at this command:

   cc -o lab4a lab4a.c

In this command:
   the option "-o" is the "output" option of the cc command,
   the argument of the "-o" option is "lab4a".  

That is, "-o lab4a" says "put the output in a file called lab4a".

Note that you can also type "make lab4a" at the Unix prompt and the
system will translate this into the appropriate command "cc -o lab4a
lab4a.c".  Try it!  We'll learn more about the "make" command as the 
semester goes on.

You can use "make foo" as shortcut for typing "cc -o foo foo.c" anytime
you like from now on.  Also, from now on, if the lab manual tells you to
create an a.out file, I prefer that you use the "-o" option to create
an executable that has the same name as the .c file, but without the
.c at the end (e.g. for lab06c.c, use "cc -o lab06 lab06.c", and run
your program with "./lab06"). 

The benefit of this approach is that you can have multiple executables
in your directory, all with different names.   


Part 1: Conditional and Relational Operators (lab4a.c, lab4a.txt)
==============================================================

Problem Statement: 

    The program will prompt the user to enter a temperature 
    in degrees farenheit, and read the value entered by the user.

    The program will then print instructions according to the
     actions that need to be taken based on the temperature entered.

    If the value is less than or equal to 32 degrees, 
      print "run heater, check pipes".

    If the value is more than 32 degrees, but less than 60 degrees,
      print "run heater".

    If the value is 60 degrees or higher, but less than 65 degrees,
      print "no heater, closed windows".

    If the value is 65 degrees or higher, but less than 75 degrees,
      print "open windows"

    If the value is 75 degrees or higher, print "run air conditioning".
   
    A series of "if ... else if ... else" statements is a reasonable
    way to approach this problem.

What to submit to WebCT:
========================

    You should submit two files for this part of the lab.  The first one,
    lab4a.c, is your C code.  You should also create a script file (lab4a.txt)
    with the following sequence of commands:

script lab4a.txt
cat lab4a.c
rm lab4a
cc -o lab4a lab4a.c
./lab4a

 [then repeat the ./lab4a command several more times, at least 5 times
 so that you demonstrate that your program can produce each of the 
 different messages.   When done, to end the script, use the exit command.]

exit

Part 2: While Loop, finding the maximum (lab4b.c, lab4b.txt)
=======================================

Problem Statement: 

    The program will prompt the user to enter a temperature 
    in degrees farenheit, and read the value entered by the user.

    The prompt should also tell the user to enter the special
    sentinel value "999" to signify that there are no more temperatures to
    enter.

    The program should repeatedly ask for temperatures until
    the sentinel value "999" is entered.

    When the user indicates that there are no more temperatures,
    the program will print out a message indicating the 
    maximum temperature that was entered.

    Note that if "999" is the only value ever entered by the user,
    there is no "meaningful" value for the maximum; in that case the
    program should print an appropriate message, such as "no valid
    temperatures were entered".

What to submit to WebCT:
========================

    The file lab4b.c (your C code), and a script file (lab4b.txt) in
    which you use "cat" to list out the program code, use "rm lab4b"
    to get rid of any old executable file, compile the program with
    "cc -o lab4b lab4b.c" (or "make lab4b"), and then run the program
    at least twice, with different input each time.  Use at least 4
    values on each run.

Part 3: While Loop, finding the average (lab4c.c, lab4c.txt)
=======================================

Problem Statement: 

    The program will prompt the user to enter a temperature 
    in degrees farenheit, and read the value entered by the user.

    The prompt should also tell the user to enter the special
    sentinel value "999" to signify that there are no more temperatures to
    enter.

    The program should repeatedly ask for temperatures until
    the sentinel value "999" is entered.

    When the user indicates that there are no more temperatures,
    the program will print out the number of temperature values
    that were entered, and the average of those values.

    Note that if "999" is the only value ever entered by the user,
    there is no "meaningful" value for the average; in that case the
    program should print an appropriate message, such as "no valid
    temperatures were entered".

    (Hint: you need to both count how many values were entered,
     as well as compute the sum of all the values in order to
     calculate an average.   You should avoid dividing by zero
     by checking for the special case where the user enters 
     999 as the first value.)

What to submit to WebCT:
========================

    The file lab4c.c (your C code), and a script file (lab4c.txt) in
    which you use "cat" to list out the program code, use the
    appropriate command to get rid of any old executable file, compile
    the program with a command that produces an executable called
    "lab4c", and then run the program at least twice, with different
    input each time.  Use at least 4 values on each run.
    
Overall Grading: 
===============

  lab4a.c 20 points lab4a.txt 10 pts
  lab4b.c 20 points lab4b.txt 10 pts
  lab4c.c 20 points lab4c.txt 10 pts
  submitting everything according to instructions: 10 pts


Phillip T Conrad
Last modified: Sun Oct 19 19:23:01 EDT 2003