-2

I have a VariableA (initialized with zero) , VariableB (initialized to constant integer X)

Variable A is increasing unevenly eg: +1, +3, +7, + 24.......

I would like to check if a variableA has reached a predefined threshold value of VariableB , I tried using Mod operator but since the variableA is increasing in a uneven format, I am not able to exactly check @ what point VariableA has crossed VariableB threshold.

Eg: Case 1

VariableA - 12 VariableB - 20 - Output - A has not touched the threshold yet

Case 2

VariableA - 19 VariableB - 20 - Output - A has not touched the threshold yet

Case 3 VariableA - 25 VariableB - 20 - Output - A has touched the current threshold

Case 4 VariableA - 41 VariableB - 20 - Output - A has touched the threshold

Case 5 VariableA - 48 VariableB - 20 - Output - A has not touched the next threshold, Since 20+20+20 has not happened.

Edits:

Case 5 VariableA - 48 VariableB - 20 - 1st Threshold will reach when VariableA is greater than or equal to Variable B (20), 2nd Threashold Will reach when variableA is greater than or equal to 2XVariableB(40), 3rd Threshold will reach when VariableA is greater than or equal to 3XVariableB(60)

Please suggest ...

Gopi
  • 19,784
  • 4
  • 24
  • 36
Rocker
  • 1
  • 2
  • Keeping `VariableB` constant is not the solution for me here. You will have to double `VariableB` if the value exceeds current threshold and compare. – Sadique Jan 22 '15 at 10:33
  • 3
    I don't think you've explained what "touching the threshold" is sufficiently well. I would say `VariableA>=VariableB` except for your case 5 which I don't understand. – Persixty Jan 22 '15 at 10:33
  • I don't understand... If variable B is 20 then as long as VariableA is > 20 it will have hit a certain threshold (multiple of VariableB), pelase explain a bit better what is the expected ouput condition. – Laurent S. Jan 22 '15 at 10:34
  • Use comparison operators!! Can you explain what is this `Since 20+20+20 has not happened.` – Gopi Jan 22 '15 at 10:34
  • ... umm... isn't that literally what you said already? – flight Jan 22 '15 at 10:43
  • I meant to say Case 5 VariableA - 48 VariableB - 20 - 1st Threshold will reach when VariableA is greater than or equal to Variable B (20), 2nd Threashold Will reach when variableA is greater than or equal to 2XVariableB(40), 3rd Threshold will reach when VariableA is greater than or equal to 3XVariableB(60) - Thats why i said 20+20+20 – Rocker Jan 22 '15 at 10:46
  • I think the question is ... "I want to know every time the value of A changes from less then a multiple of B to greater-than-or-equal-to a multiple of B". I submitted an answer based on this assumption. – hymie Jan 22 '15 at 15:45

3 Answers3

1
int a = 0, b = 20;
while(...)
{
    increment(); 
    if(a >= b) //Reached threshold
    {
        do_stuff();  
        b += 20;
    }
}

Your question is not very clear, but it might be something like this ?

(Answer edited, as I was wrong in a & b initialization)

Chostakovitch
  • 965
  • 1
  • 9
  • 33
1

If you can just use a function any time you want to increment a this is pretty simple.

int foo(int* a, int b, int increment){
    const int bar = *a / b;

    *a += increment;
    return bar - *a / b;
}

This will tell you not only if but also how many thresholds a has advanced across.

const int bar = foo(&VariableA, VariableB, 13);

if(bar == 1){
    printf("VariableA has touched %s threshold", (VariableA < VariableB * 2 ? "the current" : "a"));
}else if(bar > 1){
    printf("VariableA has touched %d thresholds", bar);
}else{
    printf("VariableA has not touched %s", (VariableA < VariableB * 2 ? "the threshold yet" : "the next threshold"));
}

This will also handle negative numbers in the increment updating when VariableA is below the first threshold again. It also handles negative numbers correctly, while % based solutions probably will not.

Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

(pseudocode, not tested)

int oldA, A, B, C;

oldA = A;
A += whatever;
C = A/B ; /* integer division on purpose */
if ( (oldA < C*B) && (A >= C*B) )
  printf "hit a threshhold\n";
hymie
  • 1,982
  • 1
  • 13
  • 18