min and max??

Hi all, i have a certain question.
Why that doesnt work?It gives zeros for min and max always.If i dont define them as zeros, there appear numbers like min - 4327867324 max - 1265764324

1
2
3
4
5
6
7
int min=0;
int max=0;
...
...
...
if (funct<min) funct=min;
if (funct>max) funct=max;

??
I looked at the topics here and in another forums, but i cant find an answer.
Last edited on

If you create a variable, and don't give it a value, it will have whatever value was in the memory space. Could be anything. Look up "uninitialised variable".

There is nothing special about making an int called min. It won't magically have a very low value. You have to set the value yourself. There are functions available that will give you the lowest possible number that a type can represent, and sometimes macros that are hard-coded with low values, but you still have to create the variable and set the value.
Last edited on
You set min and max to zero and then you never change those variables. What else are you expecting to happen?
I want to do this, but it doesnt works.When i define min as zero up there and after that - if (funct<min) funct=min, in the screen appears only "min value of the function is zero.Max- zero".
PanGalactic, I want to appear the min and max values of the function.
Last edited on
The code does what it's supposed to.

If fucnt is zero, it's already zero. If funct is less than zero, then it will get set to zero by this line:

if (funct<min) funct=min;

If funct isn't less tyhan zero, or zero, then it must be more than zero, so it will get set to zero by this line:

if (funct>max) funct=max;

No matter what funct begins as, you are setting funct to zero!
Last edited on
I know that my method doesnt work, thats why i ask you.What should i write so there will appear min and max.Not zeros?
Last edited on
If you don't want min or max to equal zero, maybe you could change them at the start, where you make them equal zero.

Are you saying you want funct to be the word "min"?
Last edited on
No, i want to make it so the program outputs the min and max of a function.
for example, the function is 1-x+2.
And the x changes every time.The min and max should be the minimum and maximum value of the function
Last edited on
funct=min;

This changes the value of "funct" and makes it whatever "min" is.

min=funct;

This changes the value of "min" and makes it whatever "funct" is.

Have you perhaps got them the wrong way round in your code?

Did you mean this:


1
2
if (funct<min) min=funct;
if (funct>max) max=funct;

Last edited on
OMG was is so stupid mistake??Thank you very much Moschops.
A final thought; what happens if you start by setting min to equal zero, and in the program the real min is actually, say, 5? The min will be reported as zero, but it was actually five. THis is bad.

There is something simple you can do to ensure this never happens (and similarly if you set max to zero, but in the function it is always actually lower than that), but I'll leave that to you to think about.
Topic archived. No new replies allowed.