Home > Software design >  How to find the number of the elements of an array that are in a specific range of values
How to find the number of the elements of an array that are in a specific range of values

Time:05-19

So I what I specifically want is to find which elements of an array are between two different values, not two different indexes. The code so far looks like this:

#include <stdio.h>
#define SIZE 5
int main(void){
    float grades[SIZE];
    float g;
    int i;
    for (i=0;i<SIZE;i  ){
        printf("Enter a grade: ");
        scanf("%f", &g);
        grades[i]= g;
    }
    float a,b;
    printf("Enter a grade range: \n");
    scanf("%f %f", &a,&b);
    return 0;
}

What I want is to tell the program, after the last scanf, to search for the array's values that are between the numbers the user enters, and then print the number of said values.

CodePudding user response:

can you try this one. in this way it do not sort, but it will display values, that are between the numbers the user enters

#include <stdio.h>
#define SIZE 5
int main(void){
    float grades[SIZE];
    float g;
    int i;
    for (i=0;i<SIZE;i  ){
        printf("Enter a grade: ");
        scanf("%f", &g);
        grades[i]= g;
    }
    float a,b;
    printf("Enter a grade range: \n");
    scanf("%f %f", &a,&b);
    
    
    for (i=0;i<SIZE;i  ){
        if(a>b){
            if(grades[i]>=b && grades[i]<=a){
                printf("%f ", grades[i]);
            }
        }else{
            if(grades[i]>=a && grades[i]<=b){
                printf("%f ", grades[i]);
            }
        }
    }
    
    return 0;
}

CodePudding user response:

This should work:

#include <stdio.h>

#define ARRAY_LEN(x)            (sizeof(x) / sizeof(*(x)))

size_t count_elems(const float * const arr, size_t arr_size, float min, float max)
{
    size_t count = 0;

    for (size_t i = 0; i < arr_size; i  = 1)
    {
        if ((arr[i] >= min) && (arr[i] <= max))
        {
            count  = 1;
        }
    }

    return count;
}

int main()
{
    float a[5] = {1.0, 0.5, 2, 3, 4};

    printf("%lu\n", count_elems(a, ARRAY_LEN(a), 0.5, 3));

    return 0;
}

The way the function works is by receiving a pointer to the float array, and iterating element by element to see if it falls within the correct range. The reason it needs the arr_size is because it can't tell the array length by itself.


It's worth noting that ARRAY_LEN() should only be used on statically allocated arrays.

CodePudding user response:

First, you need to sort your array:

#include <stdio.h>
#define SIZE 5
//We need a utility function for our integrated qsort algorithm:
int cmpfunc (const void * a, const void * b) {
   return ( *(int*)a - *(int*)b );
}

int main(void){
    float grades[SIZE];
    float g;
    int i;
    for (i=0;i<SIZE;i  ){
        printf("Enter a grade: ");
        scanf("%f", &g);
        grades[i]= g;
    }
    float a,b;
    printf("Enter a grade range: \n");
    scanf("%f %f", &a,&b);

    //Call qsort() function
    qsort(grades, SIZE, sizeof(float), cmpfunc);
    
    //Now array grades[] is sorted
    int j = 0;
    for(j; grades[j] <= a && (j < SIZE); j  ){
    }
    //grades[j] >= a
    for(j;grades[j] <= b && (j < SIZE); j  ){
        printf("%f ", grades[j]);
    }
    printf("\n");
    
    return 0;
}

By sorting your array, you create the opportunity to sequentially search for the first element, that is >= a, which means that your task will be done in O(n) complexity, worst case scenario.

For an even more optimal solution, you could also use a binary search.

After the first element that is needed to print is found, you can simply run through the rest of the elements, printing them if they are <= b, until you reach b or the end of the array (that is why those (j < SIZE) things are needed too.

  • Related