user2345
user2345

Reputation: 126

Checking of values within Array in c

How can I search an array to find out whether it contains a set of values within the specific range of elements in the array (may be from position 2 to position 7, the first elements would be start of frame, length etc )

if input_data[1:5] == (45, 35, 230, 28)

if contains 45 
    send sensor 1 data;
if contains 35
    send sensor 2 data;
if contains 230 
    send sensor 3 data;
if contains 28
    send sensor 4 data;

It may contain any combinations (order) of the above values or doesn't contain any one of the elements and should stop sending data if the associated element is not found

I am trying to send some requested data based on the received elements

Updated !!

The data is received only once(only when updated), according to the Received data i need to send the requested outgoing data in the background,

if input_data[1:5] == (45, 35, 230, 28)
if contains 45 
    send_Sensordata1 = 1

if contains 28
    send_Sensordata4 = 1

in other function (running in background)

if send_Sensordata1 ==1 
   do something here(main sending stuff goes here)
if send_Sensordata4 ==1 
   do something here (main sending stuff goes here)

so in the first incoming data i get all the elements, then the requested data is sent, but if an element is missing in the next incoming data i need to stop sending outgoing data for that particular request (like toggle)

Upvotes: 2

Views: 88

Answers (2)

Grv
Grv

Reputation: 383

The array loop will start from 1 to get element 2 as in c array start from 0.

for(i=1; i<7; i++)
{
    if(array[i] == 45) 
        send sensor 1 data;
    else if(array[i] == 35)
        send sensor 2 data;
    else if(array[i] == 230) 
        send sensor 3 data;
    else if(array[i] == 28)
        send sensor 4 data;
}

Upvotes: 0

Haris
Haris

Reputation: 12272

You have to loop over the array and look for the those particular elements every time.

for(i=2; i<7; i++)
{
    if(array[i] == 45) 
        send sensor 1 data;
    else if(array[i] == 35)
        send sensor 2 data;
    else if(array[i] == 230) 
        send sensor 3 data;
    else if(array[i] == 28)
        send sensor 4 data;
}

Upvotes: 3

Related Questions