{"id":2098,"date":"2016-09-03T00:01:02","date_gmt":"2016-09-03T07:01:02","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2098"},"modified":"2016-09-10T08:04:42","modified_gmt":"2016-09-10T15:04:42","slug":"non-identical-yet-very-similar","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2098","title":{"rendered":"Non-Identical Yet Very Similar"},"content":{"rendered":"<p>Computer&#8217;s are notoriously obedient and serious. They&#8217;re exact. If you give them inaccurate instructions or bad data, the computer does its job without question. But the real world isn&#8217;t binary and often times it&#8217;s necessary to add some forgiveness into your code.<br \/>\n<!--more--><br \/>\nAs an example, computers are good at performing searches and comparisons. The desired results appear only when the matches are perfect. The problem is that items in the real world can often be close enough that a human would match them where the diligent computer finds no match.<\/p>\n<p>As a case in point, consider the following two integer arrays:<\/p>\n<p><code>int target[COUNT] = { 24, 28, 32, 45, 50, 66, 67, 70, 80, 95 };<br \/>\nint sample[COUNT] = { 26, 26, 30, 42, 50, 61, 67, 75, 85, 99 };<\/code><\/p>\n<p>At first glance, you see that both arrays contain different sets of values. Additional examination shows that the each value is sequentially higher than the previous one and, further, they&#8217;re all positive integers less than 100. Other than that, there&#8217;s nothing interesting about the two sets of values &mdash; unless you graph them both, as shown in Figure 1.<\/p>\n<div id=\"attachment_2100\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2100\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/08\/0903-figure1.png\" alt=\"Figure 1. The two data sets graphed as lines.\" width=\"550\" height=\"367\" class=\"size-full wp-image-2100\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/08\/0903-figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/08\/0903-figure1-300x200.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2016\/08\/0903-figure1-450x300.png 450w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-2100\" class=\"wp-caption-text\">Figure 1. The two data sets graphed as lines.<\/p><\/div>\n<p>When shown visually, you can quickly see that the two arrays, <code>target[]<\/code> and <code>sample[]<\/code> are pretty much identical. If you were conducting a survey or comparing test results, you would say that there&#8217;s really little difference between the two; they both graph what is essentially the same line.<\/p>\n<p>So how do you convince the computer that they&#8217;re similar?<\/p>\n<p>The answer is what I call <em>fuzzy matching<\/em>. You code a program so that it doesn&#8217;t just compare values one-to-one. Instead, you factor in some slop, plus you provide forgiveness for values way outside the range. The amount of fuzz you can add is up to your code, but before showing how that works, the following sample program compares the two arrays in a traditional, discrete and unsympathetic way:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define COUNT 10\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nint main()\r\n{\r\n    int target[COUNT] = { 24, 28, 32, 45, 50,\r\n                          66, 67, 70, 80, 95 };\r\n    int sample[COUNT] = { 26, 26, 30, 42, 50,\r\n                          61, 67, 75, 85, 99 };\r\n    int x,match;\r\n\r\n    match = TRUE;       <span class=\"comments\">\/* initialize match *\/<\/span>\r\n\r\n    <span class=\"comments\">\/* compare arrays *\/<\/span>\r\n    for(x=0; x&lt;COUNT; x++)\r\n    {\r\n        if( target[x] != sample[x] )\r\n        {\r\n            match = FALSE;\r\n            break;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* display results *\/<\/span>\r\n    if(match)\r\n        printf(\"The arrays are identical\\n\");\r\n    else\r\n        printf(\"The arrays do not match\\n\");\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Arrays <code>target[]<\/code> and <code>sample[]<\/code> are declared at Lines 9 and 11.<\/p>\n<p>The <em>for<\/em> loop at Line 18 compares each array&#8217;s elements. When the <em>if<\/em> statement&#8217;s result at Line 20 is false, variable <code>match<\/code> is set to <code>FALSE<\/code> at Line 22 and the loop is broken (Line 23).<\/p>\n<p>The <em>if-else<\/em> structure at Line 28 displays the comparison results based on the value of variable <code>match<\/code>.<\/p>\n<p>Here is a sample run:<\/p>\n<pre><code>The arrays do not match<\/code><\/pre>\n<p>The program is correct: The arrays do not match. If the code is plotting a spacecraft&#8217;s course to Jupiter, such results would be a good thing. Yet, if it&#8217;s comparing the two lines shown in Figure 1, the results aren&#8217;t that helpful.<\/p>\n<p>To get the results you want, you must add some <em>fuzz<\/em> to the comparison formula. I&#8217;ll demonstrate how to do so in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=2104\">next week&#8217;s Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To compare two sets of values that are very similar, yet not really identical, you have to add some fuzz. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2098\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2098","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2098","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2098"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2098\/revisions"}],"predecessor-version":[{"id":2141,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2098\/revisions\/2141"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2098"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2098"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2098"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}