Author |
Message |
dc116
|
Posted: Wed Dec 24, 2008 10:52 pm Post subject: Largest number of a list |
|
|
Let's say there's a list of 10 random numbers.
code: |
var number : int
for count : 1..10
number := Rand.Int(1,30)
end for
|
Is there a keyword that displays the largest number of the 10?
Any help would be appreciated. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DanielG
|
Posted: Wed Dec 24, 2008 11:08 pm Post subject: RE:Largest number of a list |
|
|
there isn't a keyword, but you can find the largest in number by getting the random number each time and comparing it to your "maximum" variable which stores the largest, and if it is larger then change the value of maximum. |
|
|
|
|
|
Insectoid
|
Posted: Thu Dec 25, 2008 10:52 am Post subject: RE:Largest number of a list |
|
|
You need to set a number to be your initial 'largest'. This has to be very small, smaller than the smallest possible number in the list (in your case, it has to be < 1). Then go through the for loop, and if you find a number bigger than it, change your 'largest' number to that number. at the end of the for loop, the 'largest' number actually will be the largest number. |
|
|
|
|
|
DanielG
|
Posted: Thu Dec 25, 2008 1:40 pm Post subject: RE:Largest number of a list |
|
|
actually it could be 1, as long as it's not bigger than the smallest it's fine. |
|
|
|
|
|
dc116
|
Posted: Thu Dec 25, 2008 7:30 pm Post subject: RE:Largest number of a list |
|
|
Thanks for the help guys! |
|
|
|
|
|
btiffin
|
Posted: Thu Dec 25, 2008 9:34 pm Post subject: RE:Largest number of a list |
|
|
Just a quick note on correctness. Take the first (any) value of the list you are scanning as the initial value for algorithms of this type. Don't try and outguess what might be the smallest or largest, things will work better.
There are exceptions of course; COBOL has the reserved figurative constant LOW-VALUE for instance, but I hope you get my meaning.
Cheers |
|
|
|
|
|
DanielG
|
Posted: Thu Dec 25, 2008 10:36 pm Post subject: RE:Largest number of a list |
|
|
I think -2147483647 is small enough for any list containing integers (at least in turing). |
|
|
|
|
|
Insectoid
|
Posted: Fri Dec 26, 2008 9:28 am Post subject: RE:Largest number of a list |
|
|
DanielG, while the minimum integer would work, btiffin's way is actually better, as it does not rely on guessing a number smaller than the smallest. In fact, even if that number is smaller than the smallest could possibly be. As he said, it's about correctness. His way is far cleaner and maybe a little more efficient. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
A.J
|
Posted: Fri Dec 26, 2008 11:43 am Post subject: Re: Largest number of a list |
|
|
yeah, I agree with btiffin....setting the initial element of the array as the 'largest' should work easier |
|
|
|
|
|
Homer_simpson
|
Posted: Fri Dec 26, 2008 4:16 pm Post subject: Re: Largest number of a list |
|
|
actually the keyword is max
Quote: var number : int := 0
for count : 1 .. 10
number := max (Rand.Int (1, 30), number)
end for
put number
|
|
|
|
|
|
|