Please people help me. I really need your help.

I don't know how to solve this task.
I tried many times, but for nothing. Can anyone solve it and explain me?

Here is the task:
(if possible, solve it without using any functions or some other complex things,
just arrays, loops..)

Write a program to prompt the user to enter an integer. The program should then determine whether the digits
in the number are perfectly descending, ascending or none.

examples:
986321 - perfectly descending digits in a number
1579 - perfectly ascending digits in a number
15792 - none

Please guys, one more time I have to say, Help me. :(
Thanks a lot.
Last edited on
"Prompt the user to enter an integer"
1. print out a prompt to the user (use cout)
2. create a variable to hold the user's input
3. read into the variable (use cin)

determine whether the digits
in the number are perfectly descending, ascending or none
The easiest way to get each digit of a number is to start with the least-significant-digit first (i.e. in the number 25478, get the 8). To get the least-significant-digit, use the modulo operator (%). It will give you the remainder of a division.
25478 % 10 = 8, because if you divide 25478 by 10, the remainder is 8. (We used 10 because we're considering 25478 as a base-10 number).

Now that you have the first number, get the next number by taking advantage of integer division. 25478 / 10 = 2547, because diving two integers discards the fractional part and leaves you with the whole part. Apply the same modulo trick above to the number 2547 and, voila, you'll get the number 7.

The point is, you can use this method with any number to get its digits one at a time. You should also define what it means if a number has only one digit. Is it ascending, descending or none? Or both?
Both..
I know all that, but always when I try to write it in loop, that doesn't work.
I really no idea how to solve it.
What have you tried? Can you explain your algorithm?
#include <iostream>
using namespace std;
int main () {

cout << "Enter a number: ";
int num, store, temp;
cin >> num;

store = num;

while(num > 0) {
num = num % 10;
temp = num;
store = store / 10;
}

}

Something like this, but it is not good... :/
And this is another way what I have done until now, it's good the first half of this task, second is wrong. Can you give me some advice maybe?


#include <iostream>
using namespace std;
int main () {

cout << "Enter a number: ";
int num, br(0);
cin >> num;

while(num > 0) {
num = num / 10;
br = br + 1;
}

cout << "You number has " << br << " digits " << endl;

/*
Above is the number of digits of entered number.
Move on further I tried to solve it using two loops and counting digits,
but I don't know.. This doesn't work...
*/

int arr[10] = {br};
for(int i = 0; i < br-1; i++) {
for(int j = i+1; j < br; j++){

if(arr[i] < arr[j]) {
cout << "ascending" << endl;
}
else if(arr[i]> arr[j]){
cout << "descending" << endl;
}
else {
cout << "None.";
}
}
}

return 0;
}
Let's keep it simple and consider multi-digit numbers only for the time being.

Let's say our number is 24589. We're going to get through this problem two digits at a time. We're going to have lastDigit and nextToLastDigit, which will represent the least- and next-to-least-significant digits, respectively. You remember how to get digits individually using modulo, right?
1
2
3
4
5
6
7
8
9
int theNum = 24589;

int lastDigit = theNum % 10; //this will be 9
theNum /= 10; //divide theNum by 10 and store the result in itself. theNum becomes 2458

int nextToLastDigit = theNum % 10; //this will be 8
theNum /= 10; //same story. theNum becomes 245.
//notice how we're using theNum to hold "the rest of the numbers"
//while we consider two numbers at a time 

Now that we have this arrangement, let's make a couple of flags that will help us keep track of whether we're ascending or descending. Now that we have two numbers isolated, we can compare them and initialize our flags appropriately.
1
2
bool isAscending = (nextToLastDigit < lastDigit);
bool isDescending = (nextToLastDigit > lastDigit);


We have an initial snapshot of what our state looks like based on the two least-significant digits. Now we must continue through the number and see if our ascending/descending trend continues. We'll do this as long as we have more digits left to evaluate (i.e. theNum hasn't been divided all the way down to zero). To do this, we're going to throw out lastDigit, shift the nextToLastDigit into lastDigit, and get a new nextToLastDigit.
1
2
3
4
5
6
7
8
//at this point, theNum is 245
while(theNum != 0) //while we still have something in our "rest of the numbers"
{
    lastDigit = nextToLastDigit; //move 8 into last digit
    nextToLastDigit = theNum % 10; //this will be 5
    theNum /= 10; //take 5 out of "the rest of the numbers", left with 24
    //...
}

With this new digit now available to us in the variable nextToLastDigit, we can see if our current trend (whatever it is) is continuing. If we decided earlier that the first two numbers indicate we're ascending, we need to check if we're still ascending after pulling in the next digit. Same thing for descending.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(...)
{
    //...code from previous step...
    
    if(isAscending && !(nextToLastDigit < lastDigit)
    {
        //we were ascending, but doing the digit check shows that's no longer the case
        isAscending = false;
        break; //go ahead and break, no use checking the rest of the number
    }
    if(isDescending && !(nextToLastDigit > lastDigit)
    {
        //we were descending, but doing the digit check shows that's no longer the case
        isDescending = false;
        break; //go ahead and break, no use checking the rest of the number
    }    
}

By the end of this loop, we can use the flags isAscending and isDescending to decide what to tell the user.
Thank you veeeery much on your great explanation. You realy have no idea how much you helped me.
On a side note because your question is answered:

Something I try to keep in mind is that a number is only a number when you try to do arithmetic to it. For a problem like this, as the "numbers" themselves are just representations and actually doing no maths at all. You could have easily loaded the number into a std::string, then iterate through to find which character is higher or lower than the last.

Keep these things in mind.
Thank you Megatron,
I didn't knew it. I will try to use that method next time.
No worries it's not something people think about when first starting. Often it is best to ask yourself how "You" would do it.

If I gave you the number 1563 and asked if it was perfectly ascending or descending, would you take the modulus and keep dividing by ten till you reach the end? Or would you read one number at a time and consider if it is higher or lower than the last? I know which one most humans would do. :]

Always try to keep things simple.
Topic archived. No new replies allowed.