-3

int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

How can I run through this array and check each int in the array against a number(Lets say 5) and if the array int number is less than 5 then set that int to 0 and if the array int number is greater than 5 set the array int number to 1.

I'm transferring RGB values to a PLC for processing. I would like to have the PC process the RGB data and just sent an array of 1,s and 0,s to the PLC. Here's what I have so far:

Array.Copy(Reddepthcall, RedmatchedItemsAngle, 1000);

RedmatchedItemsAngleFinal = Array.FindAll(
    RedmatchedItemsAngle,
    x => x >= lBound && x <= uBound
);

for (int ctr = 0; ctr < RedmatchedItemsAngleFinal.Length; ctr++) ;

int RedcountAngle = RedmatchedItemsAngleFinal.Length;
Luke Briggs
  • 3,745
  • 1
  • 14
  • 26
joev
  • 31
  • 2
  • What have you tried? Your problem seems pretty clear so writing a loop shouldn't be too difficult? – Luke Briggs Nov 27 '16 at 04:13
  • I tried a for each loop but how to I insert the compare and change function – joev Nov 27 '16 at 04:16
  • 1
    Put your code in the question - we can't help without that. – Luke Briggs Nov 27 '16 at 04:17
  • Array.Copy(Reddepthcall, RedmatchedItemsAngle, 1000); RedmatchedItemsAngleFinal = Array.FindAll(RedmatchedItemsAngle, x => x >= lBound && x <= uBound); for (int ctr = 0; ctr < RedmatchedItemsAngleFinal.Length; ctr++) ; int RedcountAngle = RedmatchedItemsAngleFinal.Length; – joev Nov 27 '16 at 04:23
  • this code runs through an array and just looks for items in between 2 setpoints – joev Nov 27 '16 at 04:25
  • 1
    @joev --> [edit here](http://stackoverflow.com/posts/40825682/edit) – Jim Nov 27 '16 at 04:25
  • I dont know where to start for the first question – joev Nov 27 '16 at 04:27
  • @joev I've put the code in your question - you should have an option to accept the edit. It seems like a *really unusual* approach to what the question asks, so I'm not sure if my understanding of what you want to do is right. – Luke Briggs Nov 27 '16 at 04:30
  • can somebody point me in the direction to find the answer the original question. I haven't built the code to do it because I don't know where to start or how to do it – joev Nov 27 '16 at 04:39

1 Answers1

1

Try this:

int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int num = 5;
for (int i = 0; i < arr1.Length; ++i)
    arr1[i] = arr1[i] < num ? 0 : 1;

Note: You have to decide what you want to do if the value equals 5...

Thejaka Maldeniya
  • 1,076
  • 1
  • 10
  • 20