1

So I want to write a program that takes a binary value as input and converts it into gray code and vice versa.

Here's what I originally wrote:

#include <iostream>

using namespace std;

int main()
{

    int Choice;
    bool g0,g1,g2,g3,b0,b1,b2,b3;

    cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
    cin>>Choice;

    if(Choice==1)
    {

        cout<<"Enter the Binary Bits." << endl;

        cin>>b0>>b1>>b2>>b3;


        cout<<"Orginal Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;

        g3=b3;
        g2=b3^b2;
        g1=b2^b1;
        g0=b1^b0;

        cout<<"Converted Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;
    }
    else if(Choice==2)
    {

        cout<<"Enter The Gray Code Bits." << endl;

        cin>>g0>>>g1>>g2>>g3;


        cout<<"Original Gray Code Form: "<<g3 <<g2 <<g1 <<g0 << endl;

        b3=g3;
        b2=b3^g2;
        b1=b2^g1;
        b0=b1^g0;

        cout<<"Converted Binary Form: "<<b3 <<b2 <<b1 <<b0 << endl;
    }

    return 0;
}

Now this works for 4 bits but

  1. I want something that determines the size of the entered binary/gray code by the user during runtime.

    I did a bit of research and found out about using vectors in such a case but since in college we just started C++ I'm not familiar with vectors or even arrays. Is there anything else that I can use to achieve it? If not, can anyone tell me how vectors can be used for it?

  2. Secondly I want to take the input in a single line without spaces.

    Example:

    1011 rather than 1 0 1 1 or taking input for each bit in a separate line.

  3. Now I also realize that I won't be able to know the no. of bits in advance so the bits formula I used to achieve XOR operation would also be changed. Is it possible to declare a binary and gray code bool variable and somehow perform the XOR operation on those variables rather the individual bits using simpler statements, nothing complex?

Morwenn
  • 21,684
  • 12
  • 93
  • 152
user2299599
  • 19
  • 1
  • 6

2 Answers2

1

you could read a string and evaluate a char at a time, to get you started something like this:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    std::string input
    int Choice;

    cout<<"For Binary To Gray Code Enter 1." << endl <<"For Gray Code to Binary Enter 2." << endl;;
    cin>>Choice;
    cout << "Enter input string: ";
    cin >> input;

    if(Choice==1)
        Gray2Binary(input.c_str(), input.length());
    else
        Binary2Gray(input.c_str(), input.length());

    return 0;
}

void Gray2Binary(char s[], int n)
{
    int state = s[0]=='1' ? 1 : 0;
    int i=0;

    do {
        if (i > 0)
            state ^= s[i++]=='1' ? 1 : 0;

        cout << state;
    }while (i<n);

    cout << endl;
}
Exceptyon
  • 1,584
  • 16
  • 23
  • Note: why not just pass a `std::string const&` to the `Gray2Binary` function ? Why tease the attributes apart ? – Matthieu M. Apr 19 '13 at 15:24
  • @Matthiu: yes you are right, passing a const string& and using a string iterator in the loop would be better. The original code looked "simple" and didn't want to throw in too much stuff for the task – Exceptyon Apr 19 '13 at 15:32
0

It cost me 30 minutes to write, test and debug.

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;

void printReverse(bool* arr, int size){
    for(int i = size - 1; i >= 0 ; i--)
        cout << arr[i];
    cout << endl;
}
int main(){
    int choice;
    string str;

    cout << "For Binary To Gray Code Enter 1." << endl
        << "For Gray Code to Binary Enter 2." << endl;
    cin >> choice;

    if( choice != 1 && choice != 2)
        return 1;

    if(choice == 1) cout << "Enter the Binary Bits." << endl;
    else cout << "Enter the Gray Code Bits." << endl;

    cin >> str;

    int size = str.size();
    bool arr[size];

    for(int i = 0; i < size; i++)
        if(str[i] == '0')
            arr[i] = false;
        else
            arr[i] = true;

    if(choice == 1) cout << "Orginal Binary Form: ";
    else cout << "Orignal Gray Code Form: ";

    printReverse(arr, size);

    if(choice == 1){
        for(int i = 0; i < size - 1 ; i++) // explain 1
            arr[i] = arr[i] ^ arr[i + 1];
        cout << "Converted Gray Code Form: ";
        printReverse(arr, size);
    } else {
        for(int i = str.size() - 2; i >= 0 ; i--) // explain 2
             arr[i] = arr[i] ^ arr[i + 1];
        cout << "Converted Binary Form: ";
        printReverse(arr, size);
    }
    system("pause");
    return 0;
}

printReverse: (take 4 bits as an example)

cout << arr[3]; // cout << b3; or cout << g3;
cout << arr[2]; // cout << b2; or cout << g2;
cout << arr[1]; // cout << b1; or cout << g1;
cout << arr[0]; // cout << b0; or cout << g0;

explain 1: (take 4 bits as an example)

// at the beggining, arr[0], arr[1], arr[2], arr[3] are b0, b1, b2, b3
arr[0] = arr[0] ^ arr[1]; // g0 = b0 ^ b1; // arr[0] becomes g0 after assignment
arr[1] = arr[1] ^ arr[2]; // g1 = b1 ^ b2; // arr[1] becomes g1 after assignment
arr[2] = arr[2] ^ arr[3]; // g2 = b2 ^ b3; // arr[2] becomes g2 after assignment
arr[3] = arr[3];          // g3 = b3;      // arr[3] becomes g3 after assignment

explain 2: (take 4 bits as an example)

// at the beggining, arr[0], arr[1], arr[2], arr[3] are g0, g1, g2, g3
arr[3] = arr[3];          // b3 = g3;      // arr[3] becomes b3 after assignment
arr[2] = arr[2] ^ arr[3]; // b2 = g2 ^ b3; // arr[2] becomes b2 after assignment
arr[1] = arr[1] ^ arr[2]; // b1 = g1 ^ b2; // arr[1] becomes b1 after assignment
arr[0] = arr[0] ^ arr[1]; // b0 = g0 ^ b1; // arr[0] becomes b0 after assignment
johnchen902
  • 9,531
  • 1
  • 27
  • 69
  • Can you explain the following for clarification as Im new to loops for(int i = 0; i < str.size() - 1 ; i++) str[i] = ((str[i] == '1') ^ (str[i + 1] == '1')) ? '1' : '0'; and for(int i = str.size() - 2; i >= 0 ; i--) str[i] = ((str[i] == '1') ^ (str[i + 1] == '1')) ? '1' : '0'; – user2299599 Apr 19 '13 at 16:29
  • @user2299599 I changed my code to make it easier to understand. – johnchen902 Apr 20 '13 at 02:32
  • OK I do understand the Explanations you gave now :) But one thing I'm not getting is that you made a function printReverse for(int i = size - 1; i >= 0 ; i--)..how is that working.also how these arrays are differentiating between g's and b's? arr[0] = arr[0] ^ arr[1]; // g0 = b0 ^ b1;??? – user2299599 Apr 20 '13 at 16:45
  • @user2299599 I added the explanations of printReverse and of differentiating between g's and b's. – johnchen902 Apr 21 '13 at 05:43