Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to make my program switch commands at certain intervals/bounds in c++

  1. Oct 1, 2012 #1
    I have a new problem where I have 2 formulas, and I need to tell my program that at certain intervals/parameters/bounds it needs to do one of the formulas and at other intervals it needs to do another one.

    To be more descriptive When ever my time=0 to time=(T/2) it should do formula 1, and whenever it is time=(T/2) to time=T it does formula 2. So looking at it another way my
    time= [0, (T/2)] it does formula 1 and at time=[(T/2), T] it does formula 2.

    Meaning that it switches positions/formulas at every (T/2).

    I just don't know what command I would do to make this happen in c++ at first I thought of using the bool command, but the thing is the length of time the program is run is determined by the user, so the longer the time the more intervals.

    Let me explain what I mean, after the intervals time=[(T/2), T] it will keep going to make a new interval of time=[T, (3T/2)], thus switching to formula 1 again, and the intervals will keep getting bigger because of the time used.

    So any ideas? Also I'm sorry if my explanation was horrid.
     
  2. jcsd
  3. Oct 1, 2012 #2

    rcgldr

    User Avatar
    Homework Helper

    If time and T are integers, you could use (((time*2)/T)&1), which will return 0 or 1 depending on the interval.
     
  4. Oct 1, 2012 #3
    They aren't int values
     
  5. Oct 1, 2012 #4

    rcgldr

    User Avatar
    Homework Helper

    For float or double, you can use fmod((time * 2.0) / T, 2.0) and check if the remainder is < 1. or >= 1.0.
    Include math.h in order to use fmod().
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to make my program switch commands at certain intervals/bounds in c++
Loading...