C: Recursive function into iterative

Click For Summary
SUMMARY

The discussion focuses on converting a recursive function for number base conversion into an iterative function in C. The original recursive function, conversion(unsigned num, unsigned base), is transformed by utilizing a loop that continues until the number is reduced to zero. Key insights include using an array as a stack to store digits and printing them in reverse order. The maximum array size required is 32 characters to accommodate unsigned integers in base 2.

PREREQUISITES
  • Understanding of C programming language
  • Knowledge of recursion and iteration concepts
  • Familiarity with arrays and their manipulation in C
  • Basic understanding of number systems and base conversions
NEXT STEPS
  • Implement an iterative version of the conversion() function in C
  • Explore stack data structures and their applications in C
  • Learn about memory management in C, particularly with arrays
  • Investigate other number base conversion algorithms for optimization
USEFUL FOR

C programmers, computer science students, and software developers interested in algorithm optimization and understanding the differences between recursive and iterative programming techniques.

gruba
Messages
203
Reaction score
1

Homework Statement


Write an iterative function char* conversion(unsigned int num, int base),
that converts an integer num into any base (base <=10).

Homework Equations

The Attempt at a Solution


How to transform the following recursive function conversion() into iterative:
Code:
#include <stdio.h>

void conversion(unsigned num, unsigned base)
{
  if (num / base)
    conversion(num / base, base);
  printf("%u", num % base);
}

int main()
{
  unsigned num, base;
  printf("num=");
  scanf("%u", &num);
  do{
     printf("base=");
     scanf("%u", &base);
    }
  while (base < 2 || base > 10);
  conversion(num, base);
  return 0;
}
 
Physics news on Phys.org
When I have convert between recursive and iterative thinking I find it's best to think about:
* what is changing between the function calls
* how do we know when to terminate and return

So in your example with every recursive call to conversion(num, base) you do num/base so this should be done every loop.

You return when num/base truncates to 0 so use this as the condition in your loop.
 
If goal is to follow the logic in the recursive function, use an array like a stack, filling the array from [0] to [n-1], printing the array from [n-1] to [0]. Assuming unsigned is 32 bits, you'll need an array of 32 characters to hold the digits for base 2, less for other bases, so make the array size 32 characters.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K