Thread: choosing a number from a list of numbers

  1. #1

    Unhappy choosing a number from a list of numbers

    Need help in trying to write a piece of code that rounds a users integer input to the closest set of predefined numbers.


    defined values 30, 31, 36, 37

    user enters 35

    therefore I need some code that will choose the value 36, since its the closest match to the defined values. The only way I could think is a load of if statements. I've been out of touch with C for a while would really appeciate some help.


  2. #2
    Registered User
    Join Date
    Aug 2001
    if else statements aren't death, and there are a few things you can do to make life easier. If the number is <= 30 or >= 37, that's pretty simple. But you can consign the rest to single else-ifs by saying something to the effect of

    else if (n >= 31 && n <= 33)

    else if (n >= 34 && n <= 36)

    So you can just add the if and else if for 30 and 37, and you can get away with only 4 switches.

    Hope this helps.


  3. #3


    can you be a bit more specific when using the array method, I mean I know the principles behind what I need to do, but I dont have the programming experience to do it.

    I've defined a series of numbers in an array, what I need to do is compair a value the user has given and display the closest value in the array to the users.???

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas
    Salem pretty much told you how it's done, but here's a (possibly) working example:

    int array[5];
    int num;
    int current, 
    position = 0, 
    temp = 1000; 
    // fill array
    // get num
    for(i = 0; i < 5; i++)
    current = num - array[i];
     if(abs(current) < temp)
      temp = abs(current);
      position = i;
    printf("Closest Match: %i \n\n", array[position]);
    Last edited by Sebastiani; 11-15-2001 at 06:17 PM.
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Yeah, all it is is general math and comparing the outcome of subtracting and adding the results of the two closest choices.

    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM