Saturday, May 22, 2010

I am trying to implement a program in C to find the min and max of some numbers but I get the following err:?

[Linked error]undefined reference to 'maximum(float, float)'


Id returned 1 exit status





The following is the code:





#include%26lt;stdio.h%26gt;





main()


{


int i,n;


float min, max, x;


float minimum(float, float), maximum(float, float);





printf("Input n: ");


scanf("%d", %26amp;n);





printf("\nInput %d real numbers: ", n);


scanf("%f", %26amp;x);





max = x;


min = x;





for(i = 2; i %26lt;= n ; i++)


{


scanf("%f", %26amp;x);


printf("X is %f\n\n", x);


min = minimum(min, x);


max = maximum(max, x);


}


printf("\n%s%.3f\n%s%.3f\n\n",


"Minimum value: ", min,


"Maximum value: ", max)


}








float minimum(float x, float y)


{


if(x %26lt; y)


return x;


else


return y;


}

I am trying to implement a program in C to find the min and max of some numbers but I get the following err:?
%26gt;%26gt;%26gt;float minimum(float, float), maximum(float, float);





These are function prototypes, not variables. Put them before (and outside of) main(). Also, split them up like:





float minimum(float, float);


float maximum(float, float);
Reply:Congratulations, "Guy".





Where you found 4 other people to vote for your piece-o-crap answer, I'll never know.





Obviously none of them has a C compiler or they would be able to tell that your "fix" fixes nothing. Report It

Reply:It appears you tried to define minimum and maximum as variables, if you move them out of all the functions and place them above main() it will be fine, like this:





float minimum(float, float);


float maximum(float, float);





main()


{


...


}
Reply:There is absolutely nothing syntactically wrong with declaring function prototypes locally instead of globally. There is also nothing syntactically wrong with declaring multiple function prototypes in the same statement as long as they have the same return type. This line that others have objected to:





float minimum(float, float), maximum(float, float);





is actually fine right where it is. It is not the source of your problem. I recommend that you leave alone.





If I were making these functions into a library, I would define the prototypes globally and separately, as others have suggested. But I would also do it in a #include header file instead of in this source file.





So, again, for this simple exercise you are NOT wrong to declare the prototypes the way you did.





HOWEVER, if you declare the prototypes, you also have to define the functions. I see that you have defined the "minimum()" function in the last 7 lines; however you did not define the "maximum()" function anywhere. That is the reason that you are getting the following error message:





[Linked error]undefined reference to 'maximum(float, float)'





Just define a maximum() function similar to your minimum() function and you should be okay.


No comments:

Post a Comment