Undefined Ference to MIN

Asked

Viewed 155 times

1

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

My program. c:(.text+0x8b): Undefined Reference to `MIN' [Error] Ld returned 1 Exit status

Follow 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;

}

2 answers

4


One very obvious problem is that you are not including the file that has the definition of MIN. But this will not solve the problem because this function (a macro actually) only accepts two arguments being passed and you are passing four. So you’d have to create a function of your own to handle all these arguments. There’s 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;
    scanf("%f\n%f\n%f\n%f\n", &a, &b, &c, &d);
    printf("%f", (a + b + c + d - multiMin(4, a, b, c, d)) / 3);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

If you don’t want to create a function just for this you can do the following:

int main(void) {
    float a, b, c, d;
    scanf("%f\n%f\n%f\n%f\n", &a, &b, &c, &d);
    printf("%f", (a + b + c + d - fmin(a, fmin(b, fmin(c, d)))) / 3);
}

I put in the Github for future reference.

It has to include the header <math.h>.

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

  • 1

    The idea is very interesting (va_arg, ...)+1; but in my case it is not giving the expected result. I proposed: int multiMin(int num, ...) and multiMin(4,a,b,c,d)

  • 2

    @Jjoao I had wrong the kind of return, actually the idea is that it would totally work with float. I don’t know if it was just a detail like that. If that’s what it was, it’s fixed, if it’s anything else, I’d like to know what’s wrong so I can fix it so I don’t keep telling everyone the wrong thing. Unfortunately, I’m not seeing any more trouble. If it is someone who only denied because he does not like me, then nobody will say anything and even if I solve, will not withdraw the -1, because the intention was this same.

  • @bigown -1+1=8 ;)

2

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

And by the way, a messy, un-generic solution,:

#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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.