Search code examples
c++functionstructvolatile

Using struct object for a binary search operation


I'm trying to use a struct object for a binary search count function and I'm trying to get the code compiling with no luck.

What the binary search count function is doing is that it's counting the number of values there are before the limit has been reached. I have to use structs instead of classes as the code is going onto a micro controller.

The error I get when I try to compile the code is "cannot convert argument 1 from 'volatile float' to float[]"

#include <iostream>

using namespace std;

#define DLS_MAX_DATAPOINTS 1200;

typedef struct TEST_SAMPLE
{
    float loadcell;     // loadcell (N)
} TEST_SAMPLE;


// See documentation in DATALOG.C for details
typedef struct BRAKETEST
{
    int dataflag;                                   // flags indicating what data is stored
    int dataPointsCount;                            // no of sample collected during test
    TEST_SAMPLE dataPoints[];       // data collection of sampled points
} BRAKETEST;

volatile BRAKETEST braketest;                   // test object holding test data



int binarySearchCount(float arr[], float n, float key)
{
    int left = 0, right = n;

    int mid;
    while (left < right)
    {
        mid = (right + left) >> 1;

        // Check if key is present in array 
        if (arr[mid] == key)
        {
            // If duplicates are present it returns 
            // the position of last element 
            while (mid + 1 < n && arr[mid + 1] == key)
                mid++;
            break;
        }

        // If key is smaller, ignore right half 
        else if (arr[mid] > key)
            right = mid;

        // If key is greater, ignore left half 
        else
            left = mid + 1;
    }

    // If key is not found in array then it will be 
    // before mid 
    while (mid > -1 && arr[mid] > key)
        mid--;

    // Return mid + 1 because of 0-based indexing 
    // of array 
    return (mid + 1);
}


int main()
{
    int braketest_trig_level_pedal = 30; 
    int count = 0;

    double n = sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
    float results = binarySearchCount(braketest.dataPoints[count].loadcell, n, braketest_trig_level_pedal);   // braketest.dataPoints[count].loadcell
}

Solution

  • You have to change the types:

    int binarySearchCount(volatile TEST_SAMPLE arr[], float n, float key)
    
    binarySearchCount(braketest.dataPoints, n, braketest_trig_level_pedal);
    

    And change accesses to arr[mid] to arr[mid].loadcell, like:

            if (arr[mid].loadcell == key)
    

    You size calculation is wrong then:

    sizeof(braketest.dataPoints[count].loadcell) / sizeof(braketest.dataPoints[0].loadcell);
    

    Should be:

        double n = sizeof(braketest.dataPoints) / sizeof(TEST_SAMPLE);
    

    And not double as binarySearchCount expects a float.

    With those changes it will return the searched element +1 (because of the return (mid + 1) if it finds the key or the last index (again +1).