Computer Science Canada [Tutorial] Finding the Minimum and Maximum in an Array |
Author: | wtd [ Fri Oct 01, 2004 4:42 pm ] | ||||
Post subject: | [Tutorial] Finding the Minimum and Maximum in an Array | ||||
Finding Maximum The basic algorithm Create a variable to store your current maximum. Set this variable to the first value in your array. This will make sure that the result is a value from the array, rather than some default value that might be already larger than any value in the array. Now, loop through the remaining values in the array. Each time, check to see if the value is greater than the current maximum. If it is, then change the current maximum to that number. At the end of the loop, you'll have the maximum value in the array. The code
Finding Minimum The basic algorithm Create a variable to store your current minimum. Set this variable to the first value in your array. This will make sure that the result is a value from the array, rather than some default value that might be already larger than any value in the array. Now, loop through the remaining values in the array. Each time, check to see if the value is less than the current minimum. If it is, then change the current minimum to that number. At the end of the loop, you'll have the minimum value in the array. The code
|
Author: | Cervantes [ Sat Oct 02, 2004 7:01 am ] | ||||
Post subject: | |||||
err, wtd, arr (2) needs to be replaced with arr(i)
I also changed max to Max and min to Min because max and min are built-in turing functions. Thanks for the tutorial +BITS |
Author: | wtd [ Sat Oct 02, 2004 12:23 pm ] |
Post subject: | |
Thanks for the eye for detail. |