{"id":5890,"date":"2023-06-01T00:01:14","date_gmt":"2023-06-01T07:01:14","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5890"},"modified":"2023-06-08T18:40:35","modified_gmt":"2023-06-09T01:40:35","slug":"swapping-variables","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5890","title":{"rendered":"Swapping Variables"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>The classic paradigm for swapping values between two variables involves using a third variable. But is the third variable even necessary?<br \/>\n<!--more--><br \/>\nFor example, to swap <code>a<\/code> and <code>b<\/code>, you use variable <code>c<\/code>:<\/p>\n<p><code>c = a;<br \/>\na = b;<br \/>\nb = c;<\/code><\/p>\n<p>At the end of the operation, the values of variables <code>a<\/code> and <code>b<\/code> are swapped, with variable <code>c<\/code> left with nothing else to do. But is variable <code>c<\/code> even necessary?<\/p>\n<p>It turns out you need only two variables to swap their values &mdash; odd as it sounds. The process involves a bit of math and some head scratching, but it works. Rather than rush to StackOverflow for the answer, try on your own to think of a way to swap two variables&#8217; values, which is the challenge for this month&#8217;s Exercise.<\/p>\n<p>Here&#8217;s a skeleton to get you started:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2023_06_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2023_06_01-Lesson.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\r\n    printf(\"After: A=%d, B=%d\\n\",a,b);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>As the code stands now, two values are fetched from standard input. Both are output, then comes the swap (the part you write) and the results are output:<\/p>\n<p><code>Enter value A: 10<br \/>\nEnter value B: 45<br \/>\nBefore: A=10, B=45<br \/>\nAfter: A=10, B=45<\/code><\/p>\n<p>Your challenge is to add code after the comment, fancy math stuff that swaps the values without adding a third variable. In fact, as a hint, you need three lines of code (three statements) just as if you were using a third variable.<\/p>\n<p>Here is the output from my solution:<\/p>\n<p><code>Enter value A: 10<br \/>\nEnter value B: 45<br \/>\nBefore: A=10, B=45<br \/>\nAfter: A=45, B=10<\/code><\/p>\n<p>Please O please do try this exercise on your own. The solution is widely available on the interwebs, but I had a good time trying to make the swap happen without cheating. You can view my solution <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5898\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Can you put the value of <code>a<\/code> into <code>b<\/code> when using only these two variables? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5890\">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":[3],"tags":[],"class_list":["post-5890","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5890","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=5890"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5890\/revisions"}],"predecessor-version":[{"id":5913,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5890\/revisions\/5913"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}