计算3个动态表的中位数、平均值和标准差,并将它们添加到一个文件中。

问题描述 投票:1回答:1

我写了一个程序,读取一个文件,并创建3个动态数组,读取X、Y和RHO值。我想创建函数或程序来计算这些值的平均值、中位数和标准差,并将这些统计数据保存到文件中。我试图写它,但它不工作.程序。

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

struct MagicData
{
    double x;
    double y;
    double rho;
};

struct MagicDataArray
{
    size_t reserved;
    size_t count;
    struct MagicData *data;
};

void MagicDataArrayAppend(struct MagicDataArray *arr, struct MagicData* data)
{
     if (arr->count >= arr->reserved) arr->data = realloc(arr->data, sizeof(struct MagicData) * (arr->count * 3 / 2));
     arr->data[arr->count++] = *data;
}

struct MagicDataArray MagicDataArrayCreate(size_t reserved) {
     struct MagicDataArray result = {
           reserved,
           0,
           malloc(sizeof(struct MagicData) * reserved)
     };
     return result;
}

struct MagicDataArray MagicDataArrayReadF(FILE *f)
{
     struct MagicDataArray result = MagicDataArrayCreate(10);
     struct MagicData tmp;

     fscanf(f, "LP X Y RHO"); // ignore header
     while(fscanf(f, "%*d. %lf %lf %lf", &tmp.x, &tmp.y, &tmp.rho) == 3)
     {
          MagicDataArrayAppend(&result, &tmp);
     }

     return result;
}

int main(void)
{
    FILE *f = fopen("grades.txt", "r");

    struct MagicDataArray r = MagicDataArrayReadF(f);
    printf("%zd\n", r.count);

    for (size_t i=0; i < r.count; ++i)
    printf("%lf %lf %lf\n", r.data[i].x, r.data[i].y, r.data[i].rho);

    return EXIT_SUCCESS;
}

这是我文件的链接 https:/easyupload.io8swlt1。

c mean median standard-deviation
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.