{"id":8966,"date":"2023-10-04T20:58:36","date_gmt":"2023-10-04T18:58:36","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=8966"},"modified":"2025-08-20T15:47:02","modified_gmt":"2025-08-20T13:47:02","slug":"distance-converter-arrays-with-functions","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/distance-converter-arrays-with-functions\/","title":{"rendered":"Distance converter (arrays with functions)"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 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-4'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/distance-converter-arrays-with-functions\/#Task_solved_by_the_code\" >Task solved by the code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/distance-converter-arrays-with-functions\/#Notice_the_following_in_the_code\" >Notice the following in the code<\/a><\/li><\/ul><\/nav><\/div>\n<h4><span class=\"ez-toc-section\" id=\"Task_solved_by_the_code\"><\/span>Task solved by the code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>Read in N integers from the user, representing distances in kilometers, into an array.<\/li>\n<li>Distances are read as integers, while the answers\u00a0 after the conversion must be decimals.<\/li>\n<li>Convert the distances to miles.<\/li>\n<li>Find and print the sum of the entered distances.<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Notice_the_following_in_the_code\"><\/span>Notice the following in the code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<ul>\n<li>Macros are passed as parameters to functions to support better reusability of these functions\n<ul>\n<li>They will not be dependent on the macro name<\/li>\n<li>They can be reused for different length arrays in the same code<\/li>\n<\/ul>\n<\/li>\n<li>Only variables that contain relevant data and arrays are being passed to functions. Local variables and results are declared in the function (they don&#8217;t contain any relevant data to be copied to the function, so they would just make our code slower for no reason).<\/li>\n<li>All functions are commented.<\/li>\n<li>The result is calculated in a function and returned. This helps with reusability (i.e. if you need to find lots of results without print statements cluttering up the output).<\/li>\n<li>User must enter the distances as integers, but results after conversion are stored in a float array. Type casting is used for the integer array.<\/li>\n<li>Original array is preserved (might be needed for some other task for a more complex task) and results are stored in a new array.\n<ul>\n<li>Since arrays cannot be returned, the new array is also declared in main and passed to the convert function.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre class=\"toolbar:2 lang:c decode:true \">\/**\r\n * File:         dist_converter.c\r\n * Author:       Risto Heinsar\r\n * Created:      26.10.2020\r\n * Last edit:    20.06.2025\r\n *\r\n * Description:  Reads an array filled with integers (representing distances \r\n *               in kilometers) and converts it to miles. \r\n *               Prints the sum of all distances in miles.\r\n *\/\r\n#include &lt;stdio.h&gt;\r\n\r\n#define DIST_COUNT 4\r\n#define MILES_IN_KM 1.609f\r\n\r\nvoid ReadDistances(int dist[], int n);\r\nvoid ConvertArrToMiles(int distKm[], float distMi[], int n);\r\nvoid PrintDistances(float dist[], int n);\r\nfloat SumOfAllDist(float dist[], int n);\r\n\r\nint main(void)\r\n{\r\n    \/* Read distances to memory *\/\r\n    int distancesInKilometers[DIST_COUNT];\r\n    ReadDistances(distancesInKilometers, DIST_COUNT);\r\n    \r\n    \/* Convert to miles and print the converted distances *\/\r\n    float distancesInMiles[DIST_COUNT];\r\n    ConvertArrToMiles(distancesInKilometers, distancesInMiles, DIST_COUNT);\r\n    PrintDistances(distancesInMiles, DIST_COUNT);\r\n    \r\n    \/* Find the sum of all distances *\/\r\n    int totalDist = SumOfAllDist(distancesInMiles, DIST_COUNT);\r\n    printf(\"The sum of all distances is %.2f miles!\\n\", totalDist);\r\n\r\n    return 0;\r\n}\r\n\r\n\r\n\/**\r\n * Description:    Asks the user for n integers and stores them in an array.\r\n *\r\n * Parameters:     dist - array to store distances entered by the user\r\n *                 n - number of distances to read (also length of array)\r\n *\r\n * Return:         none\r\n *\/\r\nvoid ReadDistances(int dist[], int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"Enter distance %d in km (integer): \", i + 1);\r\n        scanf(\"%d\", &amp;dist[i]);\r\n    }\r\n}\r\n\r\n\r\n\/**\r\n * Description:    Converts an array filled with distances in kilometers to\r\n *                 miles and stores them in the second array.\r\n *\r\n * Parameters:     distKm - array with distances in kilometers\r\n *                 distMi - array to store calculated distances in miles\r\n *                 n - number of distances to convert (length of both arrays)\r\n *\r\n * Return:         none\r\n *\/\r\nvoid ConvertArrToMiles(int distKm[], float distMi[], int n)\r\n{\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        distMi[i] = (float)distKm[i] \/ MILES_IN_KM;\r\n    }\r\n}\r\n\r\n\r\n\/**\r\n * Description:    Prints the given array of distances with 2 decimal places \r\n *                 after the comma.\r\n *\r\n * Parameters:     dist - array with distances as decimals\r\n *                 n - length of the array.\r\n *\r\n * Return:         none\r\n *\/\r\nvoid PrintDistances(float dist[], int n)\r\n{\r\n    printf(\"The converted distances are: \");\r\n\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        printf(\"%.2f \", dist[i]);\r\n    }\r\n    printf(\"\\n\");\r\n}\r\n\r\n\/**\r\n * Description:    Sums up all the distances in the array, returns the sum\r\n *\r\n * Parameters:     dist - array with distances as decimals\r\n *                 n - length of the array.\r\n *\r\n * Return:         the sum of given array members (total distance)\r\n *\/\r\nfloat SumOfAllDist(float dist[], int n)\r\n{\r\n    float sum = 0.0f;\r\n\r\n    for (int i = 0; i &lt; n; i++)\r\n    {\r\n        sum += dist[i];\r\n    }\r\n\r\n    return sum;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Task solved by the code Read in N integers from the user, representing distances in kilometers, into an array. Distances are read as integers, while the answers\u00a0 after the conversion must be decimals. Convert the distances to miles. Find and print the sum of the entered distances. Notice the following in the code Macros are &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/distance-converter-arrays-with-functions\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Distance converter (arrays with functions)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":8,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-8966","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"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=8966"}],"version-history":[{"count":8,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8966\/revisions"}],"predecessor-version":[{"id":10270,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/8966\/revisions\/10270"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1521"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=8966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}