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:

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/*

 

Step 2: Look at the files 5airports.csv and airports.csv

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.

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.

  1. 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.
  2. In the main, we declare a FILE * variable
    • This is a pointer to a file
    • It allows is to read data from a file
  3. In the main, we declare an array of structs
    • We are going to read every line from an input file into this array
  4. 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
  5. 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.
  6. 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.
  7. 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.
  8. 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!
  9. 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.)

  1. Compile the readAirports.c program.
  2. 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
  3. 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.
  4. 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.

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

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:

 


Evaluation and Grading (300 pts total)

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.