Improving Binary to Grey code converter?

Hi! I wrote a program that converts Binary code to Grey code. It works, but I feel like it's highly inefficient and I was wondering if anyone has any suggestions from where to go from here or any other method of doing the same thing. I'd really like to become a more elegant programmer, but I'm not quite sure how to go about doing so.
I've also been trying out Project Euler, but I seem to always turn to using vectors whenever solving a problem. I feel like that's not a good habit to practice. So, any advice from the better programmers out there?

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
62
63
64
65
/* Binary to Grey Code Converter */

#include <iostream>
#include <vector>

using namespace std;

// Global Variables (I'd like to not use them because I've been told it's bad practice)
static int numDigits;
int digit;
vector<int> bin;
vector<int> grey;


void toGrey(){

	grey.push_back(bin[0]);

	for(int j = 0; j < numDigits; j++){
		if(bin[j] != bin[j+1]){
			grey.push_back(1);
		}else{
			grey.push_back(0);
		}
	}

}


void printGrey(){

	for(int i = 0; i < numDigits; i++){
		cout << grey[i];
	}

	cout << '\n';

}


void makeArray(int x){

	bin.push_back(x);

}


int main(int argc, char** argv){

	cout << "How many digits in your binary number?\n";

	cin >> numDigits;

	cout << "Please enter a binary number, digit by digit: \n";

	for(int i = 0; i < numDigits; i++){
		cin >> digit;
		makeArray(digit);
	}

	toGrey();
	printGrey();

	return 0;
}
It works, but I feel like it's highly inefficient

It may appear to work, but it has some undefined behavior. In particular, you access outside the bounds of the bin vector in toGrey.

(And, btw, it's Gray code, not grey code. It's a name, not a color.)

I've also been trying out Project Euler, but I seem to always turn to using vectors whenever solving a problem. I feel like that's not a good habit to practice.

vectors and/or deques should be your "go to" container.

If we can guarantee we only need a certain number of bits and that number of bits can be held in an integral type, we might be better off manipulating bits, but since there is no such guarantee mentioned in your code, I think I would probably just use strings as in the following code:

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
#include <iostream>
#include <string>


// returns '1' for true, '0' for false.
char exclusive_or(char a, char b)
{
    return (a != b) + '0' ;
}

std::string to_gray(const std::string& binary)
{
    std::string result(1, binary[0]);
    result.reserve(binary.size() + 1);

    for (unsigned i = 1; i < binary.size(); ++i)
        result += exclusive_or(binary[i - 1], binary[i]);

    return result;
}

int main()
{
    const char* prompt = "please enter a binary number:\n";

    std::string number;
    while ( std::cout << prompt && std::getline(std::cin, number) && !number.empty() )
        std::cout << to_gray(number) << '\n';
}


Slightly modified for online demo prettification of output: http://ideone.com/DZn4iy
Last edited on
Topic archived. No new replies allowed.