recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    recursion

    this fonction is supposed to print character by character am just wondring if I can do better

    Code:
    void PROMPT(char *T)
    {
    	int i = 0;
    
    	if(*(T+i) != '\0')
    	{
    		printf("%c", *(T+i));
    		i++;
    	}
    	else
    		return;
    	PROMPT(T+i);
    }
  • YarrOfDoom
    Recognized Expert Top Contributor
    • Aug 2007
    • 1243

    #2
    There is still some room for improvement.
    Try going over this code line by line with some example input and see if you can find what is obsolete.
    If you really can't find it, I could provide you with some more hints.

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      I feel that there is something odd because its not beautiful :) but can't get it

      Comment

      • YarrOfDoom
        Recognized Expert Top Contributor
        • Aug 2007
        • 1243

        #4
        Well to me the code looks like a converted loop (replace if by while and remove everything starting from else and it would work, however I guess you want to keep it recursive here), so try thinking about something you would need in a loop, but not necessarily in a function.

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #5
          I know that that "i" is not a good idea but how do you wanna keep track of your position on the string??

          Comment

          • YarrOfDoom
            Recognized Expert Top Contributor
            • Aug 2007
            • 1243

            #6
            If you look closely at the code, you see that i only takes the values '0' and '1', because every time the function is called, it is initialised back to '0'.
            So in this case, i can be replaced with constants throughout the code.

            Comment

            • momotaro
              Contributor
              • Sep 2006
              • 357

              #7
              the code become:


              [CODE=C]

              void PROMPT(char *T)
              {
              if(*T != '\0')
              printf("%c", *T);
              else
              return;
              PROMPT(T+1);
              }[/CODE]

              and it's working! thx

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Your printf could be replaced with putchar.

                Where you recursively call PROMPT ask yourself under what condition you get there? There are 2 return points from this function, they could be reduced to 1.

                Comment

                • momotaro
                  Contributor
                  • Sep 2006
                  • 357

                  #9
                  sorry but I don't see how

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    The function has 2 execution paths when *T != '\0' and when *T == '\0'. It also has 1 explicit return (and i implicit return). Obviously you can not remove the implicit return so examine the 2 execution paths to see which statements get executed for the 2 conditions and see if you can simplify the 2 paths and reduce to a single implicit return.

                    As a clue you need an if without an else.

                    Comment

                    • momotaro
                      Contributor
                      • Sep 2006
                      • 357

                      #11
                      Code:
                      void PROMPT(char *T)
                      {
                      	if(*T == '\0') 
                      		return;
                      	printf("%c", *T);
                      	PROMPT(T+1);	
                      }
                      thx!

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Not bad but reverse the if condition and you can remove the return statement too, you will need 1 each of these {, } too.

                        Comment

                        • momotaro
                          Contributor
                          • Sep 2006
                          • 357

                          #13
                          perfect this is recursion ! thx

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            Code:
                            if(*T == '\0')
                            is the same as

                            Code:
                            if(!(*T))
                            so you get:

                            Code:
                            if(!(*T)) return;

                            Comment

                            Working...