{"id":7998,"date":"2023-01-17T23:29:05","date_gmt":"2023-01-17T21:29:05","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=7998"},"modified":"2026-02-09T08:14:28","modified_gmt":"2026-02-09T06:14:28","slug":"pr2en2-enumerations","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/","title":{"rendered":"PR2EN2: Enumerations"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Lab_materials\" >Lab materials<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Tasks\" >Tasks<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Lab_task_W02-1_File_categorization\" >Lab task [W02-1]: File categorization<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Categories_and_extensions\" >Categories and extensions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Template_for_the_task\" >Template for the task<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Recommended_steps_for_solving_the_task\" >Recommended steps for solving the task<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Hints_and_warnings\" >Hints and warnings:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Testing_manually_when_creating_the_program\" >Testing manually when creating the program<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Testing_correctness\" >Testing correctness<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Lab_task_2_W02-2_Distance_conversion\" >Lab task 2 [W02-2]: Distance conversion<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Requirements-2\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Data_files\" >Data files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Hints_and_tricks\" >Hints and tricks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Testing\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Extra_task_W02-3_comprehensive_converter\" >Extra task [W02-3]: comprehensive converter<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Requirements-3\" >Requirements<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#After_the_class_you_should\" >After the class, you should<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_materials\"><\/span><strong>Lab materials<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/slaidid-en\/02_enum.pdf\"><strong>Enumeration<\/strong><\/a><\/li>\n<li>Additional example: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/applying-enumeration-and-header-file-to-calculator\/\">calculator with enums<\/a><\/strong><\/li>\n<li>Additional example: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/programming-ii\/code-samples\/enumeration-example\/\">enumerations<\/a><\/strong><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Tasks\"><\/span><span id=\"Tasks\" class=\"ez-toc-section\"><\/span>Tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The lab has two tasks. These tasks cover a lot of topics from Programming 1, but are enriched with the enumeration topic introduced this week.\u00a0 Both tasks also benefit from the pointers covered last week.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_W02-1_File_categorization\"><\/span>Lab task [W02-1]: File categorization<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In this task, you will create a utility that will be able to count how many files of each category exist (e.g. how many image files in a directory and its subdirectories).\u00a0 We will only create the part that categorizes files and counts the totals.<\/p>\n<p>To find the names of files, we will use knowledge from the Linux task last semester. We will use a tool called <span class=\"lang:c highlight:0 decode:true crayon-inline \">find<\/span>\u00a0 to find the files recursively and pipe them to our program. By doing this, we will be able to index and categorize any amount of files recursively in all subdirectories.<\/p>\n<p><strong>Note: To demonstrate the potential of combining programs you need have access to a bash environment. Easiest way is to test in the university environment (use the lab computer, remote desktop or create an SSH tunnel to one of our servers or lab computers).<\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Create a program that<\/p>\n<ul>\n<li>Accepts an unknown number of file names from the standard input (<span class=\"lang:c highlight:0 decode:true crayon-inline \">stdin<\/span>\u00a0).\n<ul>\n<li>You are not allowed to preemptively ask for number of inputs or have a designated string to stop reading inputs.<\/li>\n<li>To stop reading the strings and show the statistics, listen for the <span class=\"lang:c highlight:0 decode:true crayon-inline \">EOF<\/span>\u00a0 (end of file) signal.<\/li>\n<\/ul>\n<\/li>\n<li>Categorizes those files to groups based on the identifiable extensions and counts how many files in each category.<\/li>\n<li>Display how many files were in each category.<\/li>\n<li>Categories must be identified as enumeration type in code.<\/li>\n<li>One of the functions you need to have is specified for you. It needs to\u00a0 take the file extension as a parameter and returns the enum of the category. Proposed function prototype:<br \/>\n<span class=\"lang:default decode:true crayon-inline\">enum FileCategory GetFileCategory(char *extension);<\/span><\/li>\n<li>Your program can provide a prompt when started (i.e. instructions), but must not write anything to the output in between inputs.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Categories_and_extensions\"><\/span>Categories and extensions<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>This is a list of categories that you are expected to test against. The results of files using these extensions is also given as a test set.<\/p>\n<ul>\n<li>Archives: zip, rar, 7z, tar, gz<\/li>\n<li>Data: csv, xls, xlsx, ods<\/li>\n<li>Documents: pdf, doc, docx, rtf, odt<\/li>\n<li>Code: c, h, cpp, hpp, py<\/li>\n<li>Text: txt<\/li>\n<li>Images: jpg, jpeg, png, svg,<\/li>\n<li>Other: all files with extensions, but not in the previously listed types<\/li>\n<li>No extension<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Template_for_the_task\"><\/span>Template for the task<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>In order to give you a bit better idea on the expected structure and how the reading and processing would work, you are provided a template to base your task on.<\/p>\n<pre class=\"toolbar:2 lang:default decode:true \">#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\n\/\/ Maximum file name length in modern systems\r\n#define MAX_FILE_LEN 256\r\n\r\n\/\/ TODO: Define the number of categories for array declaration \r\n\/\/ (alternatively, use the enum count method instead)\r\n\r\n\r\n\/\/ TODO: Add enum declaration\r\n\r\n\r\nint main(void)\r\n{\r\n    \/\/ Stores the name of the file\r\n    char name[MAX_FILE_LEN];\r\n    \r\n    \/\/ TODO: Declare the array to hold a counter for each file category\r\n    \r\n\r\n    \/\/ Read in the name of a file with the extension until EOF occurs\r\n    while (fgets(name, MAX_FILE_LEN, stdin) != NULL)\r\n    {\r\n        \/\/ TODO: Call a function to fix the trailing newline\r\n\r\n        \r\n        \/\/ TODO: Call a function to find the position of the last point\r\n        \/\/ Hint: think of what no point symbol in the string means!\r\n\r\n        \r\n        \/\/ TODO: Call a function that returns the category type and store it.\r\n        \/\/ This is the predefined function from the requirements!\r\n\r\n\r\n        \/\/ TODO: Increase the appropriate category counter value\r\n    }\r\n    \r\n    \/\/ TODO: Call a function to print the results\r\n    \r\n    \r\n    return EXIT_SUCCESS;\r\n}\r\n<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Recommended_steps_for_solving_the_task\"><\/span>Recommended steps for solving the task<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ol>\n<li>Add a function that will fix the trailing newline in the read string<br \/>\ne.g.. <span class=\"lang:c decode:true crayon-inline\">void FixTrailingNewline(char *str);<\/span><\/li>\n<li>Add a function that will find the location of the <strong>last<\/strong> point (<span class=\"lang:c highlight:0 decode:true crayon-inline \">.<\/span>\u00a0) symbol in the string to identify the start of the file extension.<br \/>\ne.g. <span class=\"lang:c decode:true crayon-inline \">int GetLastPointPos(char *str);<\/span><\/li>\n<li>Add the category enumeration to your code and create an initialized array of counters for the categories.<\/li>\n<li>Add a function to print the array of counters (result)<br \/>\ne.g. <span class=\"lang:c decode:true crayon-inline \">void PrintFileCntPerCategory(int *categoryCounters, int numOfCategories);<\/span><\/li>\n<li>Add a function that will, based on the given extension, find the category of the file.<br \/>\n<strong>NB! Look at the flying object identification code example for inspiration!<\/strong><br \/>\ne.g.\u00a0 <span class=\"lang:c decode:true crayon-inline \">enum FileCategory GetFileCategory(char *extension);<\/span><\/li>\n<\/ol>\n<h5><span class=\"ez-toc-section\" id=\"Hints_and_warnings\"><\/span>Hints and warnings:<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Check out the additional enum example on this page. It is based on a similar categorization task, it will offer quite a few ideas on code structure.<\/li>\n<li>You should recognize various subtasks from last semester &#8211; i.e. parts of the first and second strings lab task and age classifier home work.<\/li>\n<li>If you add a count item after the last enumerated item, it will tell you the number of items in the list. This will only work if you allow it to automatically number all items!\n<pre class=\"toolbar:2 lang:default decode:true\">enum FileCategory \r\n{\r\n    CAT_DATA,\r\n    CAT_DOCUMENTS,\r\n    CAT_IMAGES, \r\n    CAT_CODE, \r\n    CAT_ARCHIVE, \r\n    CAT_TEXT, \r\n    CAT_OTHER, \r\n    CAT_NO_EXT, \r\n    CAT_COUNT\r\n};<\/pre>\n<p>This allows you to automatically declare the correct length array for counters.<\/li>\n<li>By using what you learned about pointers last week (i.e. pointer arithmetic),\u00a0 you can use the location of the point symbol as an offset to calculate the address where the extension starts. That new address would also be pointing at a string.<\/li>\n<li>The length of the reading loop is of unknown length. <span class=\"lang:default highlight:0 decode:true crayon-inline \">fgets()<\/span>\u00a0 returns <span class=\"lang:default highlight:0 decode:true crayon-inline\">NULL<\/span>\u00a0 when <span class=\"lang:default highlight:0 decode:true crayon-inline \">EOF<\/span>\u00a0 (end of file, indicating no more inputs) is reached.<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">fgets()<\/span>\u00a0 stores the trailing newline character which needs to be corrected for.<\/li>\n<li>Input for your program comes from a pipe to your programs <span class=\"lang:default highlight:0 decode:true crayon-inline\">stdin<\/span><\/li>\n<li>To quickly test without the command line, you can hit <span class=\"lang:default highlight:0 decode:true crayon-inline \">ctrl+d<\/span>\u00a0 to send the <span class=\"lang:default highlight:0 decode:true crayon-inline \">EOF<\/span>\u00a0 signal<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing_manually_when_creating_the_program\"><\/span>Testing manually when creating the program<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>To test manually, we can run the program normally, type in the names of files, pressing enter after each file name. Once done, hit <span class=\"lang:default highlight:0 decode:true crayon-inline \">ctrl+d<\/span>\u00a0 to send the EOF (end of file) signal.<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto.heinsar@lx9:~\/P&gt; .\/task1_category\r\na.zip\r\nb.zip\r\na.png\r\n\r\n    Archives        2\r\n  Code files        0\r\n        Data        0\r\n   Documents        0\r\n      Images        1\r\n  Text files        0\r\n       Other        0\r\nNo extension        0<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Testing_correctness\"><\/span><strong>Testing correctness<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>To test the correctness, we will index a folder that I have prepared for you. If you are on the lab computer, you can access it directly over the network. If you are using your own computer, download the archive provided and change the path for <span class=\"lang:c highlight:0 decode:true crayon-inline \">find<\/span>\u00a0. Your numbers for each category should match the ones presented in this example.<\/p>\n<p>We use a tool called <span class=\"lang:c highlight:0 decode:true crayon-inline \">find<\/span>\u00a0 to search for files and folders and limit it to only show files and print without the path. First we show the location where we are searching in, then we specify to only show files (omit folders) and we then print the names of files without the path. The output of find (from <span class=\"lang:c highlight:0 decode:true crayon-inline \">stdout<\/span>\u00a0, standard output stream) will be <strong>piped<\/strong> into our programs <span class=\"lang:c highlight:0 decode:true crayon-inline \">stdin<\/span>\u00a0 (standard input stream).<\/p>\n<p>Command executed: <span class=\"lang:default highlight:0 decode:true crayon-inline\">find ~\/M\/risto.heinsar\/lab_cat\/ -type f -printf &#8216;%f\\n&#8217; | .\/task1_category<\/span><\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false lang:default highlight:0 decode:true\">risto.heinsar@lx9:~\/P&gt; find ~\/M\/risto.heinsar\/lab_cat\/ -type f -printf '%f\\n' | .\/task1_category\r\n    Archives        2\r\n  Code files        7\r\n        Data        2\r\n   Documents        3\r\n      Images        0\r\n  Text files        6\r\n       Other        1\r\nNo extension        5<\/pre>\n<p>Hint: if you&#8217;re curious, you can also test your own P drive and add extensions and\/or categories.<\/p>\n<div class=\"su-spoiler su-spoiler-style-fancy su-spoiler-icon-chevron su-spoiler-closed\" data-scroll-offset=\"0\" data-anchor-in-url=\"no\"><div class=\"su-spoiler-title\" tabindex=\"0\" role=\"button\"><span class=\"su-spoiler-icon\"><\/span>Backup of the test files<\/div><div class=\"su-spoiler-content su-u-clearfix su-u-trim\">\n<p>Note: if you are unable to demonstrate the correctness due to networking issues or want to test on your own device, you can can demonstrate the correctness by using the following the archived version of the directory structure.<\/p>\n<p><strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/andmefailid\/2_1_file_cat_directory_structure.zip\">https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/andmefailid\/2_1_file_cat_directory_structure.zip<\/a><\/strong><\/p>\n<p>The structure is the same as on the M drive.<\/p>\n<\/div><\/div>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_2_W02-2_Distance_conversion\"><\/span>Lab task 2 [W02-2]: Distance conversion<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>You have been provided activity data from a group of employees in an international company. The data files are provided after the requirements section. Your task is to convert the data in the files to the desired output units, show the results of the conversion and provide basic statistics.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-2\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Program takes two command line arguments\n<ul>\n<li>First argument is the name of the input file<\/li>\n<li>Second argument is the desired output unit of distance (available options: <span class=\"lang:default highlight:0 decode:true crayon-inline \">m<\/span>\u00a0 for meters, <span class=\"lang:default highlight:0 decode:true crayon-inline \">ft<\/span>\u00a0 for feet and <span class=\"lang:default highlight:0 decode:true crayon-inline \">km<\/span>\u00a0 for kilometers)<\/li>\n<\/ul>\n<\/li>\n<li>Input file is a basic ASCII text file (first command line argument).\n<ul>\n<li>Each line in the file contains one entry<\/li>\n<li>Each entry consist of two fields, separated by a space: <span class=\"lang:default highlight:0 decode:true crayon-inline\">&lt;distance&gt; &lt;unit&gt;<\/span><\/li>\n<li>Distances are given as real numbers<\/li>\n<li>Units are given as strings. Units in the input file can only be in feet or meters.<\/li>\n<\/ul>\n<\/li>\n<li>Calculate and display all distances, converted to the desired output unit<\/li>\n<li>Calculate and output the average and total distances walked.<\/li>\n<li>All distances are shown with 2 places after the comma.<\/li>\n<li>Units must be handled using enums. Recommended list is provided:\n<pre class=\"toolbar:2 lang:default decode:true \">enum Unit {UNIT_M, UNIT_FT, UNIT_KM, UNIT_UNKNOWN};<\/pre>\n<\/li>\n<li>Conversion coefficients are also provided\n<pre class=\"toolbar:2 lang:default decode:true\">#define FT_IN_M 0.3048f\r\n#define FT_IN_MI 5280.00f\r\n#define M_IN_KM 1000.00f<\/pre>\n<\/li>\n<li>Look at hints for getting ideas how the functions might look like for more optimum code division.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Data_files\"><\/span>Data files<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>There are 3 files provided for you to test your program with. Look under the paragraph <strong><a href=\"#Testing\">Testing<\/a> <\/strong>for what you should look out for when testing with each of the files!<\/p>\n<p>Download the test files: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/andmefailid\/2_2_converter_data.zip\">https:\/\/blue.pri.ee\/ttu\/files\/iax0584\/andmefailid\/2_2_converter_data.zip<\/a><\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Hints_and_tricks\"><\/span>Hints and tricks<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<h6>Hint 1: Conversion function<\/h6>\n<p>Remember to not overcomplicate your functions. Your convert function should only handle conversion from one unit type to another. Do not try to decode the string into a unit in that function.\u00a0 Just pass it the decoded enum types and the value that needs converison.<\/p>\n<p>E.g. some ideas for the prototype of that function<\/p>\n<pre class=\"lang:c decode:true \">\/\/ Conversion using doubles\r\ndouble GetConvertedDistance(double value, enum Unit inputUnit, enum Unit destUnit);\r\n\r\n\/\/ Conversion using floats\r\nfloat GetConvertedDistance(float value, enum Unit inputUnit, enum Unit destUnit);<\/pre>\n<h6>Hint 2: Decoding the unit<\/h6>\n<p>To decode both the input and the output units, use the same function &#8211; both are strings with just the unit. It should be a function that would return the unit type.<\/p>\n<p>E.g.\u00a0\u00a0<span class=\"lang:c decode:true crayon-inline\">enum Unit GetDistanceUnitType(char *unit);<\/span><\/p>\n<h6>Hint 3: Printing the units<\/h6>\n<p>There are a lot of units in play. Printing the correct one can be a bit tricky. There are two ideas to help you with:<\/p>\n<p><strong>Option 1:<\/strong> Create a function to print the unit and call it when you need it. Call it whenever you need to print the correct unit according to the task.<\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">void PrintUnit(enum Unit unitType)\r\n{\r\n    switch (unitType)\r\n    {\r\n        case UNIT_FT:\r\n            printf(\"ft\");\r\n            break;\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Option 2:<\/strong> Create a function that will return you a pointer to a string containing the unit.\u00a0 Since it is written as a constant, it will be available in the memory after the function returns. It makes this really convenient to use it in print statements &#8211; e.g. <span class=\"lang:default decode:true crayon-inline\">printf(&#8220;%.2f %s\\n, distance, ReturnPrintableUnit(unit));<\/span><\/p>\n<pre class=\"toolbar:2 lang:default decode:true\">char * ReturnPrintableUnit(enum Unit unitType)\r\n{\r\n    switch (unitType)\r\n    {\r\n        case UNIT_FT:\r\n            return \"ft\";\r\n    }\r\n}\r\n<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Testing\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>This program has a lot of ways it can go wrong. Make sure to test for all constraints!<\/p>\n<h6>Test 1 &#8211; 3: Invalid arguments<\/h6>\n<p>This test actually is comprised of 3 different tests, but all of them have wrong arguments passed to the program.<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false nums-toggle:false lang:default highlight:0 decode:true\">risto@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 \r\nInvalid arguments!\r\nUsage: .\/task2 data_file output_unit\r\nristo@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 data.txt\r\nInvalid arguments!\r\nUsage: .\/task2 data_file output_unit\r\nristo@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 data.txt m ft\r\nInvalid arguments!\r\nUsage: .\/task2 data_file output_unit\r\n<\/pre>\n<h6>Test 4, 5: Problematic arguments<\/h6>\n<p>The next two tests are about parsing the arguments themselves and making sure that both the file exist and the unit is within the allowed list.<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false nums-toggle:false lang:default highlight:0 decode:true\">risto@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 data.txt mm\r\nError: Unknown unit!\r\nristo@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 data.txt m\r\nError opening input file: No such file or directory\r\n<\/pre>\n<h6>Test 6: empty file<\/h6>\n<p>The purpose of this test is to make sure that our program does not crash when there is no data to process.<\/p>\n<p>task2_data1.txt contents:<\/p>\n<pre class=\"lang:default highlight:0 decode:true\"><\/pre>\n<p>And the results for this data file:\u00a0<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false nums-toggle:false lang:default highlight:0 decode:true\">risto@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 task2_data1.txt m\r\nTotal: 0.00 m\r\n<\/pre>\n<h6>Test 7 &#8211; 9: Conversion tests<\/h6>\n<p>In these tests we will go over all of the possible input and output unit conversions. We use a simple data file that allows us to easily observe if our answers are correct.<\/p>\n<p>task2_data2.txt contents:<\/p>\n<pre class=\"lang:default highlight:0 decode:true\">500 m\r\n500 ft<\/pre>\n<p>And the results for this data file:<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false nums-toggle:false lang:default highlight:0 decode:true\">risto@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 task2_data2.txt ft\r\n500.00 m -&gt; 1640.42 ft\r\n500.00 ft -&gt; 500.00 ft\r\n\r\nTotal: 2140.42 ft\r\nAverage: 1070.21 ft\r\nristo@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 task2_data2.txt m\r\n500.00 m -&gt; 500.00 m\r\n500.00 ft -&gt; 152.40 m\r\n\r\nTotal: 652.40 m\r\nAverage: 326.20 m\r\nristo@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 task2_data2.txt km\r\n500.00 m -&gt; 0.50 km\r\n500.00 ft -&gt; 0.15 km\r\n\r\nTotal: 0.65 km\r\nAverage: 0.33 km\r\n<\/pre>\n<h6>Test 10: Different file<\/h6>\n<p>The emphasis for this test is to test your program with a different data file &#8211; just different length and units to make sure that nothing got passed us by.<\/p>\n<p>task2_data3.txt contents:<\/p>\n<pre class=\"lang:default highlight:0 decode:true\">1000 m\r\n5400 m\r\n12010 ft\r\n850 m\r\n4626 m\r\n9603 ft\r\n11215 ft\r\n10242 m\r\n<\/pre>\n<p>And the results for this data file:<\/p>\n<pre class=\"theme:cisco-router toolbar:2 nums:false nums-toggle:false lang:default highlight:0 decode:true\">risto@risto-wk-tux:~\/Nextcloud\/work\/ttu\/teaching\/_generic\/prog2\/lab\/wk1_enum$ .\/task2 task2_data3.txt km\r\n1000.00 m -&gt; 1.00 km\r\n5400.00 m -&gt; 5.40 km\r\n12010.00 ft -&gt; 3.66 km\r\n850.00 m -&gt; 0.85 km\r\n4626.00 m -&gt; 4.63 km\r\n9603.00 ft -&gt; 2.93 km\r\n11215.00 ft -&gt; 3.42 km\r\n10242.00 m -&gt; 10.24 km\r\n\r\nTotal: 32.12 km\r\nAverage: 4.02 km\r\n<\/pre>\n<p><span style=\"color: #ff0000;\"><strong>Sidenote: did you notice what we didn&#8217;t test for, but cold also be improtant?<\/strong><\/span><\/p>\n<h4><span class=\"ez-toc-section\" id=\"Extra_task_W02-3_comprehensive_converter\"><\/span>Extra task [W02-3]: comprehensive converter<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The extra task is based on lab task 2 and must be an extension of the base task. Disregard the concept of &#8220;walking&#8221; and consider the task as just a distance converter with statistics.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements-3\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Add support for additional distance units\n<ul>\n<li>Yard (yd)<\/li>\n<li>Inch (in)<\/li>\n<li>Decimeter (dm)<\/li>\n<\/ul>\n<\/li>\n<li>You must allow all 6 units to be both inputs and outputs for the program.<\/li>\n<li>Design the conversions in a expandable fashion so that if we would add additional units, it wouldn&#8217;t require large overhauls of the code. The complexity of adding another unit must not expand the codebase exponentially!<\/li>\n<\/ul>\n<p>Warning! Even though the expected\u00a0 method for conversion is simple to implement and manage, it may increase the error of the final result due to rounding of the conversion coefficients outside of metric system. Be careful with tasks requiring high precision!<\/p>\n<h3><span class=\"ez-toc-section\" id=\"After_the_class_you_should\"><\/span><span id=\"After_the_class_you_should\" class=\"ez-toc-section\"><\/span><strong>After the class, you should<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Have gone over many of the topics covered last semester, including command line arguments, parsing strings, reading from files etc<\/li>\n<li>Be able to work with enumerations\n<ul>\n<li>Declaring new enum types<\/li>\n<li>Know how the auto-numeration for enums works<\/li>\n<li>Declaring variables based on enum types<\/li>\n<li>Pass enums to functions, return enums from functions<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span><span id=\"Additional_content\" class=\"ez-toc-section\"><\/span><strong>Additional content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Note: most sites explaining enumerations can&#8217;t even follow the same coding style on a single page! Use the style guide provided by us!<\/p>\n<ul>\n<li>BeeJ&#8217;s guide to Enums<br \/>\n<strong><a href=\"https:\/\/beej.us\/guide\/bgc\/html\/split\/enumerated-types-enum.html#enumerated-types-enum\">https:\/\/beej.us\/guide\/bgc\/html\/split\/enumerated-types-enum.html#enumerated-types-enum<\/a><\/strong><\/li>\n<li>Enumeration (or enum) in C<br \/>\n<strong><a href=\"https:\/\/www.geeksforgeeks.org\/enumeration-enum-c\/\">https:\/\/www.geeksforgeeks.org\/enumeration-enum-c\/<\/a><\/strong><\/li>\n<li>Enumerations<br \/>\n<strong><a href=\"https:\/\/en.cppreference.com\/w\/c\/language\/enum\">https:\/\/en.cppreference.com\/w\/c\/language\/enum<\/a><\/strong><\/li>\n<li>C enums<br \/>\n<strong><a href=\"https:\/\/www.programiz.com\/c-programming\/c-enumeration\">https:\/\/www.programiz.com\/c-programming\/c-enumeration<\/a><\/strong><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab materials Slides: Enumeration Additional example: calculator with enums Additional example: enumerations Tasks The lab has two tasks. These tasks cover a lot of topics from Programming 1, but are enriched with the enumeration topic introduced this week.\u00a0 Both tasks also benefit from the pointers covered last week. Lab task [W02-1]: File categorization In this &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr2_en\/pr2en2-enumerations\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">PR2EN2: Enumerations<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105],"tags":[],"class_list":["post-7998","post","type-post","status-publish","format-standard","hentry","category-pr2_en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/7998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=7998"}],"version-history":[{"count":28,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/7998\/revisions"}],"predecessor-version":[{"id":11209,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/7998\/revisions\/11209"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=7998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=7998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=7998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}