I built a program that is cyclic called. Each time value T changes, and I’d like to compare value T and T from previous cycle, and do it for every cycle.
int T = externalsrc; //some external source
int prevT; //cannot do it because in the first cycle it will have no value when used in comparison
int prevT = 0; //cannot do it, because in every cycle prevT will be zero for my comparison
int comparison = prevT - T;
prevT = T;
How can I do it properly then? I tried also this, but still T is not declared here:
int T;
int prevT;
if (prevT != T)
prevT = 0;
else
prevT = externalsrc;
int comparison = prevT - T;
prevT = T;
Use your first answer, but declare
prevTasstaticand init to 0:…that way on every subsequent iteration the initialisation of
prevTis ignored, and the value is preserved from the last iteration.