Project 0: Intro project

This project serves as an introduction to developing and compiling C programs, as well as to the build, testing, and submission framework used in this class. The actual code you must write to receive credit for this project is trivial, allowing you to focus on the mechanics of compiling C programs and completing a project in this course. A more complete description of the project framework is contained in the project guide, which you should read in full before completing P1.

Note that this project also contains a variety of optional requirements, provided as a courtesy to help you exercise your knowledge as you learn C. Because the optional parts are not graded, you may share code with other students (although we recommend that you do not share full solutions because you will derive maximum benefit from writing them on your own). You may also post on Piazza or come to office hours for help solving these challenges.

Before proceding, you should first c02 the compilation lab.


Regular Requirements

These sections describe what you must do to receive credit for "completing" P0. Recall that we have provided many additional optional requirements, which are described in another section below.

Unit Requirements

Here is the function that you must implement in p0-intro.c:

  • int add_abs (int num1, int num2)

    Returns the sum of the absolute values of the two parameters.

There are two unit tests for this function; one is public and you can view the source code in tests/public.c, while the other is private, meaning you do not have access to its source code. This will be the case for all projects in this course--it is important that you learn to anticipate edge cases and other errant behavior.

Integration Requirements

When run, the program should print the text "Hello, world!" followed by a newline character ("\n"). This will require you to edit the code in main.c. There is a single integration test for this functionality.

Testing

For the required portion of this project, there are only three tests total. To run these tests, use the "make test" command from the project folder. At first, your output will look like this:

========================================
             UNIT TESTS
15%: Checks: 26, Failures: 22, Errors: 0
public.c:12:F:Public:C_addabs_simple:0: Assertion 'add_abs(2,3) == 5' failed [...]
Private:B_addabs_negative_ints:0: Assertion 'add_abs(2,-4) == 6' failed [...]
========================================
          INTEGRATION TESTS
A_hello                        FAIL (see outputs/A_hello.diff for details)
No memory leak found.
========================================

The test case names have been highlighted in the output above. After you finish the project, the output should look like this:

========================================
             UNIT TESTS
23%: Checks: 26, Failures: 20, Errors: 0
========================================
          INTEGRATION TESTS
A_hello                        pass
No memory leak found.
========================================

You can ignore the remaining "failures" for now; they are the tests for the optional parts of this project (see below). The important changes are that the two unit tests (C_addabs_simple and B_addabs_negative_ints) are now passing, as is the single integration test (A_hello). As long as you are passing all three of those tests, you will receive an "A" on this assignment.

Submission

Due: Fri, Sep 1 at 23:59:59 ET (midnight)

You must submit your files from stu. To submit, run the following command from your project directory:

/cs/students/cs261/f17/<INST_EID>/submit.sh p0

Make sure you substitute your instructor's e-ID ("lam2mo" or "weikleda") for "INST_EID" before running the command. Please see the project guide for general project help. Please refer to the coding standards for coding practice guidelines.


Optional Requirements

To enable output for the optional tests, run "make testall" from the main project directory instead of "make test". All optional test cases have a label beginning with an "X_" to indicate that they are extra.

These extra test cases are not graded; they are provided as additional practice in C programming and are intended to be completed before or while you are working on P1. Some of the functionality (specifically, the unit test requirements) must be implemented in functions in p0-intro.c and some functionality (the integration test requirements) must be implemented in main.c.

Note that in this project, the "integration" tests don't really test for successful integration of smaller program units; they test entirely separate functionality. This will not be the case in the rest of the projects.

Optional Unit Requirements

Here are a list of additional functionality that we have provided unit tests for as well as initial function stubs for you to complete. You will find these stubs in p0-intro.c, and that is where you should put all of the code to implement those functions.

  • int factorial (int num);

    Return the factorial of non-negative inputs; otherwise, returns 1.
  • bool is_prime (int num);

    Return true if the input is a positive prime number, false otherwise.
  • int gcd (int num1, int num2);

    Return the greatest common divisor (gcd) of the two inputs, or zero if either of the inputs is zero.
  • void add_ptr (int num1, int num2, int *ans);

    Add two numbers and store the results in ans.
  • int sum_array (int *nums, size_t n);

    Return the sum of all numbers in an array of length n.
  • void sort_array (int *nums, size_t n);

    Sort an array of length n. You may use any sorting algorithm you wish; we recommend keeping it simple.
  • vector_t add_vec (vector_t v1, vector_t v2);

    Add two vectors. (See p0-intro.h for the definition of a vector_t)
  • double dot_prod_vec (vector_t v1, vector_t v2);

    Return the dot product of two vectors.
  • int eval_str (char *expr);

    Evaluate a simple arithmetic expression in the form of a string. The expression must consist of an initial number (positive or negative) followed by a non-recursive series of addition ("+"), subtraction ("-"), or multiplication ("*") operators and accompanying operands (positive or negative integers). Return zero for any ill-formed inputs.

Optional Integration Requirements

In addition to the functions above, there are some optional P0 challenges that will require you to add new functionality that is activated using command-line switches. You should implement these features as modes in main.c. Some of these modes require parameters, which are passed as command-line arguments. We recommend using getopt to handle command-line parsing.

Multiple modes may be active at once, and if any mode is activated, the original "Hello, world!" message should not print. If multiple modes are activated, their output should be printed in the order they are listed below. If a mode with a parameter is activated multiple times (via multiple command-line switches), only the last parameter should be used.

Here is a list of modes:

  • Goodbye mode ("-g")

    Print "Goodbye!"
  • Triodd mode ("-t <n>")

    Count from one to n (one number per line), printing "odd" if the number is odd, "tri" if the number is divisible by three, and "triodd" if the number is both. Otherwise, just print the number itself.
  • Cat mode ("-c <file>")

    Print each line from the given file to standard out, similar to the Unix cat utility.
  • Uniq mode ("-u <file>")

    Print each line from the given file to standard out, but remove sequential duplicates. In other words, don't print the same line twice in a row.
  • Hexdump mode ("-h <file>")

    Dump all data from the given file to standard out, first as individual bytes (in hex) and then as ASCII characters. In the latter mode, any non-alphanumeric character should be printed as a period (".").