{"id":6755,"date":"2025-01-08T00:01:33","date_gmt":"2025-01-08T08:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6755"},"modified":"2025-01-04T10:32:19","modified_gmt":"2025-01-04T18:32:19","slug":"emulating-the-modulus-operator-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6755","title":{"rendered":"Emulating the Modulus Operator &#8211; Solution"},"content":{"rendered":"<p>In an alternative universe, the C programming language lacks a modulus operator and no companion function exists in the library. What to do! Why, you code your own modulus function, which is the challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6733\">this month&#8217;s Exercise<\/a>.<br \/>\n<!--more--><br \/>\nFor me, the weirdest part about my solution is dealing with remainders of integers. I hope you avoided the temptation to typecast values into real numbers. Yes, this operation can be performed with integers, which is good because integer division does the rounding for you.<\/p>\n<p>Here is my <em>mod()<\/em> function:<\/p>\n<p><code>int mod(int x, int y)<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;return( x-y * (x\/y) );<br \/>\n}<\/code><\/p>\n<p>The expression in the <em>return<\/em> statement looks confusing &mdash; if you read it left-to-right. Because of the order of operations, the expression is evaluated from right-to-left:<\/p>\n<p><code>(x\/y)<\/code><\/p>\n<p>The value of <code>x<\/code> is divided by <code>y<\/code>, which is rounded down because these are integer values. The result is the factor of variable <code>y<\/code> just below variable <code>x<\/code>. So if 41 and 7 are input as <code>x<\/code> and <code>y<\/code>, respectively, the value returned is 5: 5&times;7 = 35, which is less than 41.<\/p>\n<p>The next step is to multiply the factor by the second value:<\/p>\n<p><code>y * (x\/y)<\/code><\/p>\n<p>Don&#8217;t be fooled into thinking that the computer calculates <code>x-y<\/code> first. It does not. Instead, the value of <code>y<\/code> is multiplied by the result of <code>(x\/y)<\/code>, the factor just below variable <code>x<\/code>. Again, using 41 for <code>x<\/code> and 7 for <code>y<\/code> you get the result 35: 7&times;5 or 7&times;(41&div;7).<\/p>\n<p>Finally, you subtract the result of <code>y * (x\/y)<\/code> from the original value <code>x<\/code>:<\/p>\n<p><code>x-y * (x\/y)<\/code><\/p>\n<p>Using the same values as before: 41 &#8211; 7 &times; (41 &div; 7). The result is the modulus of 41 and 7.<\/p>\n<p>Here&#8217;s the full code for my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_01-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_01-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n<span class=\"comments\">\/*\r\n   Exercise January 2025\r\n   emulate the % operator\r\n*\/<\/span>\r\n#include &lt;stdio.h&gt;\r\n\r\nint mod(int x, int y)\r\n{\r\n    return( x-y * (x\/y) );\r\n}\r\n\r\nint main()\r\n{\r\n    int a,b;\r\n\r\n    printf(\"Enter two integers separated by a space: \");\r\n    scanf(\"%d\",&amp;a);\r\n    scanf(\"%d\",&amp;b);\r\n\r\n    printf(\"%% operator: %d %% %d = %d\\n\", a, b, a%b);\r\n    printf(\"mod() function: %d %% %d = %d\\n\", a, b, mod(a,b));\r\n\r\n    return 0;\r\n}\r\n<\/pre>\n<p>Obviously, other ways are available to solve this puzzle. I had to wiggle the expression around a few times to get the proper result, which was negative at one point in time. The final expression is short enough to be cryptic, but it works. I hope that your solution met with success.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an alternative universe, the C programming language lacks a modulus operator and no companion function exists in the library. What to do! Why, you code your own modulus function, which is the challenge for this month&#8217;s Exercise.<\/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-6755","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\/6755","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=6755"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6755\/revisions"}],"predecessor-version":[{"id":6784,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6755\/revisions\/6784"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}