Thread: transforming this loop into a recursive function question

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    transforming this loop into a recursive function question

    i tried to transform this loop
    Code:
    printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
    
    	*/

    Code:
    #include <stdio.h>
    void read(input2,i);
    int main()
    {
        int input2[40];
        read(input2,0);
    }
    void read(input2,i)
    {
    	char ch;
    	if (i==39)
    	{
                        input2[i] = '\0';
    	    return ;
    	}
                   if ((i<39)&&( (ch = getchar()) != '\n') &&( ch >=0))
                  {
                       input2[i] = ch;
                   }
                   read(input2,i+1); 
    }
    but its not running
    its giving me two errors like
    (13) : error C2109: subscript requires array or pointer type
    why??
    (in the edit scren its perfectly intended but when i save it gives me what you see)
    Last edited by transgalactic2; 01-21-2009 at 09:28 AM.

  2. #2
    Registered User gil_savir's Avatar
    Join Date
    Jan 2009
    Posts
    13
    let start by giving types to the parameters in:
    Code:
    void read(input2,i)

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed it to this
    Code:
    #include <stdio.h>
    void read(char input2[],int i);
    int main()
    {
        int input2[40];
        read(input2,0);
    }
    void read(char input2[],int i)
    {
    	char ch;
    	if (i==39)
    	{
           input2[i] = '\0';
    	   return ;
    	}
        if ((i<39)&&( (ch = getchar()) != '\n') &&( ch >=0))
         {
           input2[i] = ch;
         }
         read(input2,i+1); 
    }
    but its still not working as this loop

    Code:
    /*
    printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
    
    	*/

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Why are you asking so many "recursive" questions?
    Especially for things like this which have no business being recursive.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    Why are you asking so many "recursive" questions?
    Hahaha, I laughed out loud at that. But to be fair, if this is transgalactic2's way of getting a grip on recursion, let the experiments fly.

    I still say doing the fibonacci set is a terrifically enlightening recursive function exercise.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I don't think there's much value in converting random loops to recursion for learning purposes. I agree with Salem, that it would be more productive to work on directly writing functions that are more 'naturally' recursive.
    If you're short of examples of such things, I could give you plenty.

    I reckon the useful skill is not going from iterative to recursive, but the other way around.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    its for an assignment
    i need to change the loop into a recursive function
    i tried to build it
    but its not working
    Code:
    #include <stdio.h>
    void read(char input2[],int i);
    int main()
    {
        int input2[40];
        read(input2,0);
    }
    void read(char input2[],int i)
    {
    	char ch;
    	if (i==39)
    	{
           input2[i] = '\0';
    	   return ;
    	}
        if ((i<39)&&( (ch = getchar()) != '\n') &&( ch >=0))
         {
           input2[i] = ch;
         }
         read(input2,i+1); 
    }
    but its still not working as this loop

    Code:
    /*
    printf("enter string\n");
     for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch >=0; ++i)
        {
            input2[i] = ch;
        }
    
        input2[i] = '\0';
    
    	*/
    Last edited by transgalactic2; 01-21-2009 at 01:58 PM.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    basically what i need is to enter a string in a row and when i press enter
    it stops even if the array is not full to the limit.
    this loop did exactly that
    and i was told that there is no other way to do it

    i could use scanf but then again i need to use a loop with a command(which is not allowed) to clean the buffer

    ??
    Last edited by transgalactic2; 01-21-2009 at 01:47 PM.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Code:
    void read(char input2[],int i)
    {
    	char ch;
    
    	ch = getche();
    	if( ch != '\r' && ch > 0 && i < 39 )
    		input2[i] = ch;
    	else
    	{
    		input2[i] = '\0';
    		return;
    	}
    	read(input2,i+1); 
    }
    or more sophisticated:
    Code:
    int read(char input2[], int i);
    
    int main(int argc, char* argv[])
    {
    	char input2[40];
    	input2[read(input2,0)] = '\0';
    	
    	return( 0 );
    }
    
    int read(char input2[], int i)
    {
    	char ch = getche();
    	if( ch != '\r' && ch > 0 && i < 39 )
    		input2[i] = ch;
    	else
    		return( i );
    	return( read(input2,i+1) ); 
    }
    Last edited by EOP; 01-21-2009 at 02:53 PM.

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i cant use getch
    only getchar like in the loop
    i fixed a few bug in the code you resented and its not working
    it doesnt stop on enter

    Code:
    #include <stdio.h>
    void read(char input2[],int i);
    int main()
    {
        int input2[40];
        read(input2,0);
    }
    void read(char input2[],int i)
    {
    	char ch;
    
    	ch = getchar();
    	if( ch != '\0' && ch > 0 && i < 39 )
    		input2[i] = ch;
    	else
    	{
    		input2[i] = '\0';
    		return;
    	}
    	read(input2,i+1); 
    }

  11. #11
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    If you want it to stop on enter you should check if the character is equals to '\r' instead of '\0'.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by flexo87 View Post
    If you want it to stop on enter you should check if the character is equals to '\r' instead of '\0'.
    No -- because stdin is opened in text mode, the runtime automatically translates the Windows end-of-line "\r\n" into a single "\n". You should check for '\n' to find the end of line, not '\r'
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i changed the code
    but its still not working as the loop

    Code:
    
    #include <stdio.h>
    void read(char input2[],int i);
    int main()
    {
        int input2[40];
        read(input2,0);
    }
    void read(char input2[],int i)
    {
    	int ch;
    	if (ch=='\n')
    	{
           input2[i] = '\0';
    	   return ;
    	}
        if ((i<39)&&( (ch = getchar()) != '\n') &&( ch >=0))
         {
           input2[i] = ch;
         }
         read(input2,i+1); 
    }

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    Quote Originally Posted by brewbuck View Post
    No -- because stdin is opened in text mode, the runtime automatically translates the Windows end-of-line "\r\n" into a single "\n". You should check for '\n' to find the end of line, not '\r'
    For me it worked only with '\r'.

    And both of my proposals worked under each and every condition.

    (getchar() wanted always a RETURN to complete - VS 2005)
    Last edited by EOP; 01-21-2009 at 06:06 PM.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A for-loop:
    Code:
    for (int i=0; i<n; ++i) {
        doSomething(i);
    }
    A direct translation of the above for-loop into recursive form:
    Code:
    void recursivelyDoSomething(int i, int n) {
        if (i < n) {
            doSomething(i);
            recursivelyDoSomething(i+1, n);
        }
    }
    
    ...
    
    recursivelyDoSomething(0, n);
    Does that help?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. recursive function
    By technosavvy in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 05:42 AM
  4. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM
  5. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM