Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Largest number of a list
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
dc116




PostPosted: 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
Sponsor
sponsor
DanielG




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: Thu Dec 25, 2008 7:30 pm   Post subject: RE:Largest number of a list

Thanks for the help guys!
btiffin




PostPosted: 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




PostPosted: 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




PostPosted: 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
Sponsor
sponsor
A.J




PostPosted: 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




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: