Thread: The typical Largest smallest number printing

  1. #16
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    @Fader_Berg: If the user inputs all positive numbers and you have initialized min to 0 then the value of min will always be 0 and the same with the max if all the numbers are in negative.

  2. #17
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Thanks CommonTater, initializing all the values first will prevent having "undefined behavior"

    jannr

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jannr View Post
    Thanks CommonTater, initializing all the values first will prevent having "undefined behavior"

    jannr
    Yes, it will prevent that form of undefined behavior... there are others...

    Consider what happens if your user presses Enter without making an entry... If the variable is not preset to a known safe value (0 in this case) it may contain 298,143,345 ... what's that do to your program?

    Look up the values of MAX_INT and MIN_INT in the limits.h header... by initializing max to the lowest possible value, no matter what the user enters, that first if() statement will assign it that value because whatever the user entered has to be higher. Similarly by initializing min to the highest possible value you force an assignment on the first if() statement.

    Proper initialization does not always mean = 0; it will vary from one function/situation to the next.

  4. #19
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    Ahh,, Thanks for the info CommonTater.

    However, I cannot use limits.h yet as we have not taken it up yet so I'll just stick to 0 this time but I would definitely try and use limits.h.
    Great information though! Thanks again!! ^^

    jannr

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jannr View Post
    Ahh,, Thanks for the info CommonTater.

    However, I cannot use limits.h yet as we have not taken it up yet so I'll just stick to 0 this time but I would definitely try and use limits.h.
    Great information though! Thanks again!! ^^

    jannr
    Y'know what... any teacher who does not reward initiative --like using something you haven't studied in class yet-- probably should not be teaching. You should never hold yourself back just because the teacher hasn't spoon fed it to you yet... all you really do is stunt your own learning experience.

  6. #21
    Registered User
    Join Date
    Jun 2011
    Location
    Ozamiz, Philippines
    Posts
    22
    hmm, I guess you're right. I am actually studying by myself in advance here on CProgramming.com and also reading the book that was provided to us, which is "C how to program".

    I'm quite far from the class and my teacher praises us students who do advance studies and stuff but tries to keep us to the current topic as not to set off the other students to be more confused about what we're discussing if I incorporate into my code things that we, or rather my classmates, have not discussed or known yet.

    Certainly the information you gave will really be useful and I'm not really only limiting myself to the pace of our discussion but only trying to go with my classmates to also help them learn.

    *That was quite long.. HAHA!

    Btw, thanks for your concern CommonTater,

    jannr

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No worries...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting largest and smallest number
    By .C-Man. in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2011, 06:29 AM
  2. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  4. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  5. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 03:40 PM

Tags for this Thread