Thread: recursive find of the smallest member..

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

    recursive find of the smallest member..

    i wrote a function that finds recursively the smallest member in an int array
    and it says that "min is being used without being defined"

    i cant put a value into min
    i need to get it from the process.


    ??
    Code:
    #include <stdio.h>
    int recFindMin (int array[] ,int index);
    
    int main()
    {
    	int t,array[3];
    	
    	 array[0]=2;
    	 array[1]=1;
    	 array[2]=3;
        t=recFindMin(array,2);
    	printf("%d\n",t);
    
       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)
    	{
    	   min=local;
    	}
    	 return min;
    }//end func
    Last edited by transgalactic2; 01-12-2009 at 03:31 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And exactly why do you need min at all?

    More importantly, what is min when you have a
    Code:
    array[index]<=local
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    min is the total minimum
    and local is the temporary

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what is the problem in the function code?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by transgalactic2 View Post
    what is the problem in the function code?
    Did you not trace through your function for the case I described: What is the value of min when your second if-statement is false?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    in the case you described
    nothing changes
    min changes only if there is a smaller number then it.
    i added an empty else case.

    i traced it
    but from the start my array has only one cell of value 2
    ??
    Code:
    #include <stdio.h>
    int recFindMin (int array[] ,int index);
    
    int main()
    {
    	int t,array[3];
    	
    	 array[0]=2;
    	 array[1]=1;
    	 array[2]=3;
        t=recFindMin(array,2);
    	printf("%d\n",t);
    
       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)
    	{
    	   min=local;
    	}
    	else
    	{
    	}
    	 return min;
    }//end func

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, what value does min actually have in that situation? To put it another way, what value does a variable have that hasn't been set?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i think iteratively
    min doesnt change
    its stays with the same value from the previous call

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i think iteratively
    Start thinking recursively then

    For example, suppose there is a "magic function" that gives you the minimum value in the rest of the array. To implement recFindMin to find the minimum, all you need to do is compare the current value with the value returned from that "magic function", returning the smaller one. Easy, isn't it?

    Oh, and did I mention that the "magic function" is, coincidentally, recFindMin?
    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. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thanks i solved it

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    thats what i am doing i am comparing the index member with the rest of the members behind it
    ??

  12. #12
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by transgalactic2 View Post
    thats what i am doing i am comparing the index member with the rest of the members behind it
    ??
    Looks like you posted on the wrong thread.
    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. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no i am on the correct thread
    i am trying to fix this function

    i have done the compare with the rest part
    but something is missing
    ??
    Code:
    #include <stdio.h>
    int recFindMin (int array[] ,int index);
    
    int main()
    {
    	int t,array[3];
    	
    	 array[0]=2;
    	 array[1]=1;
    	 array[2]=3;
        t=recFindMin(array,2);
    	printf("%d\n",t);
    
       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)
    	{
    	   min=local;
    	}
    	else
    	{
    	}
    	 return min;
    }//end func

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to fix it

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    What is the initial value of local?.
    First try to solve the problem yourself using a debugger and use the forum only as a last resort.
    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

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