Home > Blockchain >  How to compare a fixed value against multiple values and find if any one fails to match
How to compare a fixed value against multiple values and find if any one fails to match

Time:03-20

I have a fixed int value - 1050. I have around 50 dynamic values that I want to compare with the fixed value. So I compare it in a for loop. I have a public variable which I set as ok or notok depending on result. But my problem is that the value of the public variable is always the last value that I compared. Example, If I have the 20th dynamic value as 1000, it should return notok, but the value of the variable is always the last compared value. How do I set the variable to notok even if one/multiple of the values of dynamic variable doesnt match with fixed variable? I also display the total number of notok values in a listbox.

Here is what I have:

  string result;
  for(int i = 0; i < dynamicvalue.count; i  )
  {
    if(dynamicvalue[i] != setvalue)
    {
        result = "notok";
        listBox1.Items.Add(result);
    }
    else
    {
        result = "ok";
    }
   }

CodePudding user response:

To have "notok" if theres at least one not matching, one way to do it in plain code:

string result = "ok";
for(int i=0; i<dynamicvalue.count;   i)
    {
      if(dynamicvalue[i] != setvalue)
         {
          result = "notok";
          break;
         }
     }

CodePudding user response:

You can use .Any() from Linq,

Determines whether any element of a sequence exists or satisfies a condition.

string result =  dynamicvalue.Any(x => x == setValue) ? "Ok" : "Not Ok";

If you want to use for loop without a break statement, you are just increasing the time complexity of your code.

I will never recommend it, but if you want you can try the below code

string result = "Ok";
bool flag = true;
//This for loop will iterate for n times.
for(int i = 0; i < dynamicvalue.Count; i  )
{
  if(dynamicvalue[i] != setvalue && flag)
     {
      result = "Not Ok";
      flag = false;   //flag will help us to execute this block of code only once.
     }
 }

CodePudding user response:

Perhaps the most performant way to answer this would be to keep your numbers in a HashSet instead (make dynamicvalue a HashSet<int>), then it's:

dynamicvalue.Contains(setvalue) ? "ok" : "notok"

A HashSet can much more quickly answer "do you contain this value?" than a list/array can

CodePudding user response:

By the discussion going on in the comments I'm thinking that you want to go through all the elements in dynamicvalue and check all if any of them are ok or notok. If that is the case, you should turn result into an array. You get the last compared result because each time the cycle loops, the string gets assigned a new value all over again so the previous value gets discarded.

Is this what you want to do? I wrote it in c

  int setvalue = 1050;
  int notok = 0;
  int dynamicvalue[5] = {1, 2, 3, 1050, 4}; //for example
  string result[5];
  for (int i = 0; i < sizeof(dynamicvalue); i  ){
    if (dynamicvalue[i] != setvalue){
      result[i] = "notok";
      notok  ; //to keep track of notok
    }
    else{
      result[i] = "ok";
    }
  }

Afterwards if you cycle through the result array you will see that all the values were saved. I find it simpler to have an int variable to know how many times the result was notok

CodePudding user response:

You forgot to get the actual value within dynamicvalue: your test should be if (dynamicvalue[i] != setvalue).

EDIT: And add a break; after the result="ok"; instruction to break the loop, too.

EDIT 2: An above answer gives the corrected code using a break.

CodePudding user response:

I found a solution to this by reading @deminalla 's answer.

I added two more integers to work as counters and after the for loop I compare the values of these integers to get the final result.

Here's what I did:

 string result;
 int okcounter = 0;
 int notokcounter = 0;
 for(int i = 0; i < dynamicvalue.count; i  )
   {
      if(dynamicvalue[i] != setvalue)
        {
           notokcounter   ;
           listBox1.Items.Add(notokcounter);
        }
      else
        {
             okcounter  ;;
        }
    }
    if(notokcounter >=1)
    {
       result = "notok";
     }
     else if(okcounter == dynamicvalue.count)
   {
      result = "ok";
    }
  • Related