Sample Projects in C#: Consecutive Numbers in Gray Code Sequence

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 to facilitate error correction in digital communications such as digital terrestrial television and some cable TV systems. (en.wikipedia.org, 2002)

 

Struggling to find relevant content? Order a custom essay on
Sample Projects in C#: Consecutive Numbers in Gray Code Sequence
Let our experts save you the hassle
Order Now
  • 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.
  • Two binary reflected Gray code neighbors differ by only one bit. But, it doesn’t mean that two Gray codes differing by one bit are neighbors (a => b does not mean that b => a).
  • To determine whether two Gray codes are neighbors, we have to check whether previous(a) = b OR next(a) = b. 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).
  • 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’t have to check for parity.

Screenshots of working program:

grey-code-example-1

grey-code-example-2

grey-code-example-3

Code listing with comments:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
const int N = 5;//amount of sybmols in the number
bool checkInput(char arr[])
{
    bool error = 0;
    for (int i = 0; i < 4; i++)
    {
        if ((arr[i] != '1') && (arr[i] != '0'))
        {
            std::cout << "Error in " << i + 1 << "'th symbol\n";
            error = true;
        }
    }
    return error;
}
void checkGray(char a[], char b[])
{
    int sum2(0), sum1(0);
    for (int i = 0; i < 4; i++) {
        sum1 += a[i];
        sum2 += b[i];
    }
    if (sum1 == sum2 + 1 || sum2 == sum1 + 1)std::cout << "Numbers are consecutive\n";
    else std::cout << "Numbers aren't consecutive";
}
int main()
{
    char a[N], b[N];//Gray codes
               
    std::cout << "Input the first number: ";
    std::cin.getline(a, N);//read the first number
    if (checkInput(a) == 1)//check for only 0 and 1 in the number
    {
        system("pause");
        exit(1);
    }
    std::cout << "Input the second number: ";
    std::cin.getline(b, N);//read the second number
    if (checkInput(b) == 1)//check for only 0 and 1 in the number
    {
        system("pause");
        exit(1);
    }
    checkGray(a, b);//check for consecutive numbers
    system("pause");
    return 0;
}

Reference

Gray code. (2002, April 27). Retrieved July 08, 2016, from https://en.wikipedia.org/wiki/Gray_code

Our sample projects in C# were completed just for demonstration of our possibilities, so you shouldn’t use it as your work. You can also enjoy reading one of our C# programming code examples. Better make the order right now and be satisfied with your assignment!

If you need to complete something similar to our sample projects in C# and you don’t know how to cope with this don’t panic. GPA Fix was established right for students like you. Our experts are able to provide you with qualified help to raise your GPA any time you need it. The only thing you need to do is to specify your requirements accurately. During the whole process you’ll be able to keep in touch with your expert via live chat. Also, you have the ability to track order’s progress.

Related: [Tutorial] How to Make a Website Using HTML CSS and JavaScript

Calculate the price
Make an order in advance and get the best price
Pages (550 words)
$0.00
*Price with a welcome 15% discount applied.
Pro tip: If you want to save more money and pay the lowest price, you need to set a more extended deadline.
We know how difficult it is to be a student these days. That's why our prices are one of the most affordable on the market, and there are no hidden fees.

Instead, we offer bonuses, discounts, and free services to make your experience outstanding.
Sign up, place your order, and leave the rest to our professional paper writers in less than 2 minutes.
step 1
Upload assignment instructions
Fill out the order form and provide paper details. You can even attach screenshots or add additional instructions later. If something is not clear or missing, the writer will contact you for clarification.
s
Get personalized services with GPA Fix
One writer for all your papers
You can select one writer for all your papers. This option enhances the consistency in the quality of your assignments. Select your preferred writer from the list of writers who have handledf your previous assignments
Same paper from different writers
Are you ordering the same assignment for a friend? You can get the same paper from different writers. The goal is to produce 100% unique and original papers
Copy of sources used
Our homework writers will provide you with copies of sources used on your request. Just add the option when plaing your order
What our partners say about us
We appreciate every review and are always looking for ways to grow. See what other students think about our do my paper service.
Other
Excellent
Customer 452813, August 21st, 2023
Nursing
Thank you so much for being the best website for assignment help.
Customer 452635, June 24th, 2022
Human Resources Management (HRM)
Thank you so much for your time.
Customer 452701, September 5th, 2023
Psychology
Excellent!!!!
Customer 452587, July 28th, 2021
Nursing
Fantastic work on this project as usual.
Customer 452707, July 5th, 2022
Technology
Excellent job on the paper!
Customer 452885, December 28th, 2022
Nursing
A-1 service every single time!!!
Customer 452453, July 27th, 2021
Social Work and Human Services
Excellent
Customer 452587, July 28th, 2021
Psychology
Thanks so very much. The paper is well-researched and adequately referenced. You have been of great help during the pandemic!
Customer 452467, January 31st, 2021
Social Work and Human Services
Excellent Work
Customer 452587, November 22nd, 2021
Other
GREAT
Customer 452813, June 20th, 2022
Criminal Justice
Thank you for the great paper. I like how the writer structured it.
Customer 452627, October 2nd, 2021
OUR GIFT TO YOU
15% OFF your first order
Use a coupon FIRST15 and enjoy expert help with any task at the most affordable price.
Claim my 15% OFF Order in Chat

Good News ! We now help with PROCTORED EXAM. Chat with a support agent for more information