{"id":5556,"date":"2020-10-26T01:06:14","date_gmt":"2020-10-25T23:06:14","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=5556"},"modified":"2021-10-21T21:04:14","modified_gmt":"2021-10-21T19:04:14","slug":"9-lab-functions","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/","title":{"rendered":"8. lab: functions"},"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-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Lab_content\" >Lab content<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Lab_tasks\" >Lab tasks<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Task_1_finding_results_from_an_array\" >Task 1: finding results from an array<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#1_function_reading_an_integer\" >1. function: reading an integer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#2_function_filling_an_array\" >2. function: filling an array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#3_function_printing_an_array\" >3. function: printing an array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#4_function_arithmetic_mean\" >4. function: arithmetic mean<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#5_function_minimum_value\" >5. function: minimum value<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#6_function_check_if_value_in_array\" >6. function: check if value in array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Advanced_multiples_of_n_to_a_new_array\" >Advanced:\u00a0multiples of n to a new array<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Task_2_Converting_an_existing_code_to_use_functions\" >Task 2: Converting an existing code to use functions<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Advanced_weight_limit\" >Advanced: weight limit<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#After_the_class_you_should\" >After the class, you should<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Lab_content\"><\/span><strong>Lab content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Slides:\u00a0<strong><a href=\"https:\/\/1drv.ms\/p\/s!Aqu9MAlG9GE2uRmf6h3-vTjTlk2f?e=dfKccG\">functions<\/a><\/strong><\/li>\n<li>Additional example on functions:\u00a0<strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/dist_converter.c\">dist_converter.c<\/a><\/strong><\/li>\n<li>Basecode:<strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/warehouse_kontaktope.c\">\u00a0warehouse.c<\/a><\/strong><\/li>\n<\/ul>\n<h2><span class=\"ez-toc-section\" id=\"Lab_tasks\"><\/span>Lab tasks<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Task_1_finding_results_from_an_array\"><\/span>Task 1: finding results from an array<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The purpose of this task will be to create a program, that:<\/p>\n<ul>\n<li>Reads 5 integers from the user that will be in between -100 and 100.<\/li>\n<li>Prints the entered numbers out.<\/li>\n<li>Finds and prints the arithmetic mean with 2 places after the comma.<\/li>\n<li>Finds and prints the smallest member in the array.<\/li>\n<li>Allows the user to enter a number and checks if it exists in the array or not.<\/li>\n<\/ul>\n<p>The task has the following limitations (in addition to previous practices):<\/p>\n<ul>\n<li>All of the functions described below will have to be implemented and used to find the results.<\/li>\n<li>Constants defined using macros must be passed as a function parameter and not directly used in the functions.<\/li>\n<li>If the function&#8217;s purpose is to find a results (e.g. arithmetic mean, smallest number, &#8230;), the result must be returned to main(). Side effects (printing the results within the functions) is not allowed.<\/li>\n<\/ul>\n<p><strong>Solve the program step-by-step. Test each function after creating it before proceeding. Avoid writing the code without functions at first!<\/strong> The functions given below are in the order we recommend writing them.<\/p>\n<p>We have written a simple writeup on the parameters for each functions.\u00a0 If you wish, you can add additional parameters to the second function (read array), but nothing else!<\/p>\n<p>The writeup on the functions is simplified and allows the developer to make some decisions on their own. It is not enough to be used as a sample for homework 2!<\/p>\n<h4><span class=\"ez-toc-section\" id=\"1_function_reading_an_integer\"><\/span>1. function: reading an integer<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> Reads one integer from the user, that is checked to be in the allowed range. The function must not return before the entered value is within the allowed range. If the user entered number wasn&#8217;t between the allowed minimum and maximum, the user will be warned and prompted again.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer &#8211; minimum allowed value.<\/li>\n<li>integer &#8211; maximum allowed value.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> integer, that is between the allowed minimum and maximum.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"2_function_filling_an_array\"><\/span>2. function: filling an array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> This functions reads n integers and stores them into the array. To use each integer, the previously completed function (reading an integer) is called. It is done repeatedly in a look for each array member that needs to be read.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array &#8211; the array that will be filled.<\/li>\n<li>integer &#8211; array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> none.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"3_function_printing_an_array\"><\/span>3. function: printing an array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> Function prints values stored in the array.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array &#8211; the array that will be printed.<\/li>\n<li>integer &#8211; array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> none.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"4_function_arithmetic_mean\"><\/span>4. function: arithmetic mean<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> Function finds the arithmetic mean from the array and returns it.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array &#8211; array, that contains the members of which the average will be calculated.<\/li>\n<li>integer &#8211; array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> float &#8211; arithmetic mean.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"5_function_minimum_value\"><\/span>5. function: minimum value<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> Function finds the smallest member of the array and returns it.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array &#8211; values from where the minimum value will be searched for.<\/li>\n<li>integer &#8211; array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> integer &#8211; smallest number in the array.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"6_function_check_if_value_in_array\"><\/span>6. function: check if value in array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Description:<\/strong> Function looks if the entered value is within the array or not. The result will be coded as an integer and returned.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array &#8211; values from were the search key is being looked for.<\/li>\n<li>integer &#8211; array length.<\/li>\n<li>integer &#8211; the value that is being looked for.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> integer \/ boolean &#8211; was the entered number present in the array or not.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Advanced_multiples_of_n_to_a_new_array\"><\/span>Advanced:\u00a0multiples of n to a new array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>User enters a multiplier (positive integer)<\/li>\n<li>Create a function that will create the new array\n<ul>\n<li>Values where the absolute value is a multiple of the entered multiplier will be added to the new array. New array members must also be absolute values.<\/li>\n<li>Function itself must not print out the new array nor call a function to do so.<\/li>\n<\/ul>\n<\/li>\n<li>The newly created array must be printed out. Use a function that already exists.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Task_2_Converting_an_existing_code_to_use_functions\"><\/span>Task 2: Converting an existing code to use functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Take the following code and split it into functions:<br \/>\n<a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/warehouse_kontaktope.c\">https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/warehouse_kontaktope.c<\/a><\/p>\n<p>Create the following functions, move the code from main to those functions:<\/p>\n<ul>\n<li>Array print (without return, code reuse)<\/li>\n<li>Print message about how much will fit (without return)<\/li>\n<li>Sorting (without return)<\/li>\n<li>Combining in sorting facility (returns number of items moved to the third array)<br \/>\n<i>Note: This function can work with code reuse (called twice<\/i><i>), but<\/i><i> doesn\u2019t have to!<\/i><\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Advanced_weight_limit\"><\/span>Advanced: weight limit<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Create a limit for the cargo exiting the sorting facility.<\/p>\n<p>E.g. a truck can deliver up to 1500 kg at once. Fill it with heavier items first. If some items don\u2019t fit the limit, try if a lighter item will fit.<\/p>\n<p>Print out the items that did fit on the truck and then fit separately the items that were left in the sorting facility.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"After_the_class_you_should\"><\/span><span id=\"After_the_class,_you_should_be_able_to\" class=\"ez-toc-section\"><\/span><strong>After the class, you should<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<ul>\n<li>Understand what a function is<\/li>\n<li>Understand that we have been using functions from the first week. Both functions that return values and those that don&#8217;t!<\/li>\n<li>Understand, that we can create functions just like those we have been using.<\/li>\n<li>Understand the following terms\n<ul>\n<li>Function prototype<\/li>\n<li>Function return type<\/li>\n<li>Function arguments<\/li>\n<li>Function parameters<\/li>\n<li>Function header<\/li>\n<li>Function body<\/li>\n<\/ul>\n<\/li>\n<li>Be able to create proper functions<\/li>\n<li>Be able to pass variables and arrays to functions<\/li>\n<li>Be able to store the value returned from a function<\/li>\n<li>Be able to reuse functions with different arguments<\/li>\n<li>Be able to divide your code to functions<\/li>\n<li><strong>Have a small list of universal functions that you can reuse in future codes!<\/strong><\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span><strong>Additional content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li><a href=\"https:\/\/www.tutorialspoint.com\/computer_programming\/computer_programming_functions.htm\">https:\/\/www.tutorialspoint.com\/computer_programming\/computer_programming_functions.htm<\/a><\/li>\n<li><a href=\"https:\/\/www.studytonight.com\/c\/user-defined-functions-in-c.php\">https:\/\/www.studytonight.com\/c\/user-defined-functions-in-c.php<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab content Slides:\u00a0functions Additional example on functions:\u00a0dist_converter.c Basecode:\u00a0warehouse.c Lab tasks Task 1: finding results from an array The purpose of this task will be to create a program, that: Reads 5 integers from the user that will be in between -100 and 100. Prints the entered numbers out. Finds and prints the arithmetic mean with &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/9-lab-functions\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">8. lab: functions<\/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":[75,77],"tags":[],"class_list":["post-5556","post","type-post","status-publish","format-standard","hentry","category-labs","category-pr1_en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5556","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=5556"}],"version-history":[{"count":0,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5556\/revisions"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=5556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=5556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=5556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}