{"id":6650,"date":"2024-11-08T00:01:07","date_gmt":"2024-11-08T08:01:07","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6650"},"modified":"2024-11-02T11:39:02","modified_gmt":"2024-11-02T18:39:02","slug":"ethiopian-multiplication-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6650","title":{"rendered":"Ethiopian Multiplication &#8211; Solution"},"content":{"rendered":"<p>The challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6640\">this month&#8217;s Exercise<\/a> is to write code that uses the Ethiopian Multiplication method. The process involves doubling and halving the factors, then eliminating and finally tallying the result.<br \/>\n<!--more--><br \/>\nFor my solution I use only a single <em>while<\/em> loop. This loop both tallies the results and doubles\/halves the values. As I worked on the code, I discovered that you don&#8217;t really need to maintain a two-column list, which would involve more storage, but you can eliminate even value results in the second column as they&#8217;re calculated.<\/p>\n<p>Here is my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_11-Exercise.c\" rel=\"noopener\" target=\"_blank\">2024_11-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a,b,total;\r\n\r\n    <span class=\"comments\">\/* gather input *\/<\/span>\r\n    printf(\"First value: \");\r\n    scanf(\"%d\",&amp;a);\r\n    printf(\"Second value: \");\r\n    scanf(\"%d\",&amp;b);\r\n\r\n    <span class=\"comments\">\/* do math *\/<\/span>\r\n    printf(\"Traditional: %d * %d = %d\\n\",a,b,a*b);\r\n    printf(\"  Ethiopian: %d * %d = \",a,b);\r\n    total = 0;\r\n    while(a&gt;0)\r\n    {\r\n        total += a%2 ? b : 0;\r\n        a&gt;&gt;=1;\r\n        b&lt;&lt;=1;\r\n    }\r\n    printf(\"%d\\n\",total);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Variable <code>a<\/code> is the first value, the first column. Variable <code>b<\/code> is the second. I&#8217;m unsure whether a specific order is necessary. I didn&#8217;t test Ethiopian Multiplication to see if swapping the numbers changes anything. (I don&#8217;t think it would.)<\/p>\n<p>The <em>while<\/em> loop spins until the value of <code>a<\/code> is zero or less. Variable <code>total<\/code> calculates the result, accumulating values as they&#8217;re manipulated.<\/p>\n<p>The ternary operator tests to see whether variable <code>a<\/code> (the first column) is even:<\/p>\n<p><code>a%2 ? b : 0<\/code><\/p>\n<p>If so, zero is added to variable <code>total<\/code>. Otherwise, the value of variable <code>b<\/code> is added to <code>total<\/code>.<\/p>\n<p>Next, variable <code>a<\/code> is halved: <code>a&gt;&gt;=1<\/code><\/p>\n<p>Then variable <code>b<\/code> is doubled: <code>b&lt;&lt;=1<\/code><\/p>\n<p>When the loop terminates, the result is output. It&#8217;s matched with the result generated from using the <code>*<\/code> operator.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>First value: 22<br \/>\nSecond value: 47<br \/>\nTraditional: 22 * 47 = 1034<br \/>\n&nbsp;&nbsp;Ethiopian: 22 * 47 = 1034<\/code><\/p>\n<p>I hope that your solution met with success! Remember, it need not be identical to mine. In fact, I can think of several ways to perform this calculation. As long as it uses the Ethiopian methodology and calculates the same result, consider your solution valid and passing the challenge.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The challenge for this month&#8217;s Exercise is to write code that uses the Ethiopian Multiplication method. The process involves doubling and halving the factors, then eliminating and finally tallying the result.<\/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-6650","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\/6650","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=6650"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6650\/revisions"}],"predecessor-version":[{"id":6672,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6650\/revisions\/6672"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}