Minimum and maximum of Numbers

Can you please explain why we have considered min_so_far as a very large number(infinity) and max_so_far as a very small number?

Sure @avijuneja2007,

  1. for min_so_far:
    If you would initialize it with a random number(say 0).
    Now consider that the first numbers user enters is 100.
    So, the min_so_far should be 100. //Because 0 is not a part of input
    But, your code will print 0 //On comparing 0 with 100, 0 will be smaller.

    Thus, we initialize it with infinity(large number).
    Reason:
    Whatever number the user will input, it will always be smaller.

  2. Similar, is the case for max_so_far.

Hope, this would help.
Give a like if you are satisfied.

1 Like