CISC105 lab02, Fall 2005
(sections 022-025, Instructor: P. Conrad)

Introduction

This lab is an introduction to project 1.

The main purpose of this lab is to get you ready to do project 1. In fact, when you finish this lab, you'll in one sense already half way done with project 1.

Learning objectives

By the time you finish this lab, you should be able to do all of the following:

  1. Be able to identify the right C-language "type" for storing various kinds of data (e.g. int, float, double, char array).
  2. Choose an appropriate length for a char array variable.
  3. Choose appropriate prompts for interacting with a user that types in data from the keyboard.
  4. Read a value into into a variable of type int, float, or double.
  5. Read some text (possibly with embedded spaces) into a char array.
  6. Print out variables of type int, float, double, and char array.
  7. Output a float or double as a dollar amount, appropriately formatted

Part 1: Some Example Programs for doing Input

This part of the lab does not involve anything that you have to turn in for credit. So, you could skip it, and neither your instructor nor your TA would have any way to know. Well, that's not entirely true.. we'd figure out it out pretty quickly if you were sort of clueless about all the things you could learn by actually doing it. So, I'd encourage you to go through all these steps anyway, even though there is nothing here that is going to be graded. :-)

Getting started

To get started, log on to strauss and do the following commands.

Note that in the last command, after the star, there is a space, then a period. That star, that space and that period are really important. The command tells strauss to "copy all the the files from a certain directory on pconrad's web site into my current directory". The period (by itself) means current directory. If you leave it off, you'll get an error message—possibly something about "permission denied" or "target not a directory" or some such thing.

cd ~/cisc105
mkdir lab02
cd lab02
mkdir inputExercises
cd inputExercises
cp ~pconrad/public_html/cisc105/05F/work/labs/lab02/inputExercises/* .

After doing these commands, if you do an ls command to list your files, you'll see something like the following:

> ls
Makefile inputExercise3.c inputExercise6.c
inputExercise1.c inputExercise4.c inputExercise7.c
inputExercise2.c inputExercise5.c inputExercise8.c
>

You can also see these files at the following web link

Using the make command

In this directory, you see lots of .c files, and also a special file called Makefile. Use "cat" or "more" to take a look inside that file for a moment:

cat Makefile
more Makefile

You'll see some comments in the file that tell you a little bit about it. However, all you really need to know about it for now is that it enables you to compile a whole bunch of programs with a single command:

make

Try typing that command. You should see something like the following:

> make
gcc -o inputExercise1 inputExercise1.c
gcc -o inputExercise2 inputExercise2.c
gcc -o inputExercise3 inputExercise3.c
gcc -o inputExercise4 inputExercise4.c
gcc -o inputExercise5 inputExercise5.c
gcc -o inputExercise6 inputExercise6.c
gcc -o inputExercise7 inputExercise7.c
gcc -o inputExercise8 inputExercise8.c
>

The system compiles each one of the .c programs and puts the executable code (the stuff that normally goes into a.out) into a separate file for each program:

You can change which compiler is used by changing the line in the Makefile that says CC=gcc to CC=cc (note the capital and small letters).

You can now run any of these programs by typing a dot and a slash, followed by the name of the program. For example, to run the program that corresponds to inputExercise1.c, type:

./inputExercise1

and to run the program that corresponds to inputExercise2.c, you type:

./inputExercise2

Ok, so what... why would I want to run these programs?

These programs are kind of interesting. Each of these programs asks the user for either three values:

  1. the number of pizzas the user wants to order (e.g. 3)
  2. the price of each pizza (e.g. 7.99)
  3. the topping the user wants on the pizzas (e.g. cheese, or mushroom)

Some of the programs also ask for a fourth value:

  1. the type of salad dressing the user wants (e.g. ranch).

And, as long as you use those values, the programs seem to work pretty well. All each program is supposed to do is read in the input, and then echo it back to the user to make sure that the program "got the input right". (After all, we can't really expect a program to do any calculations successfully unless we can read the input properly in the first place.)

Try running ./inputExercise1 for the values shown above, and see what happens. Hopefully, you get something like this:

> ./inputExercise1
Enter how many pizzas: 3
Enter price of Pizza in Dollars: 7.99
Enter pizza topping: cheese
How many pizzas : 3
Price in Dollars: 7.990000
Pizza Topping : cheese
>

However, now try running it again, and use a pizza topping that contains an "embedded space", for example green peppers. This time the result is not so good:

> ./inputExercise1
Enter how many pizzas: 3
Enter price of Pizza in Dollars: 7.99
Enter pizza topping: green peppers
How many pizzas : 3
Price in Dollars: 7.990000
Pizza Topping : green
>

Note that the program failed to read "peppers", and only read "green".

If you look at the source code for this program (inputExercise1.c) you can see that the pizza topping is read in using a scanf with a format string of %s. Unfortunately, this technique doesn't work when the string contains a space.

In inputExercise2 I've rearranged the order of the questions so you can see other things that can go wrong. Later versions of the program show how to get around this problem.

Your mission should you choose to accept it...

Your job is to look through all eight programs, and understand how they work. You'll need this understanding in order to complete project 1, which is due in two weeks, and is worth 10% of your final course grade. So, I hope you will accept this mission!

Try the programs out with various values. Especially interesting is to use values for the character data that contain spaces, such as green peppers for the pizza topping, and blue cheese for the salad dressing.

You'll see that the later versions of the program work a lot more effectively! Understand how each version of the program tries to build on the versions that came before. The comments in each file are there to help you.

What Prof Conrad and Prof. Harvey have found over the years of working with CISC105 students is that this problem of reading in input is often one of the most troublesome aspects of C programming. So they've written these programs to help you understand this topic better, and avoid some of the traps that past year's students have fallen into. Try them out, and learn from them.

Your instructor will go over these programs with you in lecture over the coming days, but you can learn quite a bit about them on your own just by reading the code, running the code, and by experimenting. Don't be afraid to make your own copies of these programs and try adding your own variables and your scanf(), printf() and fgets() function calls. That is the one the best things you can do to become a more skillful programmer works.

How to clean up after yourself

When you are all done, you can type the following command:

make clean

This will get rid of all the executable files. This is good, because they take up a lot of disk space, and you don't need to keep them around unless you are actively working with the programs in this directory. You can bring them back anytime you like by typing make again, so why keep around the executables?

Part 2: Describing a "table of data" related to your topic
(Filename: proj1data.txt, 50% of total points for this lab, 50% complete when done)

In this part of the lab, you are going to describe a table of data related to the "topic" that you chose in lecture. Rather than give a detailed description of what I'm looking for, it is probably more effective to just show you several examples first.

Some examples

Here are a few example tables for various topics. What you need in each case is at least two "fields" (i.e. two columns) that contain character data, and at least two that contain numeric data of some kind. It is best if you can come up with one "real number" and one "integer", but it isn't always possible (for example, I had trouble coming up with a "decimal" value for Supreme Court Justices.)

Pizzas with Side Salads (as in the inputExamples programs)

pizzaTopping numPizzas priceOfPizzaInDollars saladDressing
  cheese 2 4.99 ranch
mushroom 1 5.99 italian
green peppers 3 5.99 blue cheese
onions 1 4.99 thousand island

Hiking Trails

nameOfTrail park length state timesHiked
South Kaibab Grand Canyon NP 6.3 AZ 1
Hidden Pond Brandywine Creek SP 2 DE 4
Bright Angel Grand Canyon NP 9.3 AZ 1
Kelly's Run Holtwood Recreation Area 4 PA 1

Supreme Court Justices

Name Appointed by Year Appointed Year Born
Stephen G. Breyer Clinton 1994 1938
Ruth Bader Ginsburg Clinton 1993 1933
Clarence Thomas Bush (George H.W.) 1991 1948
David H. Souter Bush (George H.W.) 1990 1939
Anthony Kennedy Reagan 1988 1936
Antonin Scalia Reagan 1986 1936
Sandra Day O'Connor Reagan 1981 1930
John Paul Stevens Ford 1975 1920

Once you've come up with the "columns" for your table, and at least three sample "rows" for your table, you are ready to do the part that you are going to submit for credit. You should do the following commands.

(Note: If you are working on a PC, leave off the & on the emacs proj1data.txt command; include it only if you are working on a Sun Ray or other system with X Windows installed, such as a Linux system, and certain Macs and PCs with special software installed.)

mkdir -p ~/public_html/cisc105/proj1
cd ~/public_html/cisc105/proj1
emacs proj1data.txt &

Inside the file proj1data.txt, you want to document the columns in your table. Here are a couple of examples. The way I've formatted things here isn't the "only" way or the "right" way—this is a file written in English, not computer code, and it will be read by human beings, not a computer program. So format things however you like, as long as the way you write it is clear.

When you are finished writing up your proj1data.txt file, do the following sequence of commands to make it readable on the web:

chmod u+x ~
chmod a+rx ~/public_html
chmod -R a+rx ~/public_html/cisc105

Then, you should be able to see the file at the link below (if you substitute your username for "yourusername"). You should be able to click on it and open it in a web browser:

http://copland.udel.edu/~yourusername/cisc105/proj1

Your TA will grade it based on what is there after you've submitted the other part of your lab (the program lab02a.c), so don't submit that until this web page is also ready for grading.

 

P. Conrad, CISC105 Section 22, proj1data.txt

Topic: Hiking Trails


column 1: hiking trail name
type of data: character
maximum length: 20
can contain blanks: yes


column 2: location
type of data: character
maximum length: 30
can contain blanks: yes




column 3: lengthOfTrail
type of data: float
minimum value: 0
maximum value: 9999.9
units: miles
decimal places to show: 1
calculation: show length of trail in km as well.
decision: if trail is < 5 miles print short, otherwise print long.

column 4: state
type of data: character
maximum length: 2
can contain blanks: no
comments: I'll either avoid trails that are in multiple states,
or I'll divide up multi-state trails (such as the Appalachian Trail)
into segments, one per state.

column 5: timesHiked
type of data: int
minimum value: 0
maximum value: 9999

 

P. Conrad, CISC105 Section 22, proj1data.txt

Topic: Supreme Court Justices


column 1: justiceName
type of data: character
maximum length: 30
can contain blanks: yes


column 2: appointedBy
type of data: character
maximum length: 30
can contain blanks: yes

column 3: yearAppointed
type of data: int
minimum value: 1776
maximum value: 9999
units: years

column 4: yearBorn
type of data: int
minimum value: 0
maximum value: 9999
units: years


P. Conrad, CISC105 Section 22, proj1data.txt


 Topic: Digital Cameras
 column 1: brand
   type of data: character
   maximum length: 20
   can contain blanks: yes
 column 2: model
   type of data: character
   maximum length: 20
   can contain blanks: yes
 column 3: megapixels
   type of data: float
   minimum value: 0
   maximum value: 99.9
   units: megapixels
   decimal places to show: 1
 column 4: price
   type of data: double
   minimum value: 0
   maximum value: 9999.99
   units: dollars
   notes: print dollars and cents separately, separated by .

      

 

Part 3: Writing your own program to read in two of the "fields" in your table.
(Filename: lab02a.c, lab02a.txt , 50% of total points for this lab, 100% complete when done)

Now, based on the examples in the inputExamples directory that you looked through in part 1, you should be able to write a similar program that reads two fields out of your own table. For this lab, you only have to choose two of the columns, but one should be a character array, and the other one should be a numeric variable (int, float or double.) You'll do the remaining columns as part of project 1.

Start your program by doing the following commands. Two notes first:

cd
mkdir -p ~/cisc105/lab02
cd ~/cisc105/lab02
emacs lab02a.c &

Write a comment at the top of your program with your name, the date, CISC105 and your section number, and the filename lab02a.c.

Decide which two columns from your table you are going to input. Just like the inputExample programs, you are not (yet) going to input more than one row of your table; you'll choose one row, and that will be all you put in for a given run. Each row of the table will have to be a different run. Once you've decided which two columns of your table you are going to use, put a comment in your program indicating which two columns you are doing, e.g.

/* My columns from proj1data.txt are hikingTrailName (char) and numTimesHiked (int) */

Then put in the #include <stdio.h> and the int main(void) followed by an open and close brace { } , with a return 0; as the last statement. Now you are ready to start looking at the inputExample programs (1 through 8) and your proj1data.txt file, and figuring out what to put in so that you can read in two values and echo them back.

When you are finished, try compiling and running your program. Continue editing, compiling and running until your program works properly.

Finishing up and Submitting

To submit lab 2, you need to do each of the following:

  1. Make sure that your proj1data.txt file is readable on the web at the following URL. Note that you'll need to substitute your actual strauss username in place of "yourusername".

    http://copland.udel.edu/~yourusername/cisc105/proj1/proj1data.txt
  2. Upload file lab02a.c to WebCT for submission, but don't submit yet.
  3. Create a script, and call it lab02a.txt. In the script, do the following steps.

    1. Type cd ~/public_html/cisc105/proj1
    2. Type cat proj1data.txt
    3. Type cd ~/cisc105/lab02
    4. Type cat lab02a.c
    5. Compile lab02a.c with either cc or gcc (doesn't matter which)
    6. Run it several times (e.g. ./a.out or ./lab02a).

      You should not do just one run (i.e. don't just type ./a.out once, and then type in some values, and that's it). Do several runs (for this project, at least three, but no more than five) typing in different values for each run.

      In particular, show that your character fields can accept values with embedded spaces.
    7. Type exit to complete the script
  4. Upload your lab02a.txt file to WebCT and submit.
  5. Print your lab02a.txt using one of the following and give to your TA at the earliest possible date:

 


Copyright 2005, Phillip T. Conrad. Some material contributed by Terry Harvey, used by permission. Permission to copy all or any part of this assignment for non-commercial, non-profit, educational use granted to anyone, provided appropriate credit is given, and this copyright notice is maintained. All other rights reserved.