Extracting and comparing numbers

Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last
• 02-28-2011
.C-Man.
Extracting and comparing numbers
Hey everyone, I'm new to C++ and know how to extract numbers (for example, to extract 1 from 1289371 I need to do modulo 10 [% 10] and I can do it in a while loop with dividing to keep using modulo on the numbers after) but I don't know how to extract a number and then extract the one after and compare to see which one is larger and then do the same thing again and find out which one is smaller and print them both out.

i.e. Input : 783214
prints out: largest number is 8
smallest number is 1

Can anyone help me?
Thanks.

My code so far:
Code:

``` int largeNum; int smallNum; int current1; int current2; while (num != 0){                                 current1 = num % 10;                 num /= 10;                 current2 = num % 10;                 if( current1 > current2 ){                         largeNum == current1;                 }                 if( current1 < current2 ){                         largeNum == current2;                 }             }                 cout << "The largest digit : " << largeNum << endl;         cout << "The smallest digit : " << smallNum << endl;```
• 02-28-2011
Elysia
Begin with this:
To do what you describe, what do you need to do? Break it down into steps.
Then take it a step further. Break it into smaller pieces. How can you make a computer understand how to do it?
• 02-28-2011
Salem
> largeNum == current1
1. You need =, not ==

2. One of the if statements should be testing, and then assigning smallNum

Also, you need to initialise say largeNum = 0, and smallNum = 10;
• 02-28-2011
.C-Man.
Quote:

Originally Posted by Salem
> largeNum == current1
1. You need =, not ==

2. One of the if statements should be testing, and then assigning smallNum

Also, you need to initialise say largeNum = 0, and smallNum = 10;

I realized that current2 was the same thing as current1... is there a way I can compare current1 to itself every time the loop restarts? because I can't just say
Code:

`if( current1 > current1)`
right?
• 02-28-2011
Elysia
Of course you can do that, but what's the point?
Again, what you have is small logic errors. Simple errors.
So either a) translate it to logic, fix it, then translate back, or
b) use a debugger.

You shouldn't get stuck on such trivial errors.
We'll help you learn the knowledge to squash them like bugs in the future, but you have to do it yourself!
• 02-28-2011
.C-Man.
I'm trying but I don't know what to do in order to fix it. That's why I'm on here trying to ask for at least tips or something because I'm new to C++ and I'm having hard time fixing these kinds of issues.
• 02-28-2011
Elysia
That's why I'm asking you to formulate a flowchart or pseudo code from your code. Something that describes the logic in the code. And something that describes the logic that you would need to do it.
• 02-28-2011
.C-Man.
With this:
Code:

```while (num != 0){                                 current1 = num % 10;                 num /= 10;                 current2 = num % 10;                 cout <<"curr:" << current1 << endl;                 cout <<"curr2:" << current2 << endl;                 if( current1 > current2 ){                         largeNum = current1;                 }                 if( current1 < current2 ){                         smallNum = current1;                 }             }         cout << "The largest digit : " << largeNum << endl;         cout << "The smallest digit : " << smallNum << endl;```
I got this:

Enter an integer: 9372
curr:2
curr2:7
curr:7
curr2:3
curr:3
curr2:9
curr:9
curr2:0
The largest digit : 9
The smallest digit : 3

So I know what curr and curr2 are doing each time the while loop is looped...
• 02-28-2011
Elysia
So you do. Yet the answer is not correct. Now why is that?
If you would just compare your current logic to the logic that is needed, you would have your answer.
• 02-28-2011
.C-Man.
Well in this case it is correct(for largest digit) but usually isn't because I'm just comparing the curr1 and curr2 from the same loop only and not saving it but I don't know how to do that... and I don't know how to incorporate smallNum into this either...
• 02-28-2011
Elysia
Your confusion lies from the fact that you don't have a proper logic in mind.
Formulate how you would do this first. Then it will all become clear.
• 02-28-2011
.C-Man.
Thanks, I guess...
• 02-28-2011
Elysia
I don't mean this in a bad way. Formulate your logic and check back if you get stuck.
We'll guide you on this path. We will walk it together and in the end, you will be smarter and be able to tackle these problems on your own in the future.
• 03-01-2011
.C-Man.
I've been working on this all week and It's due tomorrow. I know my logic is correct in normal situations but I don't know how to make it logical for the computer. Can someone please help me?
• 03-01-2011
Elysia
What is the logic that you use for normal situations?
Show 80 post(s) from this thread on one page
Page 1 of 4 1234 Last