CS157

PmWiki

edit SideBar

Homework 1: Command-Line Arguments and scientific data types

Description

Learning a programming language, such as C, is cumulative. You are expected to be fluent in the basic syntax presented in CS156 and now are ready to move on to advanced C language topics and more complex programs.

If you find yourself struggling with syntax on this assignment you need to attend office hours for help.

For this assignment you will be using command line arguments to read in the name of the file to process, read in from a file and perform some calculations.

Your program must run with one command-line argument: the name of the file to process. Your program must ensure that one and only one command-line argument is submitted to the program. If fewer, or more arguments are submitted, you must print an appropriate usage statement.

Data File

The data file you must process will have two fields per line: a country name, and the average life expectancy, in years, for people in that country. For example:

    Canada          80.34
    Chile           76.96
    Mexico          75.84
    Poland          75.19
    Dominica        75.1
    Latvia          71.6
    Egypt           71.57
    Nicaragua       70.92
    Iran            70.56
    Uganda          51.75
    Afghanistan     43.77
    Swaziland       32.23

Here’s another, less orderly but still valid, example:

    Uganda          51.75
    UK                            +78.7
    Ukraine         067.88
    Uruguay 7.593e+1
    USA 0.7806e002
    Uzbekistan      649.8e-1
    DeathLand 9

Your program must work on these files or other such files. It should print out the country name, and the life expectancy in years, days, and microseconds.

Sample Runs

    % ./a.out bogus
    ./a.out: can't open bogus

    % ./a.out udata
    NAME           YEARS      DAYS  MICROSECONDS
    -------------  -----  --------  ------------
    Uganda         51.75  18901.30     1.633e+15
    UK             78.70  28744.58     2.484e+15
    Ukraine        67.88  24792.66     2.142e+15
    Uruguay        75.93  27732.86     2.396e+15
    USA            78.06  28510.83     2.463e+15
    Uzbekistan     64.98  23733.46     2.051e+15
    DeathLand       9.00   3287.18     2.840e+14

For the purpose of this assignment, there are 365.2425 days in a year, and 24 hours in a day.

Requirements

  • Your error messages must include the name of the program, however we may have compiled it. Don't make the mistake of hard-coding in a.out into your message.
  • It is guaranteed that the country names will be only a single word.
  • The filename will be no more than 80 characters long.
  • No data file line will be more than 80 characters long.
  • No tab characters in the output.
  • Your output must be just like the sample:
    • It must line up.
    • The YEARS and DAYS columns must be in fixed-point format, with two digits after the decimal point.
    • The MICROSECONDS column must be in scientific notation, with three digits after the decimal point.

Required functions

There are only two required functions. You have to write them, and use them:

    double years_to_days(double y);
    double years_to_microseconds(double y);

Grading

Homework 1 is worth 10 points. You’ll lose points if you submit a file that:

  • doesn’t compile
  • doesn’t work
  • doesn’t do the required error-checking
  • isn’t named life.c
  • uses language features that we have not yet covered
  • changes any of the prompts or messages
  • does fopen without fclose
  • contains poor code:
    • cryptic variable names
    • missing/useless comments
    • extra { } for no reason
  • and many, many, more!

How to submit your homework:

Go to the same directory as the life.c file, and do this:

    ~cs157/bin/checkin HW1 life.c

How to check your answer after you turn it in:

    ~cs157/bin/peek HW1 life.c

How to receive negative points:

Turn in someone else’s work.
Edit - History - Print - Recent Changes - Search
Page last modified on November 05, 2008, at 11:11 AM