choosing a number from a list of numbers

This is a discussion on choosing a number from a list of numbers within the C Programming forums, part of the General Programming Boards category; Need help in trying to write a piece of code that rounds a users integer input to the closest set ...

  1. #1
    Unregistered
    Guest

    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.

    e.g.

    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.

    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    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)
    {
    printf("31");
    }

    else if (n >= 34 && n <= 36)
    {
    printf("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.

    starX
    www.axisoftime.com
    ---------------
    starX
    www.axisoftime.com

  3. #3
    Unregistered
    Guest

    Unhappy

    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
    Location
    Waterloo, Texas
    Posts
    5,659
    Salem pretty much told you how it's done, but here's a (possibly) working example:

    Code:
    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 05:17 PM.



    ITSA
    Socket Library!

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

    --Garfield
    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, 12:21 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 04: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, 03:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21