{"id":4834,"date":"2016-12-23T04:00:58","date_gmt":"2016-12-23T04:00:58","guid":{"rendered":"https:\/\/assignment.essayshark.com\/blog\/?p=4834"},"modified":"2022-01-12T10:33:22","modified_gmt":"2022-01-12T10:33:22","slug":"sample-projects-in-c-consecutive-numbers-in-gray-code-sequence","status":"publish","type":"post","link":"https:\/\/assignmentshark.com\/blog\/sample-projects-in-c-consecutive-numbers-in-gray-code-sequence\/","title":{"rendered":"Sample Projects in C#: Consecutive Numbers in Gray Code Sequence"},"content":{"rendered":"<p style=\"text-align: center;\"><strong>Consecutive Numbers in Gray Code Sequence<\/strong><\/p>\n<ul>\n<li>The reflected binary code (RBC), also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit (binary digit). The reflected binary code was originally designed to prevent spurious output from electromechanical switches. Today, Gray codes are widely used to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems. (en.wikipedia.org, 2002)<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<ul>\n<li>So we need to implement a program in C++ that will successively read two numbers which are represented as four-bit numbers. And then it should determine whether these to numbers are consecutive in the Gray code.<\/li>\n<li>Two\u00a0binary reflected Gray code neighbors differ by only one bit. But, it doesn\u2019t mean that two Gray codes differing by one bit are neighbors (<span class=\"code\">a =&gt; b<\/span>\u00a0does not mean that\u00a0<span class=\"code\">b =&gt; a<\/span>).<\/li>\n<li>To determine whether two Gray codes are neighbors, we have to check whether\u00a0<span class=\"code\">previous(a) = b OR next(a) = b<\/span>. For a given Gray code, you get one neighbor by flipping the rightmost bit and the other neighbor bit by flipping the bit at the left of the rightmost set bit. For the Gray code 1010, the neighbors are 1011 and 1110 (1000 is not one of them).<\/li>\n<li>Whether you get the previous or the next neighbor by flipping one of these bits actually depends on the parity of the Gray code. However, since we want both neighbors, we don&#8217;t have to check for parity.<\/li>\n<\/ul>\n<p>Screenshots of working program:<\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4844 aligncenter\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-1.png\" alt=\"grey-code-example-1\" width=\"600\" height=\"306\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-1.png 660w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-1-300x153.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4846 aligncenter\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-2.png\" alt=\"grey-code-example-2\" width=\"600\" height=\"306\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-2.png 660w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-2-300x153.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4848 aligncenter\" src=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-3.png\" alt=\"grey-code-example-3\" width=\"600\" height=\"306\" srcset=\"https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-3.png 660w, https:\/\/assignmentshark.com\/blog\/wp-content\/uploads\/2016\/10\/grey-Code-example-3-300x153.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>Code listing with comments:<\/p>\n<p>[code language=&#8221;cpp&#8221;]<br \/>\n#include &lt;iostream&gt;<\/p>\n<p>const int N = 5;\/\/amount of sybmols in the number<\/p>\n<p>bool checkInput(char arr[])<br \/>\n{<br \/>\n\tbool error = 0;<br \/>\n\tfor (int i = 0; i &lt; 4; i++)<br \/>\n\t{<br \/>\n\t\tif ((arr[i] != &#8216;1&#8217;) &amp;&amp; (arr[i] != &#8216;0&#8217;))<br \/>\n\t\t{<br \/>\n\t\t\tstd::cout &lt;&lt; &quot;Error in &quot; &lt;&lt; i + 1 &lt;&lt; &quot;&#8217;th symbol\\n&quot;;<br \/>\n\t\t\terror = true;<br \/>\n\t\t}<br \/>\n\t}<\/p>\n<p>\treturn error;<br \/>\n}<\/p>\n<p>void checkGray(char a[], char b[])<br \/>\n{<br \/>\n\tint sum2(0), sum1(0);<\/p>\n<p>\tfor (int i = 0; i &lt; 4; i++) {<br \/>\n\t\tsum1 += a[i];<br \/>\n\t\tsum2 += b[i];<br \/>\n\t}<\/p>\n<p>\tif (sum1 == sum2 + 1 || sum2 == sum1 + 1)std::cout &lt;&lt; &quot;Numbers are consecutive\\n&quot;;<br \/>\n\telse std::cout &lt;&lt; &quot;Numbers aren&#8217;t consecutive&quot;;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<\/p>\n<p>\tchar a[N], b[N];\/\/Gray codes<\/p>\n<p>\tstd::cout &lt;&lt; &quot;Input the first number: &quot;;<br \/>\n\tstd::cin.getline(a, N);\/\/read the first number<\/p>\n<p>\tif (checkInput(a) == 1)\/\/check for only 0 and 1 in the number<br \/>\n\t{<br \/>\n\t\tsystem(&quot;pause&quot;);<br \/>\n\t\texit(1);<br \/>\n\t}<\/p>\n<p>\tstd::cout &lt;&lt; &quot;Input the second number: &quot;;<br \/>\n\tstd::cin.getline(b, N);\/\/read the second number<\/p>\n<p>\tif (checkInput(b) == 1)\/\/check for only 0 and 1 in the number<br \/>\n\t{<br \/>\n\t\tsystem(&quot;pause&quot;);<br \/>\n\t\texit(1);<br \/>\n\t}<\/p>\n<p>\tcheckGray(a, b);\/\/check for consecutive numbers<\/p>\n<p>\tsystem(&quot;pause&quot;);<\/p>\n<p>\treturn 0;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p style=\"text-align: center;\">Reference<\/p>\n<p>Gray code. (2002, April 27). Retrieved July 08, 2016, from <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gray_code\" target=\"_blank\" rel=\"noopener noreferrer nofollow\">https:\/\/en.wikipedia.org\/wiki\/Gray_code<\/a><\/p>\n<blockquote><p><em>Our sample projects in C# were completed just for demonstration of our possibilities, so you shouldn\u2019t use it as your work. You can also enjoy reading one of our <a href=\"https:\/\/assignmentshark.com\/blog\/c-programming-code-examples-finding-a-palindrome\/\" target=\"_blank\" rel=\"noopener noreferrer\">C# programming code examples<\/a>. Better make the order right now and be satisfied with your assignment!<\/em><\/p>\n<p><em>If you need to complete something similar to our sample projects in C# and you don\u2019t know how to cope with this don\u2019t panic. AssignmentShark was established right for students like you. Our experts are able to provide you with qualified <a href=\"https:\/\/assignmentshark.com\/\" target=\"_blank\" rel=\"noopener\">assignment help<\/a> any time you need it. The only thing you need to do is to specify your requirements accurately. During the whole process you\u2019ll be able to keep in touch with your expert via live chat. Also, you have the ability to track order\u2019s progress.<\/em><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Consecutive Numbers in Gray Code Sequence The reflected binary code (RBC), also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit (binary digit). The reflected binary code was originally designed to prevent spurious output from electromechanical switches. Today, Gray codes are widely used [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53,35],"tags":[],"class_list":["post-4834","post","type-post","status-publish","format-standard","hentry","category-it","category-samples"],"_links":{"self":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4834","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/comments?post=4834"}],"version-history":[{"count":20,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4834\/revisions"}],"predecessor-version":[{"id":13023,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/posts\/4834\/revisions\/13023"}],"wp:attachment":[{"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/media?parent=4834"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/categories?post=4834"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/assignmentshark.com\/blog\/wp-json\/wp\/v2\/tags?post=4834"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}