CS16, Fall 2009

lab07: ("lab seven")
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.

This is a pair programming lab—with a new partner

We will be assigning new pair partners for this lab. This is required, not optional.

Why? For some folks, this will be no problem—others may find this distressing. If you fall into the latter group, here's a briefly explanation of why we are doing this:

Pair partner assignments will be posted on Gauchospace. Look on gauchospace.ucsb.edu under CS16 for the link to the pair assignments.

If there is a problem with your assigned pair partner such as the following, you may speak to your TA about an alternative assignment:

However, under no circumstances should you be assigned to the same person you worked with on lab05 or lab06.

Keep the evaluation criteria in mind

As you work with your pair partner, keep in mind that part of your grade will be determined by how he/she answers these questions. So try to be a good partner!

1. Did your partner read the lab assignment and preparatory materials before coming to the scheduled lab?
2. Did your partner do a fair share of the work?
3. Did your partner cooperatively follow the pair programming model
(rotating roles of driver and navigator)?
4. Did your partner make contributions to the completion of the lab assignment?
5. Did your partner cooperate?

Step by Step Instructions

Step 0: Get your new pair assignment from Gauchospace.

Or speak with your TA to get an alternate assignment (see above).

Step 1: Complete Homework Assignment H09

Since you are with a new partner, we'd like to ask you to revisit the basic ideas of pair programming together—with some new questions, now that you have some initial experience as pair programmers. So we have another assignment H09, that is similar to the one you did previously, though a little bit different.

We also want you along sending a new email like the one you sent earlier. See H09 for details.

Turn this in to your TA during your assigned lab section

You only need to turn in one copy per pair, but it is better if you BOTH fill it out, and BOTH turn it in.

Step 2: Decide whose account you are going to work in

Remember: don't share passwords. Instead, use scp or email to share files with each other at the end of each work session. (See lab05, step 2 for details of how to use scp.)

Ultimately, you should BOTH submit your work via the turnin program, just to be safe

Step 3: Log on to CSIL, create ~/cs16/lab07 and copy this weeks files:

If you've forgotten how to create the directory, consult steps 1 and 2 from lab01.

The files for this weeks lab can be found here:

And here:

~pconrad/public_html/cs16/09F/labs/lab07/files/*

You can use the same techniques described in lab04 to copy those into your ~/cs16/lab06 directory. Consult the instructions for lab04 if you don't remember how to do this.

Step 4: 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 5: 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 6: 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 7: 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/lab07 directory and should be called lab07.txt

To submit use the turnin command: turnin lab07@cs16 lab07

Once you've scripted and submitted, you are almost done, but not quite... Two more steps!

Step 8: Will this stuff be on the next midterm? Here's a guide

The midterm next week (11/18) will cover working with arrays of structs (since we've talked about those in both lecture and lab now).

The other new stuff from this lab will not be on the 2nd midterm, but on the final exam instead:

Step 9: An extra lab—lab07a—not for a grade, but to help you prepare for the next exam

There is some more material that you should work through to help you prepare for the midterm on 11/18.

This material is in lab07a:

http://www.cs.ucsb.edu/~pconrad/cs16/09F/labs/lab07a/

None of it is graded, or requires you to turn anything in—so it is on the "honor system" to do it.

But it will appears on the midterm—likely in the form of an essay question like the last question on the first midterm about test-driven development. This time the question will be about what it means for a file to be "executable".

So, I strongly encourage you to work through it as soon as possible.

Three notes:


Evaluation and Grading (300 pts total)

Due Date

You should try to complete this assignment by the end of your discussion section in which it was assigned, i.e. before 8:50am on Thursday 11/12/2009 or before 10:50am, 11:50am, or 12:50pm on Friday 11/13/2009 (depending on which section you are enrolled in.)

If you are unable to complete it by the end of your discussion section you may continue to work on it through the week. You should do your best to finish it before next week's lab, because there will be a new lab then—lab08—and you need to be ready to start working on that.

You should also try to complete this lab before the second midterm exam (scheduled for 1pm, Wednesday 11/18) if possible, since working with structs and arrays of structs will be on the second midterm (though the string functions and reading from files will not be.)

FYI, you will be working with the same pair partner next week for lab08.

Because there is a mid-term next week, I'll give you a few extra days—lab07 will be accepted without penalty through 9pm on Monday 11/23. After that, the TA will be free to begin grading.

Late assignments will only be accepted (with penalty) up until the time the TA doing the grading is finished with grading and posting the grades for this assignment. There is no specific guarantee as to the length of that period of time.

After that, a zero will be recorded, and the only option is to make up the points via extra credit.


Copyright 2009, 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.