Write a program where the user enters numbers (doubles) on standard input. All of these numbers should be greater than zero, except for the last one, which should be -1. The -1 is a sentinel value and is how the user signals that he or she is done entering numbers.
Your program should read in all of these numbers, and print out one of three things after the user enters -1:

If the numbers are in ascending order (each number is greater than the previous one), then print "Ascending."
If the numbers are in descending order (each number is less than the previous one), then print "Descending."
If the numbers are neither ascending or descending, then print "No Order."
Ignore the final -1 in all three cases. Do print the period.

Your program must print its output after reading the -1, even though it may be able to determine that the input is in "No Order" after only a few numbers have been read.

You may assume that you always get legal input. You always get at least two numbers greater than zero, and the last number is always -1.


I am having problems with this for my cs102 class. Could anyone help?

Recommended Answers

All 6 Replies

what part do you not understand?

what part do you not understand?

How to do it really. It would be a huge help if you could show me the necessary code. Such as how to deal with the variables. I imagine it has a lot to do with if and while loops, but i'm kinda at a loss for how to do it.

This is the hint I was given.

Hint: Think about how you are going to figure this out before you start writing any code. You will need only three variables to do this -- one to hold the last number read, one to hold the current number, and one to hold what you know about them so far. Let's call them last, current and state. The first two will be doubles, and the last is an integer. You can assign various meanings to state:

If state is zero (the initial state), then we don't know what the input is yet.
If state is one, then the input is ascending.
If state is two, then the input is descending.
If state is three, then the input is in no order.

I just don't understand how to use the current and last integers. I don't know how to write out the statement needed to take in all the possible variables, and then simply cout "Ascending" and so on. Help is much appreciated.

How about starting with the basics.
Like the program which prompts for value then prints it out.

Then add to that program a second variable which allows the program to print out
- the current value
- the previous value.

Believe it or not, after those two steps you're only an if statement away from your answer.

I'm sorry, but I need it explained to me in lamens terms. I'm brand new to C++, so I really have no idea what to do still.

Huh?

Have you written the program which simply prints "hello, world"?
That's lesson 1.

What about the program which prompts you for your name, then prints "Hello jladd"?
That's lesson 2.

Because if you have, then it really isn't that much of a jump to get to the first step in my previous reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.