• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Minimum Function

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help writing a recursive minimum function. For the minimum function, lets say you have an array like 5,12,15,20. You would split it in half, so on one side you get 5 and 12 and the other 15 and 20. Therefore you compare 5 and 12 and 5 wins, then you compare 15 and 20 and 15 wins. Then you compare 5 and 15 and 5 wins, making that the minimum.

My brother sent me something but it looks way to complicated. Can someone make the same function but with less lines? Thanks for any help!!!

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you splitting the array instead of just doing a linear search for the minimum? Thats a lot less complicated and just as efficient. A non-recursive minimum function is as easy as:

 
Steven Alvarez
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats how the teacher wants it, I know how to do it using a for loop, it needs to be done recursively.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general approach might be: The minimum over all N elements is the minimum of the first element and the minimum of the N-1 remaining elements. That reduces the problem to finding the minimum of an actual number and the result of a recursive call.

But nobody here will just give you some code - this is an assignment that you should be doing. How are you going to learn if you hand in someone else's solution?
[ May 07, 2007: Message edited by: Ulf Dittmer ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic