C: Recursive function into iterative

In summary, the conversation discusses creating an iterative function that converts an integer into any base. The proposed solution involves considering what changes between recursive function calls, as well as using an array to store the digits for printing in reverse order.
  • #1
gruba
206
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
  • #2
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.
 
  • #3
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.
 

1. What is the difference between recursive and iterative functions in C?

Recursive functions are functions that call themselves until a base case is reached. Iterative functions use loops to repeat a set of instructions until a condition is met. In C, recursive functions can be more concise and elegant, but they can also use more memory and have a higher risk of causing a stack overflow.

2. How do you convert a recursive function into an iterative one in C?

To convert a recursive function into an iterative one, you can use a stack data structure to keep track of the function calls. Each time the function is called recursively, push its arguments onto the stack. Then, instead of calling the function again, pop the arguments from the stack and use them to execute the same code in a loop until the base case is reached.

3. Can all recursive functions be converted into iterative functions in C?

Yes, all recursive functions can be converted into iterative functions using the method mentioned above. However, some recursive functions may be more difficult to convert and may require more complex data structures or algorithms.

4. Why would you want to convert a recursive function into an iterative one in C?

Converting a recursive function into an iterative one can improve the performance and efficiency of the code. Iterative functions use less memory and are less likely to cause a stack overflow, making them more suitable for large or complex problems. They can also be easier to debug and maintain.

5. Are there any disadvantages to converting a recursive function into an iterative one in C?

One disadvantage of converting a recursive function into an iterative one is that it can make the code more complex and harder to understand. This can make it more difficult to debug and maintain. Additionally, some problems may be better suited for a recursive solution and may be more difficult to solve using an iterative approach.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
890
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
Back
Top