-6

I have written the following program:

#include <stdio.h>

void printValue();

int main (){
   int n = 100;
   int i;
   for (i=0; i<n; i+=1)
          printValue();
}


void printValue(){
     static unsigned int y = 0;
     printf("y = %d", y);
     y+=1;
}

How can I rewrite the algorithm to make it recursive?

3 Answers3

2
#include <stdio.h>

void printValue(void);
void times(int n, void (*func)(void)){
    if(n>0){
        func();
        times(--n, func);
    }
}

int main (void){
    int n = 100;
    times(n, printValue);
    return 0;
}

void printValue(void){
    static unsigned int y = 0;
    printf("y = %d\n", y);
    y+=1;
}

#include <stdio.h>

void printValue(int);
void repeat_upto(int init_value, int end_value, int incremental,
                 void (*func)(int)){
    if(incremental < 0 ? init_value >= end_value : init_value <= end_value){
        func(init_value);
        repeat_upto(init_value + incremental, end_value, incremental, func);
    }
}

int main (void){
    repeat_upto(0, 100-1, +1, printValue);
    return 0;
}

void printValue(int v){
    printf("%d\n", v);
}

#include <stdio.h>

void printValue(int v, int end_value){
    if(v < end_value){
        printf("%d\n", v);
        printValue(v+1, end_value);
    }
}

int main (void){
    printValue(0, 100);
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • lol way better than I was expecting to see on this question... I was planning to just wholesale down vote everyone who couldn't keep themselves from answering.. – Grady Player Jan 11 '15 at 21:19
  • @mathlearner 1) I was rewrite that part of a iterative in your code is part of the for-loop. – BLUEPIXY Jan 11 '15 at 21:29
  • 2-1) _in the comments it has been said that y will not be incremented, but why?_ The result of the operation in `y+1` has not been saved. – BLUEPIXY Jan 11 '15 at 21:32
  • 2-2) I think of other people answer would be helpful. You will modify it to consider the mistake correctly if the code is wrong. – BLUEPIXY Jan 11 '15 at 21:36
  • @mathlearner It should be incremented as it is in your code, the `y = 0` is unnecessary but the value of `y` will not be set to `0` on each call to the function, the commenter is wrong. – Iharob Al Asimi Jan 11 '15 at 21:38
  • `+1` Does not mean to change the number itself and requests the results of the calculations was `+1`. Probably close to what you petition would be `++y`. – BLUEPIXY Jan 11 '15 at 21:40
  • @iharob ah, yes, thanks. I was misunderstood. The first code(original) was `y+1`. – BLUEPIXY Jan 11 '15 at 21:42
  • I was wondering if this is actually recursive: `void times(int n, void (*func)(void)){ if(n>0){ func(); times(--n, func); } } ` Could it be modified to have if n = 0 something to stop the recursion instead of simpli if(n>0)? How would you do it? – mathlearner Jan 12 '15 at 23:29
1

This is almost the same as BLUEPIXY's answer since I believe it's the straight forward solution, but since you are confused by the function pointer, I removed that.

#include <stdio.h>
#include <stdlib.h>

void
printValue()
{
    static unsigned int y;
    printf("%d\n", y);
    y += 1;
}

void
recursiveFunction(int counter)
{
    printValue();
    if (--counter == 0)
        return;
    recursiveFunction(counter);
}

int
main()
{
    recursiveFunction(100);
    return 0;
}

Or may be you mean this

#include <stdio.h>
#include <stdlib.h>

void
printValue(int y)
{
    if (++y > 100)
        return;
    printf("%d\n", y);
    printValue(y);
}

int
main()
{
    printValue(0);
    return 0;
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • 1
    It's meant for the algorithm to be generic, with that aproach you can pass any function pointer, so you have the ability to execute any function you want from within the recursive function. – Iharob Al Asimi Jan 11 '15 at 21:53
  • The thing is what you mean by `auxiliary function`? because it's a recursive function, or you mean if you could print the values inside of the recursive function itself, for which the answer is, sure. – Iharob Al Asimi Jan 11 '15 at 22:01
  • @mathlearner Well in the last program, the `recursive function` and you may say the function that actually does something, are both the same, if that was what you meant by `auxiliary function`. – Iharob Al Asimi Jan 11 '15 at 22:11
  • In the first program there is a recursive function, `recursiveFunction(int counter)` it checks for a given condition if it was not satisfied it will call itself, otherwise it will terminate. From within that function, we call the `printValue()` function, so we **do something interesting**. In the second case the function that **does something interesting** is itself the recursive function, since unless the condition is satisfied, it will call itself over and over. – Iharob Al Asimi Jan 11 '15 at 22:20
  • It depends on what are the requirements. – Iharob Al Asimi Jan 11 '15 at 22:26
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68612/discussion-between-iharob-and-mathlearner). – Iharob Al Asimi Jan 11 '15 at 22:27
  • I was wondering if this (from BLUEBIXY's answer) is actually recursive: `void times(int n, void (*func)(void)){ if(n>0){ func(); times(--n, func); } }` Could it be modified to have if n = 0 something to stop the recursion instead of simpli if(n>0)? How would you do it? – mathlearner Jan 12 '15 at 22:48
0

Instead of

void printValue()
{
    static unsigned int y = 0;
    printf("y = %d", y);
    y+1;
}

I turn it into:

void printValue(int y)
{
    y++;
    printf("y = %d\n", y);
    printValue(y);
}

Compiled -> His function

Compiled -> Recursive function

See Same output, I just did what OP wanted.

Personal I would do this without recursive function avoid infinite loop:

for (i=0; i<n; i++)
{
      printValue(y);
}

void printValue(int y){
     printf("y = %d\n", y);
}