Recursion code for generating Gray Code in C

  • Thread starter Thread starter Peon666
  • Start date Start date
  • Tags Tags
    Code Recursion
Click For Summary
SUMMARY

The discussion focuses on implementing a recursive function in C to generate Gray code for a specified number of bits. The base case is established as 1 bit, producing the output G = {0, 1}. For n = 2, the process involves reflecting the bits, concatenating the original and reflected lists, and appending zeros and ones accordingly, resulting in G = {00, 01, 11, 10}. The provided code snippet outlines the recursive structure and logic necessary to achieve this functionality.

PREREQUISITES
  • Understanding of recursion in programming
  • Familiarity with C programming language
  • Knowledge of arrays and string manipulation in C
  • Basic concepts of Gray code generation
NEXT STEPS
  • Study the implementation of recursion in C programming
  • Learn about Gray code and its applications in digital systems
  • Explore array manipulation techniques in C
  • Investigate optimization techniques for recursive algorithms
USEFUL FOR

Students learning recursion, C programmers interested in algorithm design, and anyone looking to understand Gray code generation techniques.

Peon666
Messages
107
Reaction score
0

Homework Statement



I need to implement a recursive code for generating the Gray code for a given number of bits. For example, if the input bit is 1 it generates Gray code for 1 bit number i.e 0 and 1. Given 2, it generates Gray code for 2 bit numbers i.e 00 and 01 and so on. I don't understand how to implement and what should be the base case.

I'd be really grateful in case of any help.

Thanks.

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
First, a few pointers about recursion:
- Find the base case
- Implement that first
- Define when you should go in recursion
- If the recursive call succeeds once, it will always succeed

As you may know, recursion is, essentially, solving a huge problem by solving the smaller parts of the problem which are, in essence, the same problem as the huge one. The postulate is that when all smaller problems have been solved, the problem as a whole has been solved.

Anyway, enough of that.

Your base case is 1 bit, which results in G = {0,1}. To go to n = 2, reflect the bits (it becomes {1,0}), concatenate that list with the original list and append the 0 to the original list and the 1 to the reflected list, resulting in G = {{00},{01},{11},{10}}.

n = 1
0
1

Reflect and concatenate:
0
1
1
0

Append zeros and ones:
00
01
11
10

We now have n = 2.

In short, you pass a number of bits to your method, n that specifies the Grey code for n bits. Recursively go down to one, where you can create the base for n = 1, then work your way back up.

Code:
char[] bits

void grayCode(int numBits) {
  if(bits == 1) {
    bits = ['0','1'];
  } else {
    if(numBits != 1) { 
      grayCode(numBits - 1);
    }
    char[] mirroredBits = reverse(bits);
    bits.concat(mirroredBits);
    //for the first n bits, append 0
    //for the last n bits, append 1
  }
}

Okay, it's ugly and not C, but I'm sure you can translate it to proper C which will output exactly what you need. This code goes down to your base case, constructs it and then expands upon the base case until the specified number of bits has been reached.
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 11 ·
Replies
11
Views
1K