Lab02 CISC105, Fall 2004, P. Conrad
Goals:
- You will write a C program that reads input from the keyboard, makes some
calculations, and prints formatted output on the screen. In the process you
will work with variables, printf, scanf, assignment statements and arithmetic
expressions. You will also have an opportunity to use #define to define constants
(e.g. for conversion factors).
- You'll fix up any left over problems from last week's web ring assignment
(see details in the file: http://udel.edu/~pconrad/cisc105/04F/labs/lab01_update.html)
Preliminary Steps
- Making a directory for lab02: At the start of each week's
lab, you should create a new subdirectory under your ~/cisc105/ directory
for that weeks lab. Currently, if you cd into ~/cisc105, you should see only
two files, just as in the following example terminal session (the characters
in bold are the ones you would be typing in). Try the commands in the example
below. Your output should look similar; the only difference should be that
the location of your home directory (/home/usra/d9/55560 in my case) will
be different.
> cd
>
pwd
/home/usra/d9/55560
>
cd cisc105
>
pwd
/home/usra/d9/55560/cisc105
>
ls
lab00 lab01
> |
If you have files other than lab00 and lab01 in your ~/cisc105 subdirectory,
you should move them into one of those two subdirectories, or get rid
of them. The terminal session listed below illustrates this (a more detailed
explanation is provided after the session listing.)
> cd
>
pwd
/home/usra/d9/55560
>
cd cisc105
>
ls
lab00 lab00.dat lab00.datgg lab00.dat~ lab01
>
cat lab00.dat
Phill Conrad
Wilmington DE
Arizona
pconrad
>
mv lab00.dat lab00
>
cat lab00.datgg
>
rm lab00.datgg
rm: remove lab00.datgg (yes/no)? yes
>
rm lab00.dat~
rm: remove lab00.dat~ (yes/no)? yes
>
ls
lab00 lab01
>
|
Explanation: We use cd to change directory into the cisc105 subdirectory. We
use pwd to print the current working directory. The ls command
is used to list out the files in the directory. Note that we use the mv command
move the lab00.dat file into the lab00 subdirectory where it belongs. We use cat to
list the contents of files. The file lab00.datgg is empty, and the name indicates
that it was probably just a mistake from last week, so we use the rm command
to remove it. The file lab00.dat~ is an emacs backup file. The emacs text editor
creates a backup file each time we make a change; the name of the backup file
is the same as the original name of the file, but with a tilde (~) at the end.
These backup files may be deleted or retained; it is entirely up to you. In
this case, we delete the file with the rm command.
- Now that your ~/cisc105 directory is cleaned out except for the lab00 and
lab01 subdirectories, make a new directory for lab02, as in the example in
the box below.
Note: Next week, we'll just tell you: make a new directory for
lab03, just like we did for lab02 last week. We'll expect you to be
able to do it without detailed instructions. The week after that, for lab04,
we won't even tell you to make a new directory for lab04; by then,
we'll expect you'll just know to do it. In general, you should follow
this kind of procedure for every course where you use your Unix account at
the University: create a subdirectory structure to organize your files.
> pwd
/home/usra/d9/55560/cisc105
>
ls
lab00 lab01
>
mkdir lab02
>
ls
lab00 lab01 lab02
> cd lab02
> pwd
/home/usra/d9/55560/cisc105/lab02
> ls
>
|
Goal 1: C programming with Arithmetic Expressions and Formatted
Output
Complete exercise 3.1 from your Tan and D'Orazio textbook, p. 161.
First, it is recommended that you sketch out your program on paper. When you
are ready to type it in, cd into your ~/cisc105/lab02 directory. You can then
use emacs (or vi) to edit this file.
(By the way: this is the last time I'll write "emacs (or vi)" this
semester; from now on, any time you see "emacs", you should just
assume that vi is an acceptable alternative unless I specifically mention otherwise.)
.
We will call the file lab02.c, so the emacs
command to create and edit this file
is:
emacs lab02.c
(There's only one program to write this week, so the name is just lab02.c,
not lab02a.c).
When entering your program in emacs, you may find that there are several sections
of code that are repetitive. Here, rather than retyping, you may find "copy
and paste" to be very useful. Ask your instructor to show you "copy and paste"
in emacs during lecture. The basic commands are: CTRL/space (or CTRL/@) to
"set the mark", Escape-w to copy, and CTRL/Y to paste (yank). In
emacs notation, that's
C-@,
M-W, C-y.
By now, the process of compiling and running your program should be familiar
to you, but one last time, here is the sequence of commands (in future labs,
you'll be expected to know this as well):
> cc lab02.c
> ./a.out
Table of Olympic running distances
----------------------------------
Meters Kilometers Yards Miles
100.00 0.100 109.40 0.062
200.00 0.200 218.80 0.124
400.00 0.400 437.60 0.249
800.00 0.800 875.20 0.497
>
|
Incidentally, unless I made a serious error when I completed the assignment
myself, that's the output you should expect from your program. (If my output
is incorrect, a perfect
score
on this
assignment is the reward offered to the first student to prove my
output incorrect and provide a listing of what the correct output
should look like.)
Also, your output should be neatly formatted along these
lines. Neatness
of your formatted output definitely counts!
The textbook was not specific in the choice of how many digits
to display for each of the columns, so I made what seem (to me) to be some
reasonable choices, given the available space in the various columns, and the
number of
significant digits in the conversion factors. There are probably other reasonable
choices, but it might be safer to match mine. :-)
If you are not sure where to start: read your textbook
Lessons 3.1 through 3.4,
and Application Programs 3.1 and 3.2 in Tan and D'Orazio will be very helpful
to you in understanding what to do. If you are confused as to how to go about
writing
this program,
read these sections
in the Tan and D'Orazio textbook (pages 75-115, and pages 149-157, plus the
reading notes for these sections (found at http://udel.edu/~pconrad/cisc105/td99/ch03).
Until you have done a detailed and careful reading of those sections (not
just skimming over them, but really spending some time studying them), don't
bother your TA or instructor with questions. However, once you have read
over those sections, or, if when you read them and have questions about them, then by
all means, please ask your TA and/or instructor for
assistance!
If you are still not sure: experiment with some examples
If you want
to experiment with the application programs found in the Tan and D'Orazio
text programs you can find them in the following directory
on strauss:
~pconrad/public_html/cisc105/td99/code
Particularly helpful will be the programs in the subdirectory Chapter3/Applications.
When logged into strauss, you can use the following sequence of commands to
go to
that
directory,
look
at
the
files
there,
and
copy one
of
the files
into
your lab02 directory. Note that I also use the "mv" command as a way
to "rename"
the file to a name that is more suitable for Unix (these files were originally
on a DOS formatted floppy disk.)
> cd ~pconrad/public_html/cisc105/td99/code
>
ls
Chapter2 Chapter3 Chapter4 Chapter5 Chapter6 Chapter7 Chapter8 Chapter9
READ.ME
>
cd Chapter3
>
ls
Applications Lessons
>
cd Applications/
>
ls
A3_1.C A3_2.C A3_3.C FRICTION.DAT
>
cp A3_2.C ~/cisc105/lab02
>
cd ~/cisc105/lab02
>
ls
A3_2.C
>
mv A3_2.C a3_2.c >
cc a3_2.c
>
./a.out
Table of Celsius and Fahrenheit degrees
Degrees Degrees
Celsius Fahrenheit
0.00 32.00
20.00 68.00
40.00 104.00
60.00 140.00
80.00 176.00
100.00 212.00
>
|
These files are also available at the following URL:
http://udel.edu/~pconrad/public_html/cisc105/td99
Finishing up (and grading)
To finish up, while logged into strauss, go to your ~/cisc105/lab02 subdirectory
(use a "cd" command), and start a script called "lab02.txt".
(Note: The command to start the script is "script lab02.txt". That's the last
time I'll spell things out in this much detail. In future weeks, I'll just
say: make a script called lab03.txt, or lab04.txt, etc. You'll
be
expected
to know
that
before
you start
your script,
you should "cd into your ~/cisc105/lab03 subdirectory", or "cd into your ~/cisc105/lab04
subdirectory",
as appropriate.)
- Enter a command to change your working directory to ~/cisc105/lab02.
(Next
week, I'll just say: a command to change your working directory to your lab03
subdirectory;
you'll be expected to know that means ~/cisc105/lab03).
- The "pwd" command to show that your previous command was successful.
Next week I'll just say: a Unix command to print your working directory.
- An "ls" command to list out your files.
Next week I'll just say: a Unix command to list out your
files.
- A command that lists out the contents of the file lab02.c
This one you should know by now. If you aren't sure, check
last week's lab!
- A command to compile your lab02.c file.
Either of the two C compilers available on strauss is fine;
use whichever one you like. We include the compile command in the script
to show that there are no warnings or errors. If there are, you should
fix them before submitting, if possible (though at some point, its better
to go ahead and submit with a warning, than to get a late penalty or a
zero).
- A command to run your program.
This week, since the program has no input (all the data is
hard coded into the program), you only need to do one run. In future weeks
however, if there is input from the keyboard or from a file, you might need
to run the program several times to show that it correctly handles different
kinds of input, different numerical values, and so forth.
- An "exit" command to end your script.
That's another thing you'll be expected to just "know" next
week.
When you are done creating your script, print it (see previous labs for the
print command), and submit lab02.txt and lab02.c to WebCT.
Grading
- Correctly scripting and submitting your work (following instructions, neatness
of printed script file, etc.): 30 pts
- Correctness and neatness of output from lab02.c: 30 pts
- Programming style (includes: choice of variables names, using #define where
appropriate, comments, proper use of whitespace (not too much, not too little),
clarity of code): 30 pts
- Updating web pages to conform to latest specs (a personal page that can
contain anything you like, plus a separate CISC105 page that points to the
"next" student's CISC105 page, accessible by a URL http://udel.edu/~userid/cisc105
(all lowercase cisc): 10 pts
Total: 100 pts
*** end of lab02 ***