At least one element in an array is true ANSI C

Dejo

Level-1
Beiträge
14
Reaktionspunkte
1
Zuviel Werbung?
-> Hier kostenlos registrieren
Hello all,

I have a little problem and I hope you can help me...
I want to write a little function that can check all the ellements in the array all the time. For ex:
void _CYCLIC ProgramCyclic(void)
int a;
testboolarray : ARRAY[0..5] OF BOOL;
{
for(i=0; i<=5; i++)
{
if(testboolarray == false)
a = 5;
else
a = 10;
}
}
but with this code if ALL the elements of testboolarray are TRUE, only then I will receive a=10.
And I want if ANY element of testboolarray if it is TRUE, then to become a=10.

Can someone help me?

Thanks
 
That's not ANSI C, at least not the variable declaration (I guess it's B&R C).

I guess you're accessing the array with index, and only the forum software is removing the [ i ]. just use the code tag to avoid this problem.

You have to add a break statement after a=10; and exit the for loop when you've found one element with value of true

You can reduce it to:

C:
a = 5;
for (i = 0; i <= 5; i++) {
  if (testboolarray[i) == true) {
    a = 10;
    break;
  }
}
 
Zuviel Werbung?
-> Hier kostenlos registrieren
That's not ANSI C, at least not the variable declaration (I guess it's B&R C).

I guess you're accessing the array with index, and only the forum software is removing the [ i ]. just use the code tag to avoid this problem.

You have to add a break statement after a=10; and exit the for loop when you've found one element with value of true

You can reduce it to:

C:
a = 5;
for (i = 0; i <= 5; i++) {
  if (testboolarray[i) == true) {
    a = 10;
    break;
  }
}
Hi Thomas_v21,
thanks for your answer and yes you are right, it's a B&R PLC, that is why the C programming so mixeded...it is somekind of a C addopted for PLC :)
However, I have tried your solution and it is working!

Thanks about that! :)

Happy Holidays!
 
Zuletzt bearbeitet:
Zurück
Oben