Undefined reference to MIN

1

When compiling my discarded media program, the following error occurs. Remembering that I used MIN to discard the smallest value between a, b, c and d.

  

My program.c :( .text + 0x8b): undefined reference to 'MIN'   [Error] ld returned 1 exit status

The following is the code below:

#include <stdio.h>

int main (void)
{
    float a, b, c, d, res;

    scanf("%f\n%f\n%f\n%f\n", &a, &b, &c, &d);

    res = (a+b+c+d)- MIN(a,b,c,d)/3;
    printf("%f", res);

return 0;

}
    
asked by anonymous 07.04.2015 / 03:26

2 answers

4

A very obvious problem is that you are not including the file that has the MIN setting. But this will not solve the problem because this function (a macro actually) only accepts two arguments being passed and you are passing four. Then he would have to create a function of his own to deal with all these arguments. There is also a formula problem, so I put parentheses in the right place.

#include <stdio.h>
#include <stdarg.h>

float multiMin(int num, ...) {
    va_list lista;
    va_start(lista, num);
    float min = va_arg(lista, double);

    for (int i = 1; i < num; i++) {
        float item = va_arg(lista, double);
        if (item < min)
            min = item;
    }
    va_end(lista);
    return min;
}

int main(void) {
    float a, b, c, d, res;

    scanf("%f\n%f\n%f\n%f\n", &a, &b, &c, &d);
    res = (a + b + c + d - multiMin(4, a, b, c, d)) / 3;
    printf("%f", res);
    return 0;
}

See running on ideone .

If you do not want to create a function just for this you can do the following:

int main(void) {
    float a, b, c, d, res;

    scanf("%f\n%f\n%f\n%f\n", &a, &b, &c, &d);
    res = (a + b + c + d - fmin(a, fmin(b, fmin(c, d)))) / 3;
    printf("%f", res);
    return 0;
}

You must include the header <math.h> .

Note that I preferred to use fmin() than the MIN macro. It will work better. I see no reason to use the macro in modern compilers.

    
07.04.2015 / 03:41
2

Oops, ignore this answer: had not repaired but the previous solution already had a variant almost equal to this ...

And now an attacked solution and nothing generic,:

#define MIN2(x1,x2)        (x1 < x2 ? x1 : x2) 
#define MIN4(x1,x2,x3,x4)  MIN2( MIN2(x1,x2), MIN2(x3,x4))

#include <stdio.h>

int main(){
    float a, b, c, d;
    scanf("%f%f%f%f", &a, &b, &c, &d);
    printf("%f", (a + b + c + d - MIN4(a,b,c,d))/3);
    return 0;
}
    
07.04.2015 / 15:39