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

In summary, the user has a new problem where they need to switch between two formulas at certain intervals based on the value of time and T. The length of time the program is run is determined by the user, so the intervals will keep getting bigger. A possible solution could be to use the expressions (((time*2)/T)&1) for integer values or fmod((time * 2.0) / T, 2.0) for float or double values. The latter requires including math.h.
  • #1
odinx
9
0
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.
 
Technology news on Phys.org
  • #2
If time and T are integers, you could use (((time*2)/T)&1), which will return 0 or 1 depending on the interval.
 
  • #3
They aren't int values
 
  • #4
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().
 
  • #5


There are a few different ways you could approach this problem in c++. One option would be to use conditional statements (such as if/else or switch/case) to check the value of the time variable and determine which formula to use. For example, you could set up a loop that runs for the desired length of time and within that loop, use an if/else statement to check if the current value of time falls within the desired intervals. If it does, you can execute the appropriate formula, and if not, you can continue with the loop.

Another option would be to use a timer or clock function to keep track of the time and trigger a switch between the formulas at the desired intervals. This would involve setting up a timer or clock that increments at a set interval, and then using that timer value to determine which formula to use.

In either case, it would be helpful to break down the problem into smaller steps and think about how you can use conditional statements or timers to achieve the desired outcome. It may also be helpful to do some research on conditional statements and timers in c++ to get a better understanding of how they work and how you can implement them in your program.
 

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

1. How can I make my program execute different commands at specific time intervals in c++?

To make your program switch commands at certain intervals, you can use the sleep() function from the unistd.h library. This function will pause the execution of your program for the specified number of seconds, allowing you to implement time-based logic in your code.

2. Can I use a conditional statement to switch commands at specific bounds in c++?

Yes, you can use conditional statements such as if or switch to switch commands at specific bounds in c++. By using variables to keep track of the time intervals, you can use these statements to execute different commands based on the current time.

3. How do I set up a timer to switch commands at a specific time interval in c++?

You can use the chrono library in c++ to set up a timer. This library provides functions to get the current time and calculate elapsed time, which can be used to trigger the execution of different commands at specific intervals.

4. Can I use a loop to switch commands at certain intervals in c++?

Yes, you can use a loop such as while or for to switch commands at certain intervals in c++. By using the sleep() function within the loop, you can control the execution of your program and switch commands based on the elapsed time.

5. Is there a way to make my program switch commands at specific bounds without pausing the execution?

Yes, instead of using the sleep() function, you can use a while loop to continuously check the elapsed time and switch commands if a certain condition is met. This way, your program will continue to execute without pausing, but still switch commands at specific bounds.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
22
Views
952
  • Calculus and Beyond Homework Help
Replies
3
Views
354
  • Programming and Computer Science
Replies
2
Views
374
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
969
  • Introductory Physics Homework Help
Replies
10
Views
878
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
737
Back
Top