CS16, Winter 2009
el00: ("extra lab zero")
Extra Credit Lab:
Reading from files, arrays of structs
Goals for this lab
By the time you have completed this lab, you should:
- Be able to read lines from a file into an array of structs
- Work with that array of structs to find various values
Prior Skills/Knowledge Needed
Before completing this lab, you should have completed the labs up through lab06. In particular, you should be comfortable working with both arrays and structs separately. In this lab, we'll combine the two concepts.
For this lab, you may use pair programming
or you may work alone.
Step by Step Instructions
Step 1: Log on to CSIL, create ~/cs16/el00 and copy this labs files:
The files for this lab can be found here:
And here:
~pconrad/public_html/cs16/10W/extraLabs/el00/files/*
Use the "more" command to look first at the file 5airports.csv, then at airports.csv (you may need the "q" command in more or CTRL/C to exit since airports.csv is kind of long!)
These files contain latitude and longitude information for various airports.
- As the name suggests, 5airports.csv contains information on only five airports
- airports.csv contains information on a large number of airports.
The first line in each file gives us some clues as to what the files contain:
Code,Lat,Lon,City,State
The file is in "CSV" format, i.e. "comma separated values".
We can get an idea of how long the airports.csv file is using the command wc -l airports.csv
This command allows us to see how many lines there are in the file ('wc stands for word count, but when we pass the -l flag it counts lines instead of words):
-bash-3.2$ wc -l airports.csv
1218 airports.csv
-bash-3.2$
We can see that if we want to make an array big enough to store all of these airports, it will need to have at least 1217 elements in it.
Step 3: Look at the file readAirports.c
Next, look through the file readAirports.c—open it up in the text editor (i.e. emacs).
This is a fairly long program, and some parts of it present new concepts that may be unfamiliar at first.
Here are a few of the new things you'll encounter. Try to find each one, and look over it with your pair programming partner. See if you can figure out what the code is doing.
- The struct definition is in a separate header file: airport.h
- Take a look at that file
- There are a few new things in there—the #ifndef stuff, for example—but don't worry about that for now.
- Just look at the struct Airport definition
- We put it in a separate file because we might reuse it in other programs.
- In the main, we declare a
FILE *
variable
- This is a pointer to a file
- It allows is to read data from a file
- In the main, we declare an array of structs
- We are going to read every line from an input file into this array
- Inside the function initAirportFromString, we see calls to strtok
- strtok stands for "string tokenize"—this means to pull it apart into its pieces
- In this case, we are pulling apart a string such as
"LAX,33.93,118.4,LosAngeles,CA
"
and turning it into separate strings, i.e. "LAX"
, "33"
, "93"
, "118.4"
, "LosAngeles"
, and "CA"
.
- Notice that "," is a parameter to each call—this is because our data is separated by commas
- Inside the function initAirportFromString, we see calls to strncpy
- The strncpy function is used to copy a string
- strncpy allows us to copy the contents of the string, not just the pointer value
- The n in strncpy refers to the third argument, which is the maximum number of characters to be copied.
- In the main program, we find a call to strcmp—the string compare function
- This function allows us to see whether two strings are equal
- If the strings are equal, strcmp returns a value of 0 (think: there is 0 difference between them.)
- We use strcmp to check whether the name of the file passed in is the special value: "test"
- If argv[1] is "test", instead of opening a file, we run some tests, and quit.
- The test function runTests() also uses strcmp
- Notice that we use our test-driven development approach to check whether the function initAirportFromString did its job properly.
- The main program has code to open a file with fopen, and read data from it
- The fgets() function (file get string) is used to read lines from the file
- We combine this with a while loop that tests feof (end of file) and ferror (file error) each time through the loop to see whether to continue into the loop—in case the fgets call didn't return any data.
- Note that feof is only true AFTER you try to read and find nothing there.
- We only continue into the loop if both feof and ferror are NOT true—i.e. our attempt to read was successful and we have some data to process!
- After reading the data into the array, we use the array to look for the easternmost airport.
- You get to add code for finding the westernmost, northernmost, and southernmost airports.
Once you've looked over all of that, you are ready to start coding yourself!
Step 4: What you need to do—adding some tests, and some new code
You job is to add a few tests, and then add some extra code at the end of the main function. (That extra code will also require you to add three functions.)
- Compile the readAirports.c program.
- Run it with the command:
./readAirports test
- You should see that five tests fail
- Find the "stub" line in the function
initAirportFromString()
and comment it out
- For now, don't remove it entirely—we'll do that at a later step
- Recompile and run again with
./readAirports test
- All five tests should pass
- Next, find the function runTests()
- Inside this function, find the comment where it indicates you should add five tests
- Add those tests
- Then, bring the stub back in
initAirportFromString()
i.e.
the line that just says return;
as the first line of the function
- this prevents the function from doing any useful work
- You should see 10 tests fail
- Then take the stub back out—permanently—and all the tests should pass.
- Finally, look at the end of the main function
- There is a place there where the program prints out the easternmost airport
- Your job is to add code to also print out the westernmost, southernmost and northernmost airport
- In the process of doing that, you'll need to create three new functions:
- indexOfAirportWithLargestLongitude
- indexOfAirportWithSmallestLatitude
- indexOfAirportWithLargestLatitude
- Once you've written the new functions, and the new code in the main, you can test this code by running the program first with
./readAirports 5airports.csv
- You should see that San Diego is the easternmost airport—look at a map if you don't believe it!
- Similarly, by simple inspection of the data, you should be able to determine which of the airports (SBA, SAN, SFO, LAX, SMF) is the southernmost, northernmost, and westernmost.
- Then run your program with
./readAirports airports.csv
to see if the answers make sense (i.e. you'd expect the northernmost airport to be in Alaska, the easternmost to be in Maine, etc.)
When you've done all that, you are ready to submit!
Step 5: Script and submit
Before scripting be sure to look through the grading rubric to make sure you've done everything possible to maximize your grade on the assignment.
- You might put the rubric in one window, and have the code open in another window.
- One pair partner can read each item on the rubric out loud, and keep track of where you are in the rubric.
- The other pair partner can scroll through the code to bring up the parts of the program it refers to.
- Both partners can be responsible for looking to see that that item on the rubric is satisfied.
Script your assignment just as in previous weeks. Consult lab04 if you need a refresher on the process. Remember to script each of your programs.
Your script file should be located in your ~/cs16/el00 directory and should be called el00.txt
To submit use the turnin command: turnin el00@cs16a el00
Note: when you submit this, it is el00, which is:
lowercase E, lowercase L, zero zero
I think there have been some confusion about whether the l was a one or an L, and whether the 00 were letter Os, or zeros.
So, again: el00 is lower case E, lower case L, zero zero
- As a reminder: to submit your assignment, you need to be in the ~/cs16 directory—one level higher than the previous step (use
cd ..
)
- More details on scripting can be found in lab04
- More details on using turnin can be found in lab00 through lab02.
Once you've scripted and submitted, you are almost done, but not quite... Two more steps!
Step 8: Will this stuff be on the final exam? Here's a guide
The final exam for Winter 2010 will cover:
- working with arrays of structs.
- strings functions: strncpy, strcmp, strtok
- reading from files (FILE *, fgets, feof, ferror)
Evaluation and Grading (300 pts total)
- Mechanics 75 pts:
- 5 points: successfully submitting the contents of your el00 directory
- 5 points: el00 directory contains required C programs and el00.txt (a transcript file)
- 30 points: submission is on time and follows submission instructions (i.e. is done via turnin by the Due Date.)
- 10 points: each C file contains a header comment on first line with name "cs16 el00" and date and name(s) of students submitting the work.
- 10 points: all @@@ comments are removed in your C files
- 15 points: transcript file contains everything asked for in the instructions
- Programming Tasks
(points for correctness and style)
- 75 points—adding tests in
runTests as indicated by the comment
- Add tests and remove the @@@ comments
- The results of the tests should be added into the 'failure' variable
- The tests should fail when there is a stub in
initAirportFromString()
, and should pass when the stub is replaced with working code.
- 50 points—code for printing western most point
- This code should call a new function
indexOfAirportWithLargestLongitude()
to do its calculation
- 50 points—similar code for printing northernmost point
- It too should call a new function
- 50 points—similar code for priming southernmost point
Due Date
To earn full 300 pts extra credit: You must submit this via turnin on CSIL by midnight on Friday March 12.
To earn half credit (i.e. 1/2 of the points you would have earned) in extra credit: submit by midnight on Sunday March 14.
After that, it is still worth doing as part of your studying for the final exam, but no extra credit is available.
Copyright 2010, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.