Campbell Ritchie wrote:The problem about −1 comes from misunderstanding the loop in line 7. All you have to do is enter a non‑integer and the loop terminates. No need for −1 or break; or anything like that.
It should be possible to find largest and smallest with only one loop.Enter an int value: 736583 -1 -2 -3 -1 736583 -734 -2147483648 x
...
Second largest number is: -1 and it is located at: 1
Sorry for causing such confusion. I did say, “Once you correct those two problems”. I had corrected the problem about starting the values at 0, replacing 0 by 0x8000_0000.Dave Tolls wrote:How do you get -1 as a result, when both values start at 0? . . . .
Paul Clapham wrote:
Campbell Ritchie wrote:The problem about −1 comes from misunderstanding the loop in line 7. All you have to do is enter a non‑integer and the loop terminates. No need for −1 or break; or anything like that.
Right. But what if I wanted to use -1 as one of the numbers I'm inputting? Or some other negative numbers? The posted code won't handle those properly.
That sort of code suggests you are only dealing with natural numbers. I don't like to see such sentinel values used in case they are confused with real input. One day you will have a loop which should accept integers, positive and negative. If I had to exclude negative numbers, I would probably write a loop as follows. Note the extra pair of {} because i was declared outside the loop and the {} restrict its scope.What happens is that input reads an int and assigns it to i. That needs a pair of () to ensure the = operator runs before >=. Then the test for non‑negativity is done; i goes out of scope at line 8.pshivvy pat wrote:. . . -1 is there as a value which we were instructed to stop at. . . .
Campbell Ritchie wrote:
That sort of code suggests you are only dealing with natural numbers. . . .pshivvy pat wrote:. . . -1 is there as a value which we were instructed to stop at. . . .
Paul Clapham wrote:Okay. So I could enter -2 and -4 and -17 and your code would go on to find the second largest of those three numbers? What would it output?
pshivvy pat wrote:
I made a code which takes all user inputs and then adds it into an array list. I am fine with what I have there. I am confused as to why the for loop returns the largest number even though my logic tells me it should return the second smallest one. Any help is appreciated!
Mohammed Al-otaibi wrote:. . . it is working fine but with delibrate number entry
it gives erroneous results with other mixed values
pshivvy pat wrote:. . . Could you tell me a set your tried?
So an input comprising only negative numbers will not cause it to return the second largest input. In that case, you have been given instructions which produce incorrect results from some inputs.pshivvy pat wrote:. . . It returns 0 if only negative values are entered . . .
All things are lawful, but not all things are profitable.
Dave Tolls has already said that.Mohammed Al-otaibi wrote:and from mental math and logic point you should alter two lines: . . . .
What does that mean? It doesn't add anything useful to the discussion..the se one should retail old largest value, not updated one.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |