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

GPA Fix: Online Essay Writing Service from Top Tutors
Save More. Score Better
Pages (550 words)
Approximate price: -

Why Hire a Professional Essay Writer from GPA Fix

Quality Urgent Essays

GPA Fix online platform provides the best essay writing service ever received elsewhere, thanks to our professional essay writers. Experience gained in the 10+ years of being in the assignment writing industry is also an added advantage.

Professional and Experienced Academic Writers

A lot is done to maintain us as the best essay writing service provider. Besides owning a degree from a recognized university, a writer must pass the rigorous tests we take them through before they are considered eligible to offer urgent essay help on our website.

Affordable Urgent Assignment Help

Who said a reliable website should not be cheap also? While it is human nature to associate prices with quality, that does not always work. At gpafix.com, we guarantee you cheap and urgent essay help without compromising the quality.

24/7 Available Urgent Essay Writing Service

Whether it is a ‘write my essay for me cheap’ or ‘edit my essay’ order you want to request, always feel free to reach us at any time of the day. Our experts work in shifts to ensure that you access urgent essay writing services at any time of the day.

Plagiarism Free Essays Online

The punitive measures that come with plagiarized content are so harsh, and we understand that. We consequently ensure to write each client’s papers from scratch no matter how urgent the essay is. To acknowledge borrowed content, our quick writer references and cites the work.

Wow Customer Support 24/7

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

We'll send you the first draft for approval by at
Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

So much stress and so little time? Take care of yourself: let us help you with your tasks. We offer all kinds of writing services.

Essays

Custom Essay Writing Services

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission and Business Papers

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Editing

Editing and Proofreading

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Coursework

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.