closest value

This is a discussion on closest value within the C Programming forums, part of the General Programming Boards category; Say you do a series of calculations and come up with a value. now you dont want to display the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    closest value

    Say you do a series of calculations and come up with a value.
    now you dont want to display the calculated value but you want to check to see which of the following values it is closest to: 1/4000, 1/2000, 1/1000, 1/500, 1/250, 1/125, 1/60, 1/30,
    1/15, 1/8, 1/4, 1/2, 1, 2, 4, 8, 15, 30, 60

    how do you do that in C

    Also can some one please tell me how to demonstrate that in psuedocode??

    thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Draw it on paper?
    Code:
    a------x------b---------c-----d----e
    Which is x closest to? The shorter distance: a-to-x or x-to-b, etc.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    but how do u do that in C??

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Subtract.

    You've got list of positions to compare with a current position. Go through the list and find the smallest difference.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    thanks dave but i realise i have to do that, i can even do it for one value but i dont know how to compare the whole list and find the smallest difference in C.

    i dont mean you to do it all for me, just need more help e.g. what command line do i use???

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    You've got some hints for a start, please post your initial attempt.

    Are you familiar with arrays? Loops? Functions?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int main (int argc, char * argv)
    {
    // Mainline Variable Declarations
    FILE * output = stdout;
    FILE * input = stdin;
    
    IF value-[1/400,...etc]=0
                   insertvalue == 1
    ELSE
    
                  //i have no idea how to find the least difference
    something like that??

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    In a cycle you do:
    Code:
    if (diff < currMinDiff)
    {
       currMinDiff = diff;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Directly showing you the exact lines is not a good idea , but I can tell you the algoirth that came to my head firstly.

    You can keep a control variable. Than you take the substraction of the number you want to compare and the number you have taken from the calculations you have made before. You put that into the control variable . In each loop you compare the control variable with the former one. If the new one is the shorter than the former you put the new one to the control variable.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It is probably worth considering that the values are non-linear. So you need to find the two values that are closest to your ideal value. Say you calculate 0.01 (1/100), but 1/100 isn't valid, it has to be either 1/125 or 1/60 - so now you need to see which of those is the "closest". Subtracting isn't really that good here either, you should probably use some sort of "proportional difference", e.g. the (y - x1) / x1 compared with (x2 - y) / x2, where x1 and x2 are the closest value above and below.

    You could of course cheat - if the value is less than 1/60, and above 1/125, then pick 1/125 - that's most likely a good approximation - or some fixed ratio of "allowed overexposure", e.g. if you come up with a value pretty close to 1/60, say up to 10% "faster", you use 1/60, then go to 1/125.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  2. The closest to a homework question without it being locked
    By DanFraser in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-28-2009, 02:25 AM
  3. Determine the closest departure time
    By Kyeong in forum C Programming
    Replies: 9
    Last Post: 10-07-2008, 08:06 PM
  4. Determining what object is closest:
    By Queatrix in forum C++ Programming
    Replies: 9
    Last Post: 08-16-2006, 11:18 PM
  5. Replies: 9
    Last Post: 11-20-2003, 07:55 PM

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