{"id":577,"date":"2014-03-08T00:01:42","date_gmt":"2014-03-08T08:01:42","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=577"},"modified":"2014-03-01T10:08:07","modified_gmt":"2014-03-01T18:08:07","slug":"splitting-things-in-half","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=577","title":{"rendered":"Splitting Things in Half"},"content":{"rendered":"<p>It&#8217;s pretty safe to avoid your first instinct when it comes to dividing two integers. That first instinct is most likely to typecast the <em>int<\/em> values as float. That works, and many times it can be the best solution, but it&#8217;s not always <em>the<\/em> solution.<br \/>\n<!--more--><br \/>\nWhen you deal in integer values, and those values must be split, you typically ignore anything left over &#8212; the remainder. So 13 divided by 2 is going to equal either 6 or 7, take your pick. You don&#8217;t want to split hairs &#8212; or integers.<\/p>\n<p>For example, consider you have a burning desire to split a string of text in two. Strings with an even length split evenly. Strings with an odd length don&#8217;t split evenly; you can&#8217;t have a string with half a letter in it.<\/p>\n<p>In the end, you manipulate an integer value without regard for fractions.<\/p>\n<p>The simple way to split an <em>int<\/em> value in twain is to divide it by 2. Yes, division is normally performed with floating point values. Even when dividing integers, the result often ends up being a <em>float<\/em>. Feel free to disregard that thinking for a moment and behold this sample code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int alpha = 29;\r\n\r\n    printf(\"29 divided by 2 is %d.\\n\",alpha\/2);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s the output:<\/p>\n<p><code>29 divided by 2 is 14.<\/code><\/p>\n<p>And, of course, that answer won&#8217;t get you an &#8216;A&#8217; on human math quiz, but it&#8217;s how the computer deals with dividing two <em>int<\/em> values. It&#8217;s also sufficient for splitting a string in two; 14 characters of a 29-character string is good enough for &#8220;half.&#8221;<\/p>\n<p>To get the true result, you need to make two modifications.<\/p>\n<p>First, at least one of the values in the equation <code>alpha\/2<\/code> must be a floating point value, a real number.<\/p>\n<p>Second, you need to modify the <em>printf()<\/em> statement to use the <code>%f<\/code> conversion character.<\/p>\n<p>Solving the first problem can be done in several ways. The most obvious is to change variable <code>alpha<\/code> from an <em>int<\/em> to a <em>float<\/em> at Line 5:<\/p>\n<p><code>float alpha = 29;<\/code><\/p>\n<p>Or you can typecast either the variable <code>alpha<\/code> or the immediate value <code>2<\/code> in the <em>printf()<\/em> function, thus:<\/p>\n<p><code>printf(\"29 divided by 2 is %f.\\n\",<span style=\"color:red\">(float)alpha<\/span>\/2);<\/code><\/p>\n<p>Or this:<\/p>\n<p><code>printf(\"29 divided by 2 is %f.\\n\",alpha\/<span style=\"color:red\">(float)2<\/span>);<\/code><\/p>\n<p>You see above that I&#8217;ve already addressed the second modification, which is changing the <code>%d<\/code> to <code>%f<\/code>. Actually, I prefer to use <code>%.1f<\/code> for this problem, which yields the following output:<\/p>\n<p><code>29 divided by 2 is 14.5.<\/code><\/p>\n<p>That result isn&#8217;t going to help you when it comes to printing half a string of text, but it&#8217;s one way to solve the problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Division normally lies in the realm of real numbers, the <em>floats<\/em> and <em>doubles<\/em>. It can, however, be done quickly on an integer, just not that accurately. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=577\">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-577","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\/577","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=577"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/577\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=577"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=577"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=577"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}