0

I codded a program to print n-bit binary gray codes. But I'm not sure if it is a backtracking program. If it is not a backtracking program, what is something I can do to use it in this code.

I want to print the binary codes themselves, not their decimal equivalent.

                vector<string> grayCode(int n)
                {
                
                    if(n<=0)
                        return {"0"};
                    if(n == 1)
                        return {"0", "1"};
                    
                    vector<string> list1 = grayCode(n-1);
                    
                    vector<string> mainList;
                    
                    for(int i=0; i<list1.size(); i++)
                        mainList.push_back("0" + list1[i]);
                        
                    for(int i=list1.size()-1; i>-1; i--){
                        mainList.push_back("1" + list1[i]);
                    
                    return mainList;
                }
                
                int main()
                {
                    vector<string> gcode = grayCode(4);
                    for(int i=0; i<gcode.size(); i++)
                        cout<< gcode[i] << "    ";
                    
                    return 0;
                }
Hanzo
  • 1
  • It's not a backtracking program, and backtracking is not a technique that can be applied to this problem. So the question is why do you think you need to use backtracking to solve this problem? – john Aug 07 '22 at 13:20
  • It is however a recursive program. Perhaps you have got backtracking and recursion mixed up. – john Aug 07 '22 at 13:22
  • This is a university assignment I have been given, apologies for not mentioning that. I have seen a gray code algorithm that does use backtracking (at least they claimed to have used it), but the output is shown in decimal only instead of the actual gray code. https://www.geeksforgeeks.org/backtracking-approach-generate-n-bit-gray-codes/ – Hanzo Aug 07 '22 at 13:30
  • I would not call that backtracking either. The idea behind backtracking is that you have to make choices but you later find that you've made the wrong choice, so you have to **backtrack** to that wrong choice and make a different choice. Neither that code or your code are backtracking algorithms by that definition. – john Aug 07 '22 at 13:36
  • Your code looks good however. The only piece I would change is `if(n<=0) return {"0"};` to `if(n<=0) return {};` – john Aug 07 '22 at 13:39
  • Oh yes that makes sense, I forgot 0 is not equivalent to NULL.. – Hanzo Aug 07 '22 at 13:42
  • And none of the question has anything to do with grey code at all. You are simply asking how to output a value in binary. – Goswin von Brederlow Aug 09 '22 at 07:03

1 Answers1

0

I want to print the binary codes themselves, not their decimal equivalent.

That should be your question. None of it has to do with grey code or backtracking. It's a simple output formatting question.

You can change the output format using for example std::hex like this:

std::cout << std::hex << gcode[i] << "    ";

That will output the value in hexadecimal. There is also std::oct for octal. Sadly there is no std::bin for binary. And std::cout.setbase(2); will also not work.

I think the simplest is to use std::bitset, specifically the to_string().

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42