Sunday, July 12, 2009

How can I use the Math.Max method in c# to compare 6 numbers?

I also need to find the minimum as well.

How can I use the Math.Max method in c# to compare 6 numbers?
Nested looping





Put the values in an array


Use the array index value as the looping control value, e.g.


the outer loop goes from i=0 to 4 while the inner loop goes from


j=i+1 to 5 (array indices start at zero, right?)





Compare the values in the array positions i and j using the max function.


You can store the max and min found after the comparisons.


Change the stored max only if the new max is greater than the one you have stored.


Change the stored min only if the new min is less than the one you have stored.





And no, I won't write it for you - but good luck! :)


--------------------------------


Doesn't matter how you get the values... you can still save and compare them. An array is just for simplicity's sake. If you haven't done arrays yet, then just store them in variables a1, b2, c3, d4, e5, and f6 (as examples). Store your overall max in max_all, store your overall min in min_all. Compare them all sequentially if you need to.





The max(a1,b2) function will give you back the larger of the two values. The max(a1, max_all) result will tell you if you should replace your stored max with the one you just got back from comparing the first two.





Nest them all if you want:


max_all = max(a1,max(b2,max(c3, max(d4, max(e5, f6)))))





Heck, now I'm writing code ;-)





Still, good luck.

geranium

No comments:

Post a Comment