【问题标题】:Debugging Program, looking for data spike调试程序,寻找数据尖峰
【发布时间】:2017-11-20 01:40:09
【问题描述】:

所以我的程序正在寻找 100(psi) 的峰值,所以当它在数据点 0 处打印时,它应该比 -1 高 100。尽管我整天都在使用它,而且我的一生都无法让它捕捉到尖峰。我添加了条件句,把它们拿走了,无论如何我都可以完善它。有人可以查看我的代码并找到捕获峰值的好方法吗?

到目前为止我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>

#define MAX_CHANGE      100
#define ARRAY_SIZE      21

typedef struct data_slice
{
        int t;          // -> Time
        float tp;       // -> Valve pressure
    float tf;       // -> Sodium flow
    float tt;       // -> Sodium temp in Celsius
} data_slice;

// Function Declarations
void get_values(float * pressure, float * flow, float * temp);
void printIt(data_slice * data);
void initializeArray(data_slice * data);
bool spikeValueRecorded(data_slice * data, int outputIndex);


int main()
{
    srand((unsigned int)time(NULL));
    data_slice data[ARRAY_SIZE];
    int index = -1;

    while (1)
    {
        // Initialize the entire array
        initializeArray(data);

                    // If there's a spike.....
        if (spikeValueRecorded(data, index))
        {
            // Set the previous "time" in array to negatives
                int temp = index;
            if (index < MAX_CHANGE)
            //index = index + ARRAY_SIZE;
            for (int i = 0; i >= -10; --i)
            {
                data[temp].t = i;
                temp = temp - 1;
                if (temp < 0)
                    temp = temp + ARRAY_SIZE;
            }

                // Record for 10 more seconds
            for (int i = 0; i <= 10; ++i)
            {
                data[index].t = i;
                index = (index + 1) % ARRAY_SIZE; //         Increment the index of the circular array
                get_values(&data[index].tp, &data[index].tf,    &data[index].tt);       // "Record" the values
            }
            break;
        }
    }
    // Print the finished recording
    printIt(data);
}

// Return: void
// in - Values of the data_slice struct
//
// Description: The three values of the struct (data_slice) to be filled in
void get_values(float * pressure, float * flow, float * temp)
{
    *pressure   = (float)(rand() % (700 - 500 + 1) + 500);  //    Range: 500 - 700
    *flow       = (float)(rand() % (20 - 10 + 1) + 10);     //   Range: 10 - 20
    *temp       = (float)(rand() % (200 - 100 + 1) + 100);  //  Range: 100 - 200
}

// Return: void
// in - The array of data_slice
//
// Description: Prints the entire array being passed in
void printIt(data_slice * data)
{
    // Find the indice holding the time value of -10
     int indice = 0;
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        if (data[i].t == -10)
        {
            indice = i;
            break;
        }
    }

    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        printf("%i\t %f\t %f\t %f\n", data[indice].t, data[indice].tp,    data[indice].tf, data[indice].tt);
        indice = (indice + 1) % ARRAY_SIZE;
    }
}

// Return: void
// in - The array of data_slice
//
// Description: Initializes the entire array to random values and their   times to 0
void initializeArray(data_slice * data)
{
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        data[i].t = 0;
        get_values(&data[i].tp, &data[i].tf, &data[i].tt);
    }
}

// Return: boolean
// in - The array of data_slice
// out - Indice of the pressure spike
//
// Description: Returns true if a positive spike in pressure has been   recorded.
//      outputIndex will hold the 0-indice of the pressure spike, else -1
bool spikeValueRecorded(data_slice * data, int outputIndex)
{
    float oldValue = data[0].tp;
    for (int i = 0; i < ARRAY_SIZE; ++i)
    {
        if (data[i].tp - oldValue < MAX_CHANGE)
        {
            outputIndex = i;
            return true;
        }
    }
    outputIndex = -1;
    return false;
}

【问题讨论】:

    标签: c arrays debugging user-defined-functions


    【解决方案1】:

    您不能通过值传递参数进行输出。

    尝试改变

     bool spikeValueRecorded(data_slice * data, int outputIndex)
    

     bool spikeValueRecorded(data_slice * data, int *outputIndex) 
    

    然后分别将分配更新为*outputIndex = i;*outputIndex = -1;

    最后,在调用站点,通过引用传递index。 IE。, if (spikeValueRecorded(data, &amp;index))

    【讨论】:

    • 我认为这种比较也是倒退的:if (data[i].tp - oldValue &lt; MAX_CHANGE)。我想你的意思是&gt;
    猜你喜欢
    • 1970-01-01
    • 2017-02-09
    • 2015-02-10
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多