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

Username:   Password: 
 RegisterRegister   
 Simple number comparison for a beginner
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Naveg




PostPosted: Fri Jan 28, 2005 6:44 pm   Post subject: Simple number comparison for a beginner

How do i compare 5 user specified numbers and print the largest and smallest? without using a billion if statements
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Jan 28, 2005 7:20 pm   Post subject: (No subject)

For finding the minimum:


  1. Create a variable, set it to the value of the first number.
  2. Compare that variable to the next number in the list. If it's larger set the variable to the value of the other variable.
  3. If you're not at the end of the list, go to step 2.
  4. Print the variable, which holds the smallest number.


Assuming an array with 5 numbers: [3, 4, 1, 2, 9]

code:
foo = 3
foo > 4?  No.  foo = 3
foo > 1? Yes.  foo = 1
foo > 2? No.  foo = 1
foo > 9? No.  foo = 1
1 is the smallest number
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: