samedi 8 août 2020

For-loop-condition (vector of ints). How to get the previous value?

I have vector of integers which is filled only by 1 or 0 values. What I am trying to make is that when the current value is 1 and previous/old is 0 or opposite if Current=0 and Previous=1, then to assign another variable(AvgCan) to 0.

I am trying to get from FOR condition previous value. However, if I try it the usual way I still get the same value all the time until the loop end. The issue is in the first if-statement.

int AvgCan = 0;
int OldAvgCan = 0;
int iteration = 0;
int iterationDecrease = 0;

for (int i = 0; i < resultINT.size(); i++)
{
  //myFileO << to_string(resultINT.at(i)) + "\n";
  cout << to_string((resultINT.at(i))) + " Current" + "\n";
  cout << to_string((resultINT.at(i - iteration))) + " Old" + "\n" << endl;
  cout << to_string(AvgCan) + "\n" << endl;

  iteration = i;
  iterationDecrease = i - 1;
  if ((resultINT.at(i)) != (resultINT.at(iteration - iterationDecrease)))
  {
     AvgCan = 0;
  }

  if ((resultINT.at(i)) == 1)
  {
     /*if ((resultINT.at(i- iteration)) != 1)
      {
          AvgCan = 0;
      }*/
     AvgCan++;
  }

  if ((resultINT.at(i)) == 0)
  {
     /*if ((resultINT.at(i- iteration))!=0 )
     {
         AvgCan = 0;
     }*/
     AvgCan--;
  }
  myFileO << to_string(AvgCan) + "\n";
}

As you can see I assigned iterator i to iteration variable and i - 1 to iterationDecrease. (I also tried i-- and similar possible ways.)

I simulated the data so the results are 1,1,1,1,0,0,0,0. When it is changing from 1 to 0 and it gets to the if condition, but each next iteration it still returns 1 like old values, even when it's 0.

enter image description here

I am adding also screenshot for better understanding. On the right side is output in the console.

Aucun commentaire:

Enregistrer un commentaire