Filter Structs by Field Value for example Sensor Threshold

Code

#include <stdio.h>
#include <stdint.h>
#include <string.h>

typedef struct{
    char name[10]; 
    uint8_t value; 
}Sensor_t; 

void printf_above_threshold(Sensor_t sensor[], uint8_t n, uint8_t threshold){
    for(int i = 0; i<n; i++){
        if(sensor[i].value >= threshold){
            printf("%s ",sensor[i].name);
            printf("%hhu\n",sensor[i].value);
        }
    }
}

int main(){
    uint8_t n, threshold; 
    scanf("%hhu%hhu",&n ,&threshold); 
    if(n>100){
        printf("ditmemay");
        return 0;
    }

    Sensor_t sensor[100];
    for(uint8_t i=0; i<n; i++){
        scanf("%9s %hhu",sensor[i].name, &sensor[i].value); 
    }

    printf_above_threshold(sensor, n, threshold); 
    return 0;
}

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

4 50 T1 45 T2 67 T3 10 T4 90

Expected Output

T2 67 T4 90