PR2EN4: Structures 2 and basic header

Lab materials

Lab tasks

For this lab, the base task is divided into two parts graded separately.

Lab task part 1: Print associated data

In part one, we’ll print the data we are reading from the data files. For this, we need to make sure to make the necessary associations.

Data files

The data is given to you in 2 files, described by an ERD (entity-relationship diagram.

Note: These kind of diagrams can also be shown as UML class diagrams. Check additional materials for a link.

ERD cardinality rules state that

  • A person may own 0 or more vehicles
  • Every vehicle must belong to exactly one person

The data is being associated by the Estonian ID code (person_id)

Download the data files from this link: https://blue.pri.ee/ttu/files/iax0584/andmefailid/4_data.zip

Requirements
  • Implement a header file. Store the struct declarations, macros and function prototypes in there. If necessary, also enums.
  • Read data from two files given to you. Store it in two separately declared structure arrays
    • Avoid more complex solutions such as substructures and pointers to structures during the lab task!
    • eID must be stored as a 64-bit integer in the structures. This will reduce the CPU clock cycles required for comparisons! Use inttypes.h !
  • Practice pointer arithmetic with struct arrays
    • Use the array (->) member-access  operator whenever working with the struct arrays (reading, writing, comparing)
    • Avoid square brackets []for indexing struct arrays
  • Sort the list of people alphabetically in ascending order by their first name.
  • Print out the list of vehicle owners, followed by all the vehicles they own
    • Write the owners information only once as a header
    • Follow this by printing all the vehicles they own
    • For people without any associated vehicles, write an appropriate message
Recommended functions

NB! The list here is just a recommendation. Your functions and their parameters can be different.

Testing

This is the expected output from part 1

Lab task part 2: Calculate taxes

Part 2 continues on part 1 with all the existing requirements staying in place and continuing through this part of the task.

Requirements
  • Find and print the taxation values for every person.
    • Print both the sum as a value and as a percentage to the annual income.
    • Write a warning if the tax percentage is 10% or greater than their annual income.
    • Write an error if there is no income
    • Remember! Make functions that only do one thing. Do not put calculations in the same function as reading the data or printing. Have a separate function that only does the tax calculations!
  • Hint: You should add additional member(s) to your structures to be able to store and display the required results!  Structure members don’t need match exactly to the data fields in the file, adding or even processing and storing it differently in memory is OK.
Recommended functions

NB! The list here is just a recommendation. Your functions and their parameters can be different.

Testing

This is the expected output from part 2

After this class, you should

  • Know how to create and include a header file to your code file
  • Know what should be written to the header file and what must not be written there
  • Know common use cases for headers
  • Know what’s the difference and how the addressing changes whether you are using quotes “” or angle brackets <> to include a header
  • Know why we need guards for the headers and how to implement them
  • Know how to embed debugging statements in your code using macros in two different ways
  • Know how to initialize structures
  • Know how to nest structures
  • Know how to return a structure
  • Know how and when to use the arrow operator to access structure members
  • Know how to use pointer arithmetic when working with structure arrays
  • Be able to read simpler ERD diagrams and know what they are used for modelling data
  • Know that there are class diagrams in UML that are similarly used to model data as ERD diagrams are.

Additional materials