Computer Science Canada

Simple number comparison for a beginner

Author:  Naveg [ 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

Author:  wtd [ Fri Jan 28, 2005 7:20 pm ]
Post 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


: