Working with Multiple Files

  1. Calculate total resistance
    Implement a struct circuit that stores the resistances of three different resistors and a boolean value indicating whether the three resistors are in series or parallel. Declare struct circuit and a typedef to Circuit in circuit.h. Store the resistances using double precision floating point numbers.
    Create the following files: circuit.c, circuit.h, main.c, ui.c, ui.h and implement the functions below.
    /* main.c */
    // create at least two different circuits (one parallel and one serial)
    // and use both draw_circuit(.) and print_circuit(.) on each of them
    int main();

    /* circuit.c, circuit.h */
    // return the total resistance of the circuit
    double calc_circuit_resistance(Circuit c);

    /* in ui.c, ui.h */
    // read the resistances from stdin
    // also ask the user whether a parallel or series connection is used
    // return a new circuit struct containing the above data
    Circuit read_circuit();
    // print a visual representation of the struct; this can be something like
    // -[220Ω]-[330Ω]-[220Ω]- and
    //
    // ┌[220Ω]┐
    // ─┼[330Ω]┼─
    // └[220Ω]┘
    void draw_circuit(Circuit c);
    // print the values of the resistors, whether a parallel or series connection
    // is used and the total resistance
    void print_circuit(Circuit c);
    Declare all functions in the corresponding header files and include the header files wherever needed. Make sure to use #include guards in each of the header files.
  2. Inventory management system
    Your employer stores many different electric components (resistors, capacitors, ...). It is your duty to create an in-memory database to keep track of these components. Create the following files to contain your program: inventory.{c,h}, interface.{c,h}. Use the existing main.c and extend main(.) to use your new code.
    In inventory.h define a type Item. Each Item stores Create the following functions:
    /* inventory.c, inventory.h */
    // Return the total number of resistors in the inventory.
    // Eg. there are 100 resistors with 330 ohm, 50 with 5600 ohm and 10 with
    // 2 megaohm, the total number is 160.
    size_t count_resistors(Item items[], size_t count);

    // Return the total number of capacitors in the inventory.
    size_t count_capacitors(Item items[], size_t count);

    /* interface.c, interface.h */
    // ask the user to enter a new item on the command-line;
    // add the new item to the given list and return the count increased by one
    // (since there is one more element in the inventory array)
    size_t add_item(Item items[], size_t count);

    // print given item to stdout
    void print_item(Item i);

    // print all items, one per line
    void print_items(Item items[], size_t count);

    // command-line interface to create and manage an inventory
    // start a loop that asks the user which action they want to perform
    // `a` -> create a new item and add it to the inventory array
    // `c` -> show the number of capacitors
    // `e` -> exit the program
    // `i n` -> show the n-th item (0-indexed) of the inventory array
    // `h` -> show this help text
    // `l` -> show the list of items
    // `r` -> show the number of resistors
    void cli();
    In main(.) call cli(). Create an array of items of sufficient size inside of cli().
  3. Build systems
    1. In order to be able to compile the above tasks conveniently, write a GNU Makefile with the following high level targets:
      • all - compile everything
      • clean - clean up everything
      Make sure to only compile what really needs to be compiled when make is called. To do so, you will need to create additional targets for the object files.
    2. Once the GNU Makefile works properly, create a CMakeLists.txt that generates make files for you. Test building your project with cmake. Remember that cmake builds are done out of source.
    3. Finally, add a README.md file to this exercise and briefly explain how the project can be compiled. The markdown cheatsheet has all the information you need on the syntax to get started.