Ramblings of Daniel Graziotin

# An example of mutual recursion using C

A

The first lecture of DSA was already interesting for me, I learned something about “Mutual Recursion” (when two recursive procedures call each other). The pseudo-code example was about checking if a number is either even or odd.
Given that

• 0 is even
• N is even if n-1 is odd
• N is odd if n-1 is even

And the algorithm:

I implemented a tiny C program which uses it:

##### dgraziotin

Dr. Daniel Graziotin received his PhD in computer science, software engineering at the Free University of Bozen-Bolzano, Italy. His research interests include human aspects in empirical software engineering with psychological measurements, Web engineering, and open science. He researches, publishes, and reviews for venues in software engineering, human-computer interaction, and psychology. Daniel is the founder of the psychoempirical software engineering discipline and guidelines. He is associate editor at the Journal of Open Research Software, academic editor at the Research Ideas and Outcomes (RIO) journal, and academic editor at the Open Communications in Computer Science journal. He is the local coordinator of the Italian Open science local group for the Open Knowledge Foundation. He is a member of ACM, SIGSOFT, and IEEE.

• andi says:

# define odd(n) ((n)&1)
# define even(n) (!(n&1)) or #define even(n) ((n+1)&1)

# define odd(n) ((n)%1)
# define even(n) ((n+1)%1)

what else to learn ? ..ö..
how to find a problem to an existing solution

• Cheers for this smart example of using macros. However, the exercise was not to smartly implement the solution to the “even or odd?” problem.
The snippet was given in order to provide an example of implementation of mutual recursion in C.

By dgraziotin
Ramblings of Daniel Graziotin