inputting integer array in a row recursevly..

This is a discussion on inputting integer array in a row recursevly.. within the C Programming forums, part of the General Programming Boards category; i need to enter an array in a row number so they will become an array when i enter 1 ...

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

    inputting integer array in a row recursevly..

    i need to enter an array in a row number so they will become an array
    when i enter
    1 2 3 4 5 6 7 8
    it says debug assertion failed

    why its not working??
    Code:
    #include <stdio.h>
    int read_array(int input[],int i,int size);
     
    int main()
    {
        int input[40];
        printf("%d",read_array(input,0,8));
    }
    int read_array(int input[],int i,int size)
    {
    	int flag;
           
    	if (i==size-1)
    	{
         
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         read(input,i+1,size); 
    }

  2. #2
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I don't see any values being passed into input... But I don't understand all of that code eather, so I could be wrong, but I still don't see anywhere where anything could be assigned to input :P
    Currently research OpenGL

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    did you try
    1234567
    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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    oh another recursion topic, I'm surprised

    Code:
    read(input,i+1,size);
    did you mean read_array(input,i+1,size)?

    PS: what tools are you using? any basic debugger should allow to spot the error, did you try?

    PS2: once again, why open a new thread while the very same thing is discussed in another one? you won't get more answers that way...
    Last edited by root4; 01-21-2009 at 04:18 PM.

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i enter 1 2 3 4 5 6 7 8
    ok it works
    but i get a warning that not all path return value
    i cant see what case i missed

    Code:
     
    #include <stdio.h>
    int read_array(int input[],int i,int size);
     
    int main()
    {
        int input[40];
        printf("%d",read_array(input,0,8));
    }
    int read_array(int input[],int i,int size)
    {
    	int flag;
           
    	if (i==size-1)
    	{
         
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         read_array(input,i+1,size); 
    }

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    but i get a warning that not all path return value
    i cant see what case i missed
    Are you joking? your function is 10 lines long and you can't see that?
    Code:
         read_array(input,i+1,size); 
         // hint-of-the-day: function ends here
    }

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    hint 2:
    int
    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

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it need to end when i==size
    or when the input is wrong

    i dont need to return anything in the end except the recursive call
    i changed the code so the recursive call will go to some variable
    but the warning keeps showing
    what case i missed??

    Code:
     
    #include <stdio.h>
    int read_array(int input[],int i,int size);
     
    int main()
    {
        int input[40];
        printf("%d",read_array(input,0,8));
    }
    int read_array(int input[],int i,int size)
    {
    	int flag,rt;
           
    	if (i==size-1)
    	{
         
    	   return 1;
    	}
         flag=scanf("%d",&input[i]);
    	 if (flag==0)
    	 {
    		 return 0;
    	 }
    	 else
    	 {
    	 }
         rt=read_array(input,i+1,size); 
    }

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed it thanks

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
         rt=read_array(input,i+1,size); 
         return 0;
    }
    Sigh.
    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

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i put return rt in the end
    and it worked fine
    is there a problem with how i did it??

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    I suggest you get back to C programming basics before resuming this.
    i dont need to return anything in the end
    Sure. But your program tells otherwise and the compiler works on your program not on what you wish it should do.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i put return rt in the end instead of return 0
    and it worked fine
    is there a problem with how i did it??

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    as long as you return something, the compiler will be happy.

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by transgalactic2 View Post
    i put return rt in the end instead of return 0
    and it worked fine
    is there a problem with how i did it??
    Yes, that is okay. Your compiler or debugger is just being picky because it cannot be sure that control will never reach the end of the function, even if
    Code:
    int func() {
         while (1) return 0;
         return 0;    // still need this
    }
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting character array to integer array
    By quiet_forever in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2007, 06:48 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. String To Integer Array!!!!
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 10-29-2001, 10:15 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21