{"id":6286,"date":"2021-09-23T18:27:28","date_gmt":"2021-09-23T16:27:28","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=6286"},"modified":"2023-10-03T22:26:55","modified_gmt":"2023-10-03T20:26:55","slug":"homework-age-grouping","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/","title":{"rendered":"Homework: age grouping"},"content":{"rendered":"<p>Your task is to create a program that will analyze the ages of guests for an event. As input, you will get an unknown number of age values until the user enters 0 as the age. At that point the input will stop and the output statistics will be printed.<\/p>\n<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\/maaratlemata-en\/homework-age-grouping\/#Grouping_requirements\" >Grouping requirements:<\/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\/maaratlemata-en\/homework-age-grouping\/#Data_structure\" >Data structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/#Program_requirements\" >Program requirements:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/#Required_functions\" >Required functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/#Program_structure\" >Program structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/#Algorithm\" >Algorithm<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/#Sample\" >Sample<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Grouping_requirements\"><\/span><strong>Grouping requirements:<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Everyone below 18 is a minor<\/li>\n<li>Ages 18 and beyond will be put grouped with a step of 5 years (e.g. 18 &#8211; 22, 23 &#8211; 27 etc.)<\/li>\n<li>Everyone whose age is above the previously defined groups will be grouped as elders.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Data_structure\"><\/span>Data structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The counters for each group will be created as an array, where each array member is for one of the predefined groups. The size of the array is defined with the macro <span class=\"lang:default decode:true crayon-inline\">GROUPS<\/span> .<\/p>\n<ul>\n<li>Index <span class=\"lang:default decode:true crayon-inline \">GROUPS &#8211; 1<\/span>\u00a0 is for elders<\/li>\n<li>Index <span class=\"lang:default decode:true crayon-inline \">GROUPS &#8211; 2<\/span>\u00a0 is for minors<\/li>\n<li>Indexes from 0 to <span class=\"lang:default decode:true crayon-inline \">GROUPS &#8211; 3<\/span>\u00a0 are used for the rest of the age groups.<br \/>\nI.e. index 0 is for ages 18 &#8211; 22; index 1 is for 23 &#8211; 27 etc.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Program_requirements\"><\/span>Program requirements:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Grouping settings must be easily configurable by only adjusting macros.\n<ul>\n<li>Group count<\/li>\n<li>Group stepping<\/li>\n<li>Upper bound for minor group<\/li>\n<\/ul>\n<\/li>\n<li>The rest of the code must automatically adjust itself if any of the macros were altered.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Required_functions\"><\/span>Required functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For this task, you should create at least 3 functions<\/p>\n<ul>\n<li>To validate that the user input is within allowed and reasonable bounds<\/li>\n<li>To find the group index that the age belongs to. Group index must be returned by this function and stored into <span class=\"lang:c highlight:0 decode:true crayon-inline\">idx<\/span>\u00a0 variable<\/li>\n<li>To print the results<\/li>\n<\/ul>\n<p>You can also make a 4th function to make the program even nicer by moving the infinite loop from main into a separate function handling the processing.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Program_structure\"><\/span>Program structure<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We are giving you the generic structure for the program with the macros already in place. You need to use them in the final code.<\/p>\n<p>The places where you should call your functions (the ones listed by &#8220;required functions&#8221; section)\u00a0 are highlighted as comments.<\/p>\n<pre class=\"toolbar:1 lang:c decode:true\">#include &lt;stdio.h&gt;\r\n\r\n\/* Number of age groups in total *\/ \r\n#define GROUPS 11\r\n\/* Array position for minors group *\/\r\n#define GROUP_MINOR (GROUPS - 2)\r\n\/* Array position for elders group *\/\r\n#define GROUP_ELDER (GROUPS - 1)\r\n\/* Group size*\/\r\n#define GROUP_STEP 5\r\n\/* Upper bound for minors *\/\r\n#define MINOR_UPPER_BOUND 17\r\n\/* Calculates lower bound for elders *\/\r\n#define ELDER_LOWER_BOUND (MINOR_UPPER_BOUND + (GROUP_STEP * (GROUPS - 2)))\r\n\r\n\r\nint main(void)\r\n{\r\n    int ageGroups[GROUPS] = {0};\r\n    int idx;\r\n    \r\n    \/* Infinite loop for input*\/\r\n    while (1)  \r\n    {\r\n        \/* Grab user input (call your function!) *\/\r\n        \r\n\r\n        \/* Check for break condition (age 0)*\/\r\n\r\n        \r\n        \/* Determine the index (call your function!) *\/\r\n        \/* idx = YourFunctionName()*\/\r\n        \r\n        \r\n        \/* Add to the age group counter *\/\r\n        ageGroups[idx]++;\r\n    }\r\n\r\n    \/* Pint the results (Call your function!) *\/\r\n        \r\n    return 0;\r\n}\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Algorithm\"><\/span>Algorithm<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>We have modeled most of the algorithm for you that you need to implement. Some of the calculations we have left for you to figure out &#8211; e.g. how to find the end points for age ranges in the output and a little bit of the array indexing as well.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-8933 size-full\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en.png\" alt=\"\" width=\"1329\" height=\"1017\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en.png 1329w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en-300x230.png 300w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en-1024x784.png 1024w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2021\/09\/nd5_kodu_vanuselise_jaotamise_algo_en-768x588.png 768w\" sizes=\"auto, (max-width: 1329px) 100vw, 1329px\" \/><\/a><\/p>\n<h3><span class=\"ez-toc-section\" id=\"Sample\"><\/span>Sample<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Here is a sample of the program and how it behaves. You can also see a debugging output &#8211; it is useful to sanity check yourself. You can print out even more data just to be sure. In this case it helps to identify which counter (array slot) was increased. In the final version you do not need to include the counter, but for development it is highly recommended.<\/p>\n<p>NB! When testing that your code works, try to also change the GROUPS and GROUP_STEP macros and see if everything still calculates correctly.<\/p>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\" title=\"Sample: Age classifier test\">This program categorizes participants of the event into age brackets!\r\nTo stop entry and show statistics, enter 0 or a negative number at any point!\r\n\r\nEnter participants age: 1\r\nDEBUG: idx = 9\r\nEnter participants age: 15\r\nDEBUG: idx = 9\r\nEnter participants age: 17\r\nDEBUG: idx = 9\r\nEnter participants age: 18\r\nDEBUG: idx = 0\r\nEnter participants age: 22\r\nDEBUG: idx = 0\r\nEnter participants age: 23\r\nDEBUG: idx = 1\r\nEnter participants age: 45\r\nDEBUG: idx = 5\r\nEnter participants age: 62\r\nDEBUG: idx = 8\r\nEnter participants age: 63\r\nDEBUG: idx = 10\r\nEnter participants age: 99\r\nDEBUG: idx = 10\r\nEnter participants age: 0\r\n\r\nMinors: 3\r\nAges 18 - 22: 2\r\nAges 23 - 27: 1\r\nAges 28 - 32: 0\r\nAges 33 - 37: 0\r\nAges 38 - 42: 0\r\nAges 43 - 47: 1\r\nAges 48 - 52: 0\r\nAges 53 - 57: 0\r\nAges 58 - 62: 1\r\nElders: 2<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Your task is to create a program that will analyze the ages of guests for an event. As input, you will get an unknown number of age values until the user enters 0 as the age. At that point the input will stop and the output statistics will be printed. Grouping requirements: Everyone below 18 &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/maaratlemata-en\/homework-age-grouping\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Homework: age grouping<\/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":[10],"tags":[],"class_list":["post-6286","post","type-post","status-publish","format-standard","hentry","category-maaratlemata-en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6286","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=6286"}],"version-history":[{"count":5,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6286\/revisions"}],"predecessor-version":[{"id":8920,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/6286\/revisions\/8920"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=6286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=6286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=6286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}