T
ja, geanu sowas habe ich gemeint, wobei die Schleife wohl einen Wert vergisst.
lucky_tux schrieb:
Hallo,
meinst du soetwas?
#include <stdlib.h>
#include <stdarg.h>
#include <float.h>
#include <stdio.h>
double dmax(int n, ...)
{
double max = DBL_MIN;
va_list a;
va_start(a, n);
while (--n > 0) {
double e = va_arg(a, double);
max = e > max ? e : max;
}
va_end(a);
return max;
}
int main()
{
double a = 2.2, b = 3.3, c = 5.5, d = 4.4;
printf("%f\n", dmax(4, a, b, c, d));
return EXIT_SUCCESS;
}
Oder möchtest du das Maximum aus einem Array bestimmen?
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <stdio.h>
void* max(void *base, size_t nmemb, size_t size,
int (*compar) (const void *, const void *))
{
register unsigned char *i, *r, *max;
i = r = max = base;
r += nmemb * size;
while (i < r) {
if (compar(i, max) > 0)
max = i;
i += size;
}
return max;
}
static int cmp(const void *p1, const void *p2)
{
double v1, v2;
v1 = *(const double *)p1;
v2 = *(const double *)p2;
if (fabsl(v1 - v2) < DBL_EPSILON)
return 0;
return v1 < v2 ? -1 : 1;
}
int main()
{
double v[] = { }, *m;
size_t n = sizeof(v) / sizeof(double);
m = max(v, n, sizeof(double), cmp);
printf("%f\n", *m);
free(m);
return EXIT_SUCCESS;
}
Grüße
Martin