{"id":1485,"date":"2015-08-08T00:01:33","date_gmt":"2015-08-08T07:01:33","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1485"},"modified":"2015-08-01T09:04:36","modified_gmt":"2015-08-01T16:04:36","slug":"plotting-squares-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1485","title":{"rendered":"Plotting Squares &#8211; Solution"},"content":{"rendered":"<p>The key to unlocking <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1453\">this month&#8217;s Exercise<\/a> is not to go insane on the code that processes the solution. As I wrote in the Exercise post, with data analysis in C, it&#8217;s important to properly represent the data so that you can easily manipulate it.<br \/>\n<!--more--><br \/>\nAs a review, the Exercise is to examine four data sets, each of which lists four coordinates in column-row pairs. Rows are numbered. Columns are lettered. Here are the four data sets:<\/p>\n<p>Data set 0: <span style=\"color:#00ACF1\">B2, B6, F2, E5<\/span><br \/>\nData set 1: <span style=\"color:#F1231B\">B1, B4, E1, E4<\/span><br \/>\nData set 2: <span style=\"color:#008844\">C2, C5, G2, G5<\/span><br \/>\nData set 3: <span style=\"color:#EED400\">E6, E7, F6, F7<\/span><\/p>\n<p>Figure 1 illustrates how these coordinates plot out in a grid.<\/p>\n<div id=\"attachment_1486\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1486\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-figure2.png\" alt=\"Figure 1.  The data set coordinates plotted on a grid (color-coded).\" width=\"400\" height=\"317\" class=\"size-full wp-image-1486\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-figure2.png 400w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-figure2-300x238.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-figure2-379x300.png 379w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><p id=\"caption-attachment-1486\" class=\"wp-caption-text\">Figure 1.  The data set coordinates plotted on a grid (color-coded).<\/p><\/div>\n<p>My first approach was to represent the coordinates as string values. So I created four <em>char<\/em> pointer arrays:<\/p>\n<pre class=\"screen\">\r\nchar *dataset0[4] = { \"B2\", \"B6\", \"F2\", \"E5\" };\r\nchar *dataset1[4] = { \"B1\", \"B4\", \"E1\", \"E4\" };\r\nchar *dataset2[4] = { \"C2\", \"C5\", \"G2\", \"G5\" };\r\nchar *dataset3[4] = { \"E6\", \"E7\", \"F6\", \"F7\" };<\/pre>\n<p>I could have created a single, multi-dimensional array, but that proved to be too unwieldy in the code as it involved those nasty <code>**<\/code> pointer things. Even the above solution involved lots of pointers and extra manipulation required to extrapolate the data. In fact, that was the biggest problem: Getting the dataset into a manipulatable format.<\/p>\n<p>My second approach was to use structures to hold the coordinates. I used this nested structure:<\/p>\n<pre class=\"screen\">\r\nstruct coordinates {\r\n\tchar column;\r\n\tint row;\r\n};\r\nstruct dataset {\r\n\tstruct coordinates upper_left;\r\n\tstruct coordinates lower_left;\r\n\tstruct coordinates upper_right;\r\n\tstruct coordinates lower_right;\r\n};<\/pre>\n<p>The dataset is declared as follows:<\/p>\n<pre class=\"screen\">\r\nstruct dataset square[4] = {\r\n\t{ {'B',2}, {'B',6}, {'F',2}, {'E',5} },\r\n\t{ {'B',1}, {'B',4}, {'E',1}, {'E',4} },\r\n\t{ {'C',2}, {'C',5}, {'G',2}, {'G',5} },\r\n\t{ {'E',6}, {'E',7}, {'F',6}, {'F',7} }\r\n};<\/pre>\n<p>While the mechanics of the solution worked much the same, the code using the structure was far more readable than the code using strings.<\/p>\n<p>For both my solutions, the code features three functions:<\/p>\n<p><em>main()<\/em> declares the dataset and passes it to the <em>examine()<\/em> function.<\/p>\n<p><em>examine()<\/em> displays the dataset. It calls the <em>calculate()<\/em> function to determine whether the dataset represents points that plot a square. The value returned, TRUE or FALSE, is examined and the results displayed.<\/p>\n<p><em>calculate()<\/em> extrapolates the distance between the coordinates. It stores the distances in four <em>int<\/em> variables: <code>top<\/code>, <code>bottom<\/code>, <code>left<\/code>, and <code>right<\/code>. The function calculates the &#8220;distance&#8221; between the variables and returns TRUE or FALSE based on the results.<\/p>\n<p>Just to show you the difference in my code between pointers and structures, here is my first attempt at assigning distances when using pointers:<\/p>\n<pre class=\"screen\">\r\ntop = *(*(ds+2)+0) - *(*(ds+0)+0);\r\nbottom = *(*(ds+3)+0) - *(*(ds+1)+0);\r\nleft = *(*(ds+1)+1) - *(*(ds+0)+1);\r\nright = *(*(ds+3)+1) - *(*(ds+2)+1);<\/pre>\n<p>Here is the same process when using a structure, as done in my second attempt:<\/p>\n<pre class=\"screen\">\r\ntop = s->upper_right.column - s->upper_left.column;\r\nbottom = s->lower_right.column - s->lower_left.column;\r\nleft = s->lower_left.row - s->upper_left.row;\r\nright = s->lower_right.row - s->upper_right.row;<\/pre>\n<p>A non-programmer may find both examples equally bewildering. My observation is that the second one is not only to be prettier but far more readable.<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-b.c\">Click here<\/a> to view my first solution (pointers).<\/p>\n<p><a href=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/08exercise-a.c\">Click here<\/a> to view my second solution (structures).<\/p>\n<p>As usual, your solution need not be exactly the same as mine. For both my solutions, the output was the same:<\/p>\n<pre><code>Examining data set: B2 B6 F2 E5 \r\n\tCoordinates do not plot a square\r\nExamining data set: B1 B4 E1 E4 \r\n\tCoordinates plot a square\r\nExamining data set: C2 C5 G2 G5 \r\n\tCoordinates do not plot a square\r\nExamining data set: E6 E7 F6 F7 \r\n\tCoordinates plot a square<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The key to unlocking this month&#8217;s Exercise is not to go insane on the code that processes the solution. As I wrote in the Exercise post, with data analysis in C, it&#8217;s important to properly represent the data so that &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1485\">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":[5],"tags":[],"class_list":["post-1485","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1485","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=1485"}],"version-history":[{"count":9,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1485\/revisions"}],"predecessor-version":[{"id":1512,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1485\/revisions\/1512"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1485"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}