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

AI Thread Summary
The discussion revolves around implementing a program in C++ that alternates between two formulas based on time intervals. The requirement is to apply formula 1 during the interval from time = 0 to time = (T/2) and formula 2 from time = (T/2) to time = T. This pattern continues, with the program switching formulas at every (T/2) interval, leading to progressively larger intervals as time increases. To achieve this, a suggestion is made to use the fmod() function from the math.h library to handle floating-point values. By calculating fmod((time * 2.0) / T, 2.0), the program can determine which formula to apply based on whether the remainder is less than 1 or greater than or equal to 1. This approach effectively manages the alternating intervals without needing a boolean command, accommodating user-defined lengths of time.
odinx
Messages
9
Reaction score
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
If time and T are integers, you could use (((time*2)/T)&1), which will return 0 or 1 depending on the interval.
 
They aren't int values
 
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().
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top