CS 31 Lab 2: C Warmup

Due Tuesday, February 6, before midnight


Contents


Goals


Working with Partners

This and all subsequent labs will be partnered assignments. We will assign partners for the first few labs, and then give you the opportunity to choose your partner.

You and your partner should work on all aspects of the project together: initial top-down design, incremental testing and debugging, and final stress testing and code review. There may be short periods of time where you each go off and implement some small part independently. However you should frequently come back together and talk through your changes. Partnerships where partners work mostly independently rarely work out well and rarely result in complete solutions. Partnerships where partners work side-by-side for all or most of the time tend to work out very well. Please make time in your schedule for your partner.

Treat your partner with respect and strive for an equitable partnership. By the end of the assignment you and your partner should each have a complete understanding of your joint solution.


Getting Starting Point Code

Both you and your partner should:

  1. On the CS system, cd into your cs31/labs subdirectory

    $ cd ~/cs31/labs
  2. Get your Lab2 ssh-URL from the GitHub server for our class: CS31-s18

  3. Clone a local copy of your shared repo in your private cs31/labs subdirectory:

    $ git clone [your_Lab2_URL]

You should have the following starter files in your repo:

  $ cd [your_Lab2_URL]
  $ ls
  Makefile       README.md   floats2.txt  floatsbig.txt  readfile.h
  QUESTIONNAIRE  floats.txt  floats3.txt  readfile.c     sorter.c

For more detailed instructions see the the Using git page. As you and your partner work on your joint solution, you will want to push and pull changes from the master repo into your local repo.


Assignment Details

Your job is to write a program, sorter.c, that sorts floating point numbers. Your program should: read some unsorted floats in from a file, store them in an array, print some information about them to the user, sort them using the algorithm of your choice, and print them out in sorted order.

Example run

Your program will take one command line argument: the name of an input file containing a bunch of floats.

  $ ./sorter floats.txt
  floats.txt contains 10 floats, ranging from 0.00 to 9.00
  The unsorted values are: 9.00 8.00 4.00 5.00 7.00 3.00 2.00 1.00 6.00 0.00 
  The sorted values are: 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00
  

File Format

Here is an example of a valid input file for this program:

    $ cat floats.txt
    10 0.0 9.0
    9.0
    8.0
    4.0
    5.0
    7.0
    3.0
    2.0
    1.0
    6.0
    0.0

The input file consists of a header line followed by one float on each line. The header contains the number of floats in the file (10) the minimum float (0.0) and the maximum float (9.0).

Lab 1 Starting Point Files

sorter.c is the only one of these files that you should modify.

File I/O

We'll read input files using functions from the provided readfile library. readfile.h contains function prototypes for the readfile library. There are function comments in this file that describe each function and a high-level comment that describes how to use the library.

Here are the general rules for how to use these functions:

  1. Open a file by calling open_file, passing in the name of the file to open as a string. The return value of open_file tells you whether or not the file was opened successfully--0 if the file is successfully opened, -1 if the file cannot be opened. You should always check the return value of this function and respond appropriately.

  2. Call the read_int, read_string, read_float functions to read values from the file into your program's variables, where the name of the function you call determines the resulting type of the return value. Like open_file, these functions return 0 on success, and you should always check their return value. If you've reached the end of the file, they will return -1.

    These functions take arguments much like scanf does: they need to know the address in memory where the value read in should be stored.

  3. Close the file when you're done with it with close_file.


Requirements

Your program's output should match the example output above. For full credit, your program should:


Tips

The following is the suggested way to implement the lab and fulfill the requirements:

More Tips


Submitting

There is a QUESTIONNAIRE file for you to fill out and submit with your lab solution.

Only one person in the partnership needs to git push your solution. It doesn't hurt if you both push, but the last pushed version before the due date is the one we will grade.

If git push fails, then there are likely local changes you haven't committed. Commit those first, then try pushing again:

$ git add sorter.c
$ git add QUESTIONNAIRE
$ git commit -m "Finished lab"
$ git push

The push will also fail if your partner pushed, but you have not pulled their changes. Do a git pull. Run make and test that your code still works. Then you can add, commit, and push. If this doesn't work, take a look at the "Troubleshooting" section of the Using git page. You may need to do a merge. After the merge, test that your code still works before submitting.