{"id":7388,"date":"2022-09-25T18:24:15","date_gmt":"2022-09-25T16:24:15","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=7388"},"modified":"2022-10-04T14:29:59","modified_gmt":"2022-10-04T12:29:59","slug":"6-lab-functions","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/","title":{"rendered":"6. 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-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Lab_material\" >Lab material<\/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\/6-lab-functions\/#Lab_tasks\" >Lab 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\/6-lab-functions\/#Task_1_Electricity_price_calculator\" >Task 1: Electricity price calculator<\/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\/6-lab-functions\/#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\/6-lab-functions\/#Recommendations_and_hints\" >Recommendations and hints<\/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\/6-lab-functions\/#Testing\" >Testing<\/a><ul class='ez-toc-list-level-6' ><li class='ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Test_1_no_errors\" >Test 1: no errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Test_2_invalid_input_tests\" >Test 2: invalid input tests<\/a><\/li><\/ul><\/li><\/ul><\/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\/6-lab-functions\/#Task_2_finding_results_from_an_array\" >Task 2: finding results from an array<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Requirements-2\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Code_general_structure\" >Code general structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Step-by-step_guide_to_solve_the_task\" >Step-by-step guide to solve the task<\/a><ul class='ez-toc-list-level-6' ><li class='ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#_%E2%9E%A1_Function_reading_a_single_integer\" >Function: reading a single integer<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#_%E2%9E%A1_Function_filling_the_array\" >Function: filling the array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#_%E2%9E%A1_Function_printing_the_array\" >Function: printing the array<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#_%E2%9E%A1_Function_arithmetic_mean\" >Function: arithmetic mean<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#_%E2%9E%A1_Function_minimum_value\" >Function: minimum value<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Testing-2\" >Testing<\/a><ul class='ez-toc-list-level-6' ><li class='ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Test_1_no_errors-2\" >Test 1: no errors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-6'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Test_2_errors_in_input\" >Test 2: errors in input<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Advanced_task_1_Check_if_value_in_array\" >Advanced task 1: Check if value in array<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Function_description\" >Function description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Testing-3\" >Testing<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Advanced_task_2_multiples_of_n_to_the_new_array\" >Advanced task 2: multiples of n to the 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-25\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-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'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_material\"><\/span><strong>Lab material<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/livettu-my.sharepoint.com\/:p:\/g\/personal\/risto_heinsar_ttu_ee\/EdzpQ6Vqz5RFrsdjJG99IPMBo2f4iUc0T8p2tprqXyFIgA?e=adc0Gq\"><strong>functions<\/strong><\/a><\/li>\n<li>Additional example: <strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/dist_converter.c\">dist_converter.c<\/a><\/strong><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Lab_tasks\"><\/span>Lab tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For this lab, you have 2 tasks. There is also an advanced task, which extends the features of lab task #2.<\/p>\n<p>In the first task, you need to write the declarative part of functions in a pre-written program. In this task, you will only be working with variables.<\/p>\n<p>In the second task we focus on arrays and you have to write the entire program.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Task_1_Electricity_price_calculator\"><\/span>Task 1: Electricity price calculator<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In this task, we have written most of the program already. This includes the entirety of <span class=\"lang:default highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function.<\/p>\n<p>You will need to fill in the missing parts in the functions for this program. In the base code, all of the returns from the functions have been written as <span class=\"lang:default highlight:0 decode:true crayon-inline \">return 0<\/span> . This is done so that the program would compile and you could test it step-by-step. You need to replace this in every function based on the description in the function comment, right above the function itself.<\/p>\n<p><strong>Download the starter code from here: <a class=\"item file\" href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/aluskoodid\/6_1_electricity_basecode.c\">6_1_electricity_basecode.c<\/a><\/strong><\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Start by introducing yourself to the base code. Your task must be built on it.<\/li>\n<li>Your task is to write te declarative part of seven functions. The purpose of the function is described right before it as a comment.<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">main()<\/span>\u00a0 function and the structure of the code cannot be changed. It&#8217;s recommended to also avoid renaming the functions and variables.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Recommendations_and_hints\"><\/span>Recommendations and hints<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Start by going through the code. Look at where all the parts of the code are &#8211; libraries, macros, prototypes, main function and the rest of the user-defined functions. Check out how main calls the user-defined functions, what is given as parameters etc. Do not change anything right now!<\/li>\n<li>Once familiar, start by writing the function bodies (declarative part of the function).\u00a0<strong>Solve 1 function at a time, compile and test if it works!<\/strong> Do not move forward before it works!<\/li>\n<li>The recommended structure for the input functions is a do while loop, which has an if statement inside to print an error for wrong input.<\/li>\n<li>All other functions are solvable by just writing one line. You need to replace the\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline \">return 0<\/span>\u00a0 with the correct formula.<\/li>\n<li>Careful with units! There is a mix of megawatw-hours, kilowatw-hours and watt-hours! Check the function comment for which it is.<\/li>\n<li>VAT &#8211;\u00a0<em>value added tax<\/em>, 20% in Estonia<\/li>\n<li>Current market price in MWh before taxes:\u00a0 <strong><a href=\"https:\/\/dashboard.elering.ee\/et\">https:\/\/dashboard.elering.ee\/et<\/a><\/strong><\/li>\n<li>Current market price per kWh in cents, including taxes:\u00a0 <strong><a href=\"https:\/\/www.elektrikell.ee\">https:\/\/www.elektrikell.ee<\/a><\/strong><\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Testing\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<h6><span class=\"ez-toc-section\" id=\"Test_1_no_errors\"><\/span><strong>Test 1: no errors<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\">Enter the market price for electricity in MWh: 412.65\r\n\r\nMarket cost of electcity is 412.65 EUR \/ MWh.\r\nThis is 0.4126 EUR per kWh before and 0.4952 EUR per kWh after taxes.\r\n\r\nLets do a rough savings estimate when switching from incandescent bulbs to LEDs\r\n\r\nNumber of E27 lightbulbs in use: 9\r\nAverage hours per day the bulbs are turned on for: 6\r\n\r\nResults are calculated for a 30-day month.\r\n\r\nUsing 60 W incandescent bulbs consumes 97200 W, costing 48.13 EUR\r\nUsing 9 W LED bulbs consumes 14580 W, costing 7.22 EUR\r\nThat's a saving of 40.91 EUR.\r\nAt the price of 0.69, you could buy 59 packs of instant noodles with that money!<\/pre>\n<h6><span class=\"ez-toc-section\" id=\"Test_2_invalid_input_tests\"><\/span>Test 2: invalid input tests<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true \">Enter the market price for electricity in MWh: -250  \r\nRetry! Must be &gt; 0\r\n&gt; -5\r\nRetry! Must be &gt; 0\r\n&gt; 125\r\n\r\nMarket cost of electcity is 125.00 EUR \/ MWh.\r\nThis is 0.1250 EUR per kWh before and 0.1500 EUR per kWh after taxes.\r\n\r\nLets do a rough savings estimate when switching from incandescent bulbs to LEDs\r\n\r\nNumber of E27 lightbulbs in use: -1\r\nRetry! Must be &gt; 0\r\n&gt; -3\r\nRetry! Must be &gt; 0\r\n&gt; 8\r\nAverage hours per day the bulbs are turned on for: 7\r\n\r\nResults are calculated for a 30-day month.\r\n\r\nUsing 60 W incandescent bulbs consumes 100800 W, costing 15.12 EUR\r\nUsing 9 W LED bulbs consumes 15120 W, costing 2.27 EUR\r\nThat's a saving of 12.85 EUR.\r\nAt the price of 0.69, you could buy 18 packs of instant noodles with that money!<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Task_2_finding_results_from_an_array\"><\/span>Task 2: finding results from an array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The purpose of the second task is to find results from a user-entered array, based on the knowledge acquired so far. The functions you create must be universal (array length must not be fixed, but passed). These will also be the first function in your own collection of functions that you can easily copy in as needed later in the course!<\/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 should read 5 integers from the user. Numbers must be from -100 to 100, ends inclusive.<\/li>\n<li>Print out the numbers entered (self-check)<\/li>\n<li>Find and print the arithmetic mean with 2 places after the comma.<\/li>\n<li>Find and print the smallest value in the array<\/li>\n<li>Allow the user to enter a number and then find if it was a part of the originally entered array or not.<\/li>\n<li><strong><strong>All functions must be made according to the function descriptions provided on this page under &#8220;<\/strong>Step-by-step guide to solve the task&#8221;<\/strong><\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Code_general_structure\"><\/span>Code general structure<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>This is a general recommendation of order of operations for the code which you may use as a template.<\/p>\n<pre class=\"lang:c decode:true \">#include &lt;stdio.h&gt;\r\n\r\n\/\/ Constants for min and max value\r\n#define VALUE_MIN -100\r\n#define VALUE_MAX 100\r\n\r\n\/\/ Constant for amount of numbers the program processes\r\n#define NUM_CNT 5\r\n\r\n\/\/ Function prototypes\r\n\r\n\r\n\r\nint main(void)\r\n{\r\n    \/\/ Call the function \"filling the array\"\r\n    \r\n    \r\n    \/\/ Call the function \"printing the array\"\r\n    \r\n    \r\n    \/\/ Call the function \"arithmetic mean\", \r\n    \/\/ and print the answer returned in here\r\n    \r\n    \r\n    \/\/ Call the function \"minimum value\",\r\n    \/\/ and print the answer returned in here\r\n    \r\n    \r\n    \/\/ Call \"reading a single integer\". Store the returned value.\r\n    \/\/ It will be passed the the next function.\r\n    \r\n    \r\n    \/\/ Call the function \"check if value in array\",\r\n    \/\/ after which print the conclusion right here whether the number was in the array or not    \r\n    \r\n    return 0;\r\n}\r\n<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Step-by-step_guide_to_solve_the_task\"><\/span>Step-by-step guide to solve the task<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>In addition to what you have studied before, the following rules also apply<\/p>\n<ul>\n<li>Constants defined as macros that could vary from task to task (e.g. array length, min and max values, tax percentage) must be passed as a parameter. This does not apply for magical number that will never change (e.g. mass of an electron, universal gas constant, pi).\n<pre class=\"toolbar:2 lang:default decode:true\">\/\/ Sample given from lab task 1\r\n\r\n\/\/ Magical number is defined as a macro\r\n#define VAT_PERCENTAGE 20.0f\r\n\r\n\/\/ When calling the function, we pass it\r\nCalculateVat(pricePerKwh, VAT_PERCENTAGE);\r\n\r\n\/\/ In the function we accept it as a parameter (local variable)\r\nfloat CalculateVat(float price, float vatPcnt):<\/pre>\n<\/li>\n<li>The functions you create for calculation results must not have side effects (e.g. arithmetic mean, min value, does the vaule exist in an array). The result must be returned and printed out in main. Printing the result in the function is not allowed.<\/li>\n<li>Side effects are allowed (and expected) in functions dealing with input and output (e.g. reading a single integer, printing of the array).<\/li>\n<li>No global variables! All variables must be declared as local variables, values should be passed as parameters and given back using the return value.<\/li>\n<\/ul>\n<p><strong>Solve the program step-by-step. Test your code every time after you complete a function.\u00a0<\/strong>The functions are given in the order in which we recommend to complete them for this task.<\/p>\n<p>You must create all functions described below. There can be more functions than initially may seem reasonable. Just as a reminder &#8211; <strong>the philosophy is to write simple and short functions that typically don&#8217;t have any side effects and that do only one thing (and do it well).<\/strong><\/p>\n<h6><span class=\"ez-toc-section\" id=\"_%E2%9E%A1_Function_reading_a_single_integer\"><\/span>\u00a0\u27a1<strong> Function<\/strong><strong>: reading a single integer<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p><strong>Description:<\/strong><\/p>\n<p>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\u2019t 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 \u2013 minimum allowed value.<\/li>\n<li>integer \u2013 maximum allowed value.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong>\u00a0integer, that is between the allowed minimum and maximum.<\/p>\n<p><strong>Use:\u00a0<\/strong>You will need to ask the user for input in two places in your program. Use this function for both of those!<\/p>\n<h6><span class=\"ez-toc-section\" id=\"_%E2%9E%A1_Function_filling_the_array\"><\/span>\u00a0\u27a1 Function: filling the array<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p><strong>Description: <\/strong>Function is used to read n integers and store them in the array. However the function itself does not include any\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">scanf()<\/span>\u00a0 statements to read the values! To read a number, instead it will call the function &#8220;reading a single integer&#8221; and the value returned by it will be stored into the array. This will be done repeatedly in a loop for each member of the array.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array \u2013 the array that will be filled.<\/li>\n<li>integer \u2013 array length.<\/li>\n<\/ul>\n<p>You can add 2 extra parameters, min and max, if you wish to do so (lower and upper bound of the values).<\/p>\n<p><strong>Return:<\/strong>\u00a0none.<\/p>\n<h6><span class=\"ez-toc-section\" id=\"_%E2%9E%A1_Function_printing_the_array\"><\/span>\u00a0\u27a1 Function: printing the array<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p><strong>Description:<\/strong>\u00a0Function prints values stored in the array.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array \u2013 the array that will be printed.<\/li>\n<li>integer \u2013 array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong>\u00a0none.<\/p>\n<h6><span class=\"ez-toc-section\" id=\"_%E2%9E%A1_Function_arithmetic_mean\"><\/span>\u00a0\u27a1<strong>\u00a0<\/strong>Function: arithmetic mean<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p><strong>Description:<\/strong>\u00a0Function finds the arithmetic mean from the array and returns it.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array \u2013 array, that contains the members of which the average will be calculated.<\/li>\n<li>integer \u2013 array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> real number \u2013 arithmetic mean.<\/p>\n<h6><span class=\"ez-toc-section\" id=\"_%E2%9E%A1_Function_minimum_value\"><\/span>\u00a0\u27a1<strong>\u00a0<\/strong>Function: minimum value<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<p><strong>Description:<\/strong>\u00a0Function finds the smallest member of the array and returns it.<\/p>\n<p><strong>Parameters:<\/strong><\/p>\n<ul>\n<li>integer array \u2013 values from where the minimum value will be searched for.<\/li>\n<li>integer \u2013 array length.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong>\u00a0integer \u2013 smallest number in the array.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Testing-2\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<h6><span class=\"ez-toc-section\" id=\"Test_1_no_errors-2\"><\/span>Test 1: no errors<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true \" title=\"N\u00e4idis 1: Korrektne sisend\">Enter number 1 \/ 5: 5\r\nEnter number 2 \/ 5: 9\r\nEnter number 3 \/ 5: -2\r\nEnter number 4 \/ 5: 16\r\nEnter number 5 \/ 5: 3\r\n\r\nThe entered numbers are: 5 9 -2 16 3\r\n\r\nSmallest value is -2\r\nArithmetic mean is 6.20<\/pre>\n<h6><span class=\"ez-toc-section\" id=\"Test_2_errors_in_input\"><\/span>Test 2: errors in input<span class=\"ez-toc-section-end\"><\/span><\/h6>\n<pre class=\"theme:cisco-router toolbar:1 nums:false nums-toggle:false lang:default highlight:0 decode:true\" title=\"N\u00e4idis 2: Vigane sisend\">Enter number 1 \/ 5: 15\r\nEnter number 2 \/ 5: -234\r\nError! Allowed input range is -100 .. 100\r\n&gt; 999\r\nError! Allowed input range is -100 .. 100\r\n&gt; -5\r\nEnter number 3 \/ 5: 19\r\nEnter number 4 \/ 5: 25\r\nEnter number 5 \/ 5: 55\r\n\r\nThe entered numbers are: 15 -5 19 25 55\r\n\r\nSmallest value is -5\r\nArithmetic mean is 21.80<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Advanced_task_1_Check_if_value_in_array\"><\/span>Advanced task 1: Check if value in array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>For this task, you will create a function that checks if a given number is in the array or not.<\/p>\n<p>You can use a boolean value instead of a coded integer, however boolean data type is not yet covered in the class (will be in a few weeks).<\/p>\n<p><strong>Reminders:<\/strong><\/p>\n<ul>\n<li>When returning a coded integer, avoid magical numbers! 0 and 1 are magical in this context because they have a meaning.<\/li>\n<li>Function cannot have side effects.<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Function_description\"><\/span>Function description<span class=\"ez-toc-section-end\"><\/span><\/h5>\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 \u2013 values from were the search key is being looked for.<\/li>\n<li>integer \u2013 array length.<\/li>\n<li>integer \u2013 the value that is being looked for.<\/li>\n<\/ul>\n<p><strong>Return:<\/strong> integer \u2013 was the entered number present in the array or not.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Testing-3\"><\/span>Testing<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<pre class=\"theme:cisco-router toolbar:1 nums:false nums-toggle:false lang:default highlight:0 decode:true\" title=\"N\u00e4idis 2: Vigane sisend\">Enter number 1 \/ 5: 15\r\nEnter number 2 \/ 5: -234\r\nError! Allowed input range is -100 .. 100\r\n&gt; 999\r\nError! Allowed input range is -100 .. 100\r\n&gt; -5\r\nEnter number 3 \/ 5: 19\r\nEnter number 4 \/ 5: 25\r\nEnter number 5 \/ 5: 55\r\n\r\nThe entered numbers are: 15 -5 19 25 55\r\n\r\nSmallest value is -5\r\nArithmetic mean is 21.80\r\n\r\nEnter a number to search for: 12345\r\nError! Allowed input range is -100 .. 100\r\n&gt; 55\r\nThe number 55 is in the array!<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Advanced_task_2_multiples_of_n_to_the_new_array\"><\/span>Advanced task 2: multiples of n to the new array<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>User enters a multiplier (positive integer). Reusa a function you already have made!<\/li>\n<li>Create a function inside of which you will fill the members for a 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<\/ul>\n<ul>\n<li>Function cannot have any side effects &#8211; it is not allowed to print the array. In addition, you are not allowed to call a print function from this function.<\/li>\n<\/ul>\n<\/li>\n<li>Print the members of the newly populated array. Use a function that you already have made before.<\/li>\n<\/ul>\n<p><i>E.g.: Input array (-5, 3, -12, 9, 22), multiplier (3), formed array(3, 12, 9)<\/i><\/p>\n<h3><span class=\"ez-toc-section\" id=\"After_the_class_you_should\"><\/span><strong>After the class, you should<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Know the difference between a local and a global variable<\/li>\n<li>Understand what a function is and why it is important to use them<\/li>\n<li>Understand that we have been using functions from the first week. Both functions that return values and those that don\u2019t!<\/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 decompose your code into smaller functions<\/li>\n<li><strong>Have a small list of universal functions that you can reuse in future codes! This list of functions will increase every week from now on.<\/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 material Slides: functions Additional example: dist_converter.c Lab tasks For this lab, you have 2 tasks. There is also an advanced task, which extends the features of lab task #2. In the first task, you need to write the declarative part of functions in a pre-written program. In this task, you will only be working &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/6-lab-functions\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">6. 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-7388","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\/7388","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=7388"}],"version-history":[{"count":0,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/7388\/revisions"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=7388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=7388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=7388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}