Results 1 to 2 of 2

Thread: Recursive min problem

  1. #1
    Join Date
    Jan 2010
    Posts
    63
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Recursive min problem

    I am given a matrix and size.
    Qt Code:
    1. const int MAX=50;
    2. int min;
    3. int mat[];
    4. int FindMin(int mat[MAX],int size)
    5. {
    6. if (size==0)
    7. return 0;
    8.  
    9.  
    10. min=mat[0];
    11.  
    12. if (mat[size]<min)
    13. min=mat[size];
    14. else return FindMin(mat,size-1);
    15. }
    16.  
    17. main()
    18. {
    19. int i;
    20. srand(time(0));
    21. for(i=0;i<MAX;++i)
    22. mat[i]=1+rand()%MAX;
    23. cout<<"min-="<<<min
    To copy to clipboard, switch view to plain text mode 

    This code far from works all the time. How can I make it work ?

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Recursive min problem

    i) note that for an array of size n, the last valid index is n-1
    ii) standard recursion (not efficient, btw)

    Qt Code:
    1. int FindMin(int mat[MAX],int size)
    2. {
    3. if (size==0) return 0;
    4.  
    5. int min = FindMin(mat, size-1);
    6. return std::min(min, mat[size-1]);
    7. }
    To copy to clipboard, switch view to plain text mode 

    iii) linear code (faster)

    i
    Qt Code:
    1. nt FindMin(int mat[MAX],int size)
    2. {
    3. if (size==0) return 0;
    4. int min=mat[0];
    5. for (int=1; i<size; ++i)
    6. min=std::min(min,mat[i]);
    7. return min;
    8. }
    To copy to clipboard, switch view to plain text mode 


    iv) easiest way: do NOT INVENT THE WHEEL all over again

    Qt Code:
    1. #include <algorithm>
    2. int FindMin(int mat[MAX], int size)
    3. {
    4. return *std::min_element(mat, mat+size)
    5. }
    To copy to clipboard, switch view to plain text mode 

    for such trivial things, the stdlib already offers algorithm that work (if you use Qt classes, Qt offers such algorithms as well)


    HTH

Similar Threads

  1. Recursive call detected
    By SebastianBecker in forum Qt Programming
    Replies: 3
    Last Post: 10th September 2009, 17:25
  2. FTP recursive treeWidget
    By Aji Enrico in forum Qt Programming
    Replies: 2
    Last Post: 12th April 2008, 11:26
  3. recursive problem
    By Shawn in forum Qt Programming
    Replies: 6
    Last Post: 16th October 2007, 18:54
  4. recursive removal of dirs
    By soul_rebel in forum Qt Programming
    Replies: 1
    Last Post: 13th August 2007, 16:24
  5. how to recursive a directory in qt?
    By deweyjew in forum Qt Programming
    Replies: 5
    Last Post: 7th November 2006, 09:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.