a sequence of positive integer terminated by -1 , is given write a program to determine whether these numbers are in ascending order asume there are at least two numbers .
All you will need to do, assuming they are an ordered list, is to compare the first three values, two at a time, and decide if they are increasing or decreasing.
why does he need 3 values for that?cant he do the same with just 2? and why is it mentioned that the sequence is terminated by -1? i mean there must be something else to that... @eSpeX
The -1 is so that you can continue the evaluations until the end. Take two numbers, a and b, and 2 binary flags, asc and des. Prime the system by taking in a and b. Then go to a loop. Test if a > b if yes, set des. If b > a set asc. If = set both. Set a to b, then take in the next number to b, if it is -1, end the loop. If it is not, loop.
@MicroBot the three integers was an effort to address that they did not say the numbers were unique. Further consideration brings up the fact that a number could be repeated more than two times. The logic however is the same in any of the cases, you compare two numbers and increment your way up the chain until you hit a -1, if your number is less than zero you know you have reached the end as @e.mccormick laid out.
ok thnx :)
Join our real-time social learning platform and learn together with your friends!