Help with a condition function

This is a discussion on Help with a condition function within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I need a little help. I need a function that will do the following: I have two variables ...

  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
    20,981
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,662
    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 01:21 PM.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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, 04:45 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

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