自定义书面比较功能的问题

问题描述 投票:0回答:2

有人可以快速查看我的代码,因为我就是找不到为什么我没有得到我想要的结果。这里的目标是从我给函数的数组中找到最大和最小的数字,如果最大和最小之间的差异大于 0.001,那么我应该得到一个 printf 语句,如果不是,那么另一个。

非常感谢您的帮助。

#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>

int alle_gleich(double*, int);
int pruefe_sort(double*, int);
double max_delta(double*, int);
// Vorschlag für Felddeklarationen und Initialisierungen :

int alle_gleich(double* array[], int anz)
{
    int i = 0;

    if (anz < 2)
        return -1;

    double* max = array[0];
    double* min = array[0];
    double summe=0;

    for (i; i < anz; i++)
    {
        if (array[i] > max)
            max = array[i];
        if (array[i] < min)
            min = array[i];
    }

    if ((max - min) > 0.001)
        return 1;
    else
        return 0;
}

int main(void)
{
    int n = 4;
    double werte_praktisch_gleich[] = { 9.9995, 10.0, 10.0002, 9.99991 };
    double werte_sort_0123[] = { 0, 1, 2, 3 };
    double werte_unsortiert[] = { 1, 3, 2, 0 };
    double werte_maxabw_0komma1[] = { 1.0, 1.05, 0.95, 1.01 };
    double werte_identisch;
    
    werte_identisch = alle_gleich(werte_praktisch_gleich, n);
    
    if (alle_gleich(werte_praktisch_gleich, n) == 1)
        printf("Die Werte sind identisch!\n");
    else
        printf("Die Werte sind nicht identisch!\n");
    
    return 0;
}
c max declaration min function-definition
2个回答
1
投票

几个问题...

  1. alle_gleich
    中,第一个参数应该是
    const double *
    not
    double *array[]
    。后者相当于
    double **array
    .
  2. alle_gleich
    中,
    max
    min
    应该只是
    double
    not
    double *
    .
  3. main
    中,无需调用函数两次。只需记住返回值并更改
    if
    .
  4. main
    中,有几个未使用的数组。
  5. alle_gleich
    计算
    min
    max
    values 但没有办法将它们返回给调用者。同样与
    summe
  6. 如果我们创建一个struct来保存它们,我们可以返回
    多个
    值。

这里是修复了错误的代码。它被概括为在[新]中返回多个值

struct

#define _CRT_SECURE_NO_WARINGS
#include <stdio.h>

struct minmax {
    double minval;                      // minimum value found
    int minidx;                         // index into array of min value

    double maxval;                      // maximum value found
    int maxidx;                         // index into array of max value

    double summe;                       // sum of array elements
    int ismatch;                        // 1=(min - max) > 0.001
};

int alle_gleich(const double *, int, struct minmax *);
int pruefe_sort(double *, int);
double max_delta(double *, int);

// Vorschlag für Felddeklarationen und Initialisierungen :

int
alle_gleich(const double *array, int anz, struct minmax *ret)
{
    int i = 0;
    double curval;

    if (anz < 1)
        return -1;

    // to simulate the original function, pass a null pointer
    struct minmax noarg;
    if (ret == NULL)
        ret = &noarg;

    curval = array[i];

    ret->minval = curval;
    ret->minidx = i;

    ret->maxval = curval;
    ret->maxidx = i;

    ret->summe = curval;

    for (i = 1;  i < anz;  i++) {
        curval = array[i];

        // calculate the sum
        ret->summe += curval;

        // get minimum value and the corresponding array index
        if (curval < ret->minval) {
            ret->minval = curval;
            ret->minidx = i;
        }

        // get maximum value and the corresponding array index
        if (curval > ret->maxval) {
            ret->maxval = curval;
            ret->maxidx = i;
        }
    }

    // calculate the return value
    ret->ismatch = ((ret->maxval - ret->minval) > 0.001);

    return ret->ismatch;
}

void
dotest(const double *arr,int n,const char *sym)
{

    printf("\n");
    printf("%s:\n",sym);

    struct minmax ret;
    int ismatch = alle_gleich(arr, n, &ret);

    if (ismatch)
        printf("Die Werte sind identisch!\n");
    else
        printf("Die Werte sind nicht identisch!\n");

    printf("Summe is %g\n",ret.summe);
    printf("Min is %g at index %d\n",ret.minval,ret.minidx);
    printf("Max is %g at index %d\n",ret.maxval,ret.maxidx);
}

#define DOTEST(_sym) \
    dotest(_sym,sizeof(_sym) / sizeof(_sym[0]),#_sym)

int
main(void)
{
    double werte_praktisch_gleich[] = { 9.9995, 10.0, 10.0002, 9.99991 };
    double werte_sort_0123[] = { 0, 1, 2, 3 };
    double werte_unsortiert[] = { 1, 3, 2, 0 };
    double werte_maxabw_0komma1[] = { 1.0, 1.05, 0.95, 1.01 };

    DOTEST(werte_praktisch_gleich);
    DOTEST(werte_sort_0123);
    DOTEST(werte_unsortiert);
    DOTEST(werte_maxabw_0komma1);

    return 0;
}

程序输出如下:


werte_praktisch_gleich:
Die Werte sind nicht identisch!
Summe is 39.9996
Min is 9.9995 at index 0
Max is 10.0002 at index 2

werte_sort_0123:
Die Werte sind identisch!
Summe is 6
Min is 0 at index 0
Max is 3 at index 3

werte_unsortiert:
Die Werte sind identisch!
Summe is 6
Min is 0 at index 3
Max is 3 at index 1

werte_maxabw_0komma1:
Die Werte sind identisch!
Summe is 4.01
Min is 0.95 at index 2
Max is 1.05 at index 1

1
投票

对于初学者来说,这些函数声明

int alle_gleich(double*, int);

int alle_gleich(double* array[], int anz)

不重合

调用函数

alle_gleich
两次是没有意义的

werte_identisch = alle_gleich(werte_praktisch_gleich, n);

if (alle_gleich(werte_praktisch_gleich, n) == 1)
//...

编译器应该为这些声明发出消息

double* max = array[0];
double* min = array[0];

因为有双精度初始化指针。

还有变量

summe

double summe=0;

未使用。

可以通过以下方式声明和定义函数。

int alle_gleich( const double array[], size_t n )
{
    if ( n < 2 ) return 0;

    double max = array[0];
    double min = array[0];

    for ( size_t i = 1; i < n; i++ )
    {
        if ( max < array[i] )
        {
            max = array[i];
        }
        else if ( array[i] < min )
        {
            min = array[i];
        }
    }

    return max - min > 0.001;
}

并使用英文单词作为标识符。否则你的代码是不可读的。

尽管编写只执行一项任务的函数几乎总是更好。也就是说,您可以编写一个函数来查找数组中的最小和最大元素。然后使用此信息来比较另一个代码中的最小值和最大值。

例如定义以下函数。

struct indices_pair
{
    size_t first;
    size_t second;
};

struct indices_pair minmax_element( const double a[], size_t n )
{
    struct indices_pair minmax = { .first = 0, .second = 0 };

    for ( size_t i = 1; i < n; i++ )
    {
        if ( minmax.second < a[i] )
        {
            minmax.second = i;
        }
        else if ( a[i] < minmax.first )
        {
            minmax.first = i;
        }
    }

    return minmax;
}

主要你可以写

struct indices_pair werte_identisch = minmax_element( werte_praktisch_gleich, n );

if ( werte_identisch.first != werte_identisch.second && werte_identisch.second - werte_identisch.first > 0.001 )
{
    puts( "Die Werte sind identisch!" );
}
else
{
    puts( "Die Werte sind nicht identisch!" );
}
© www.soinside.com 2019 - 2024. All rights reserved.