Thread: recursive find of the smallest member..

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to solve it again
    but the compiler doesnt run it
    and it doesnt says on what line it happens

    Code:
    #include <stdio.h>
    int recFindMin (int array[] ,int index);
    
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=6;
    	 array[2]=3;
        
    
    printf("%d\n",min(array, 2));
    
     
    
       return 0;
    }
    
    int recFindMin (int array[] ,int index)
    {
    	int min,local;
        if (index==0)
    	{
           return array[index];
    	}
        
        local=recFindMin (array ,index-1);
    	if (array[index]>local)
    	{
    	   return local;
    	}
    	else
    	{
    		return array[index];
    	}
    	 return min;
    }//end func

  2. #17
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by transgalactic2 View Post
    i tried to solve it again
    but the compiler doesnt run it
    and it doesnt says on what line it happens
    That's because it isn't a syntax error but a logical error.
    Recursion is one of the difficult areas in programming.
    The following article explains how recursion works.
    http://faq.cprogramming.com/cgi-bin/...&id=1073086407
    Read this first before programming using recursion because recursion is just not like any other stuff.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #18
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i think i solved it

    Code:
    #include <stdio.h>
    int recFindMin (int array[100] ,int index);
    
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=1;
    	 array[2]=3;
        
    
    printf("%d\n",recFindMin(array, 2));
    
     
    
       return 0;
    }
    
    int recFindMin (int array[100] ,int index)
    {
    	int  local;
        if (index==0)
    	{
           return array[index];
    	}
        
        local=recFindMin (array ,index-1);
    	if (array[index]>local)
    	{
    	   return local;
    	}
    	else
    	{
    		return array[index];
    	}
    	  
    }//end func

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    when i tried to tell it to check not all the array but til a certain index
    it gave the correct output
    but i got an error
    "the array is corrupt"
    why??

    Code:
    #include <stdio.h>
    int recFindMin (int array[100] ,int index);
    
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=1;
    	 array[2]=3;
         array[3]=0;
    
    printf("%d\n",recFindMin(array, 2));
    
     
    
       return 0;
    }
    
    int recFindMin (int array[100] ,int index)
    {
    	int  local;
        if (index==0)
    	{
           return array[index];
    	}
        
        local=recFindMin (array ,index-1);
    	if (array[index]>local)
    	{
    	   return local;
    	}
    	else
    	{
    		return array[index];
    	}
    	  
    }//end func

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by transgalactic2
    thats what i am doing i am comparing the index member with the rest of the members behind it
    Suppose the input is 2, 3, 1, 4, 5. Describe, step by step, how your algorithm finds the minimum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i thought of the simplest case
    if i got an array of two cells
    |1|2|

    in that case recFindMin (array ,index-1);
    will give me 1
    and i compare it with 2 the current index
    and return the smaller amongst them

    why i get a corrupt array error while running??

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by transgalactic2
    in that case recFindMin (array ,index-1);
    will give me 1
    and i compare it with 2 the current index
    and return the smaller amongst them
    However, you first call recFindMin(array, 2). This means that you access array[2], which is out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i ran your input on to my code
    it output the correct answer
    but after that i get
    "stack around the variable array is corrupted"

    Code:
    #include <stdio.h>
    int recFindMin (int array[100] ,int index);
    
    int main()
    {
    	int array[3];
    	
    	 array[0]=2;
    	 array[1]=3;
    	 array[2]=1;
         array[3]=4;
    	 array[4]=5;
    	 
    
    printf("%d\n",recFindMin(array, 4));
    
     
    
       return 0;
    }
    
    int recFindMin (int array[100] ,int index)
    {
    	int  local;
        if (index==0)
    	{
           return array[index];
    	}
        
        local=recFindMin (array ,index-1);
    	if (array[index]>local)
    	{
    	   return local;
    	}
    	else
    	{
    		return array[index];
    	}
    	  
    }//end func

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    You forgot to change the array size.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    max size of array is 4 and you are accessing the 5th element.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    However, you first call recFindMin(array, 2). This means that you access array[2], which is out of bounds.
    in what line it accsess
    array[2]

  12. #27
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by transgalactic2 View Post
    in what line it accsess
    array[2]
    Your program stops working if the array had only two elements which of course is a trivial case.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    it from 0..4
    i am supposed to access the 5th element

    in what line the problem is?

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by transgalactic2
    in what line it accsess
    array[2]
    On this line:
    Code:
    if (array[index]>local)
    However, I am wrong, since your array has 3 elements, so array[2] is not out of bounds. On the other hand, you also wrote array[3]=0; which does access the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    is it a problem in a certain line
    or i need to change the whole program?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Replies: 2
    Last Post: 09-05-2004, 07:13 AM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM