Smallest and Largest of N numbers..

Pages: 12
Because it needs to compate to smaller values on your code.
:::
if u get value 20 its smaller than the infinite so it will be replaced
but if its 0
0 is not smaller than 20 so nothing will happen
hope that u understand
But someone told me that this is not the efficient way of programming..., because we've fix the value of a variable..
Xidike wrote:
But someone told me that ...

I'm not sure what you mean.

The reply from JLBorges pretty much gave a good answer to the problem.
http://www.cplusplus.com/forum/beginner/115708/#msg631682
We have fixed nothing. We are someone too and we tell you.

Option 1 was to use the first real value. Problem is that then you have to treat the first input differently from the others.

Option 2 is to initialize with a good default and treat every input the same way. Simpler code with less repetition means less errors.

numeric_limits<int>::max() is a good default for minimum.
There is no possibility that all of input would be larger than that. (10000000 is not a good default.)
If every input equals the default, then the default truly equals the smallest input.
If any input is smaller than default, then the minimum will be updated and you get a real answer.
or you can just use INT_MAX/2 or just int max for unsigned int
Last edited on
Topic archived. No new replies allowed.
Pages: 12