{"id":5898,"date":"2023-06-08T00:01:33","date_gmt":"2023-06-08T07:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5898"},"modified":"2023-05-27T14:27:04","modified_gmt":"2023-05-27T21:27:04","slug":"swapping-variables-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5898","title":{"rendered":"Swapping Variables &#8211; Solution"},"content":{"rendered":"<p>This <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5890\">month&#8217;s Exercise<\/a> challenges you to swap two variables&#8217; values without using a third variable. It&#8217;s a solution widely available on the Internet, but your job is to figure out the technique without looking elsewhere.<br \/>\n<!--more--><br \/>\nI confess that I saw this solution months ago and marveled at it. But I forgot the specifics. Rather than look it up again, I set out to devise it on my own, using only my vague memory of the mathematical operations used on the two variables to swap values. Here are the three statements I use:<\/p>\n<p><code>b = b + a;<br \/>\na = b - a;<br \/>\nb = b - a;<\/code><\/p>\n<p>Yes, it took me a while to hone this result, which works for both signed and unsigned values. Figure 1 helps illustrate how the operation works.<\/p>\n<div id=\"attachment_5899\" style=\"width: 360px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5899\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/05\/0608-figure1-ab.png\" alt=\"\" width=\"350\" height=\"557\" class=\"size-full wp-image-5899\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/05\/0608-figure1-ab.png 350w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2023\/05\/0608-figure1-ab-189x300.png 189w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><p id=\"caption-attachment-5899\" class=\"wp-caption-text\">Figure 1. Swapping two variables&#8217; values by using only two variables.<\/p><\/div>\n<p>Effectively, variable <code>b<\/code> becomes what would otherwise be swapping variable <code>c<\/code>. First it holds the sum of <code>a<\/code> and <code>b<\/code>: <code>b = b + a<\/code><\/p>\n<p>When original <code>a<\/code> is subtracted, what&#8217;s left over is <code>b<\/code>, which is assigned to <code>a<\/code>: <code>a = b - a<\/code><\/p>\n<p>Finally, the new value of <code>a<\/code> (original <code>b<\/code>) is subtracted from new <code>b<\/code>, which yields the original value of <code>a<\/code>, assigned to <code>b<\/code>: <code>b = b - a<\/code><\/p>\n<p>It took my brain a few minutes to accept this solution. I even tried to condense it to only two statements, but either I&#8217;m not that smart or such a solution isn&#8217;t possible. Regardless, here is the full solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_06-Exercise.c\" rel=\"noopener\" target=\"_blank\">2023_06-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;\r\n\r\n    printf(\"Enter value A: \");\r\n    scanf(\"%d\",&amp;a);\r\n    printf(\"Enter value B: \");\r\n    scanf(\"%d\",&amp;b);\r\n\r\n    printf(\"Before: A=%d, B=%d\\n\",a,b);\r\n\r\n    <span class=\"comments\">\/* swap variables *\/<\/span>\r\n    b = b + a;\r\n    a = b - a;\r\n    b = b - a;\r\n\r\n    printf(\"After: A=%d, B=%d\\n\",a,b);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>After writing this code, I checked the interwebs to see what I found earlier, the inspiration for this Exercise. Yep, I got it right. I hope you did as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This month&#8217;s Exercise challenges you to swap two variables&#8217; values without using a third variable. It&#8217;s a solution widely available on the Internet, but your job is to figure out the technique without looking elsewhere.<\/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-5898","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\/5898","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=5898"}],"version-history":[{"count":2,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5898\/revisions"}],"predecessor-version":[{"id":5905,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5898\/revisions\/5905"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}