Thread: How to read 10 integers and print out the smallest number?Please Help!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    13

    How to read 10 integers and print out the smallest number?Please Help!

    I need to write a program that reads 10 integers and determines and prints out the minimum integer.
    The hint is not to compare 10 integers at one time.
    The question is how do I compare one at a time using if-else statements?

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Get one thing down at a time. First read in 10 integers, that right there will involve a loop. Then you can worry about comparing, which will also involve looping with if-else statements.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    I can't use loop since we haven't covered it in class.

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Well then I would suggest reading in one number. Set that initial first number equal to smallest integer. After that its just a matter of reading in 9 other numbers manually and comparing each one to the smallest_variable.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Read the first number and save it in a variable named min for example.Then read the second variable.If it's value is lees than min ,then set min to the number you just read and so on

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    How do you save it?

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    So do I ask the user for the integers one at a time or all at the same time?

  8. #8
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    One at a time. Scanf after Scanf that is assuming you are not allowed to use loops.

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Do you have to declare all variables in the beginning or can you declare it anywhere (just before you use it)?

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It depends on the standard you are using.With the old one you have to declare them at top.With the new one - C99 - you can declare them anywhere just before using them

  11. #11
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    For C89, you must declare all of your variables at the beginning of a scope block. It depends if you are compiling under that standard, however I would still suggest declaring all variables at beginning to adhere to original C standard.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    I know I should scanf one at a time, but what about printf for prompting the user to enter 10 integers, should i do that 1 at a time as well?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could print the message to them, to enter 10 numbers. Best to count the number they have entered, as they are being entered though, because user's are notorious for giving bad input from the keyboard.

    I you sure you can't use a loop or call a function to help with this? This is worse than watching little short legged dachshunds dogs, in a race.
    Last edited by Adak; 10-07-2012 at 11:47 AM.

  14. #14
    Registered User
    Join Date
    Oct 2012
    Posts
    13
    Yea, I can't use loop, only can use if, else, switch.

  15. #15
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Try this:
    prompt user for input
    store input in variable
    print out the variable on the screen.

    The best programs are written a little bit at a time - The pseudo written above is a great place to start.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determine the largest and smallest of three integers
    By SilentPirate007 in forum C Programming
    Replies: 15
    Last Post: 03-21-2010, 01:25 AM
  2. Need to develop a code to obtain the two smallest integers.
    By edwinhndz17 in forum C Programming
    Replies: 13
    Last Post: 09-12-2009, 09:18 PM
  3. selfstudy: Remove all smallest integers
    By tongboy in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2006, 05:14 AM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM