Thread: Help with a condition function

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    85

    Help with a condition function

    Hello everyone,


    I need a little help.

    I need a function that will do the following:

    I have two variables ( can have more if needed)

    If scale is 0-5 then ticks = 5
    If scale is 6-10 then ticks = 2.5
    If Scale is 11-15 then ticks is 1.25
    ...AND SO ON.

    I would code with conditionals, but It needs to be good forr an unlimited number of scales.

    I need to set ticcks based on scale


    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would code with conditionals, but It needs to be good forr an unlimited number of scales.
    You need to find out how to map the scale to the ticks.
    As a hint, notice that:
    scale = [0-5] => ticks = 5 / 1
    scale = [6-10] => ticks = 5 / 2
    scale = [11-15] => ticks = 5 / 4
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    85
    So it looks like I could do something like:
    Code:
    i = 0;
    
    for(j = 6;j<=scale;j=j+5)
    {
         i++;
    }
    
    ticks = 5/(2^i);

    Does this seem like it would work?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Does this seem like it would work?
    Gee, willickers. Your computer won't explode if you try it.
    Last edited by 7stud; 02-24-2006 at 02:21 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You could use the forumla:

    ticks = 5.0 / (1 << ((scale-1)/5));

    Of course, you'd have to treat the zero case differently, unless you shift your ranges to the more convenient 0-4, 5-9, 10-14, 15-19, ... and get rid of the "-1" above.

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Code:
    (2^i)
    This may not do what you're expecting. ^ is the operator for bitwise XOR and not for raising a number to a power. You use the math.h function pow() for that.
    But since you're using 2 as a base and integers as exponent you can use left bitwise shifting instead which is very fast but will not give the value expected if shifted out of bonds.
    "2^i" in math can be written as "1 << i" in C/C++

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM