Lab materials
- Slides: Structures 2 + basic header
- Additional example: structure array with pointers (This is the most relevant for the lab task!)
- Additional example: returning a struct
- Additional example: structure in a structure
- Additional example: structure pointer as a structure member
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
1. Reading people file Parameter: pointer to the people struct array Parameter: maximum number of people that can be read (array len) Return: Number of people Note: you may want to add file name as the third parameter! 2. Reading vehicles file Parameter: pointer to the vehicles struct array Parameter: maximum number of vehicles that can be read (array len) Return: Number of vehicles Note: you may want to add file name as the third parameter! 3. Sorting people based on their name Parameter: pointer to the people struct array Parameter: number of people in the array Return: none 4. Displaying data Parameter: pointer to the people struct array Parameter: number of people in the array Parameter: pointer to the vehicles struct array Parameter: number of vehicles in the array Return: none |
Testing
This is the expected output from part 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
30610072188 Andres Siid (income: 0.00) car 1: 931NTM (tax: 319.00) 49112012007 Anne Saar (income: 42510.00) car 1: 994MBP (tax: 552.00) car 2: 552PND (tax: 391.00) car 3: 553PND (tax: 391.00) 68604152911 Dolores Vilipuu (income: 45000.00) car 1: 002XA (tax: 300.00) 44801231278 Jan Karu (income: 0.00) *** No registered cars *** 55506211816 Marko Rebane (income: 15000.00) car 1: VANITY1 (tax: 2500.00) car 2: 050AAC (tax: 925.00) 49011162694 Sten-Oliver Vallassaar (income: 20000.00) car 1: 001ABC (tax: 400.00) 60210031677 Toomas Luik (income: 30000.00) *** No registered cars *** |
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.
1 2 3 4 5 6 7 8 9 10 11 |
1. Calculating taxes Parameter: Vehicles structure array Parameter: Number of vehicles Parameter: People structure array Parameter: Number of people Return: none 2. Printing taxes and warnings Parameter: People structure array Parameter: Number of people Return: none |
Testing
This is the expected output from part 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
30610072188 Andres Siid (income: 0.00) car 1: 931NTM (tax: 319.00) 49112012007 Anne Saar (income: 42510.00) car 1: 994MBP (tax: 552.00) car 2: 552PND (tax: 391.00) car 3: 553PND (tax: 391.00) 68604152911 Dolores Vilipuu (income: 45000.00) car 1: 002XA (tax: 300.00) 44801231278 Jan Karu (income: 0.00) *** No registered cars *** 55506211816 Marko Rebane (income: 15000.00) car 1: VANITY1 (tax: 2500.00) car 2: 050AAC (tax: 925.00) 49011162694 Sten-Oliver Vallassaar (income: 20000.00) car 1: 001ABC (tax: 400.00) 60210031677 Toomas Luik (income: 30000.00) *** No registered cars *** Name: Andres Siid ------------------ Income: 0.00 EUR Tax: 319.00 EUR Tax pcnt: 0.00 % ERROR: Missing income Name: Anne Saar ---------------- Income: 42510.00 EUR Tax: 1334.00 EUR Tax pcnt: 3.14 % Name: Dolores Vilipuu ---------------------- Income: 45000.00 EUR Tax: 300.00 EUR Tax pcnt: 0.67 % Name: Jan Karu --------------- Income: 0.00 EUR Tax: 0.00 EUR Tax pcnt: 0.00 % ERROR: Missing income Name: Marko Rebane ------------------- Income: 15000.00 EUR Tax: 3425.00 EUR Tax pcnt: 22.83 % WARNING! Tax > 10% Name: Sten-Oliver Vallassaar ----------------------------- Income: 20000.00 EUR Tax: 400.00 EUR Tax pcnt: 2.00 % Name: Toomas Luik ------------------ Income: 30000.00 EUR Tax: 0.00 EUR Tax pcnt: 0.00 % |
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
- C Header Files
https://www.tutorialspoint.com/cprogramming/c_header_files.htm - ERD – Entity Relationship Diagram
https://www.visual-paradigm.com/guide/data-modeling/what-is-entity-relationship-diagram/ - Data models using UML class diagrams (alternative to ERD)
https://sparxsystems.com/resources/tutorials/uml/datamodel.html - Cardinality rules in data modelling
https://en.wikipedia.org/wiki/Cardinality_(data_modeling)