Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop
Printable View
Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop
Okay, so, the logic is basically
While number is not 0
Get first number
Get second number
If first number > second number, remember first number
If first number < second number, remember second number
Loop
Right?
But there is something missing here. First you have to correct this algorithm since you want to pick out the largest and the smallest number.
Okay, what is missing?
Oh come on, you should be able to spot that.
Take a sample number and run it through that algorithm if you must. See what happens.
Take, say, 1234. What happens? Why does the algorithm fail? What is it that you're not taking into account?
It says the largest number is 1. It seems like it's printing out the comparison of curr1 and curr2 towards the very end of the loops ( if 1 > 0 ('curr1' > 'curr2')
1234
--> First number: 4
--> Second number: 3
4 > 3, remember 4
--> First number: 2
--> Second number: 1
2 -> 1, remember 2
Print largest number (remembered number): 2
Print smallest number (not remembered at all!): ?
I say to you, what is wrong with this algorithm?
It compares 2 at a time and saves the comparison at the end of the loop. That's why earlier in this post I asked if I could compare curr1 to itself everytime the while loops... is it possible?
And what is that going to do? It makes no sense. You need to fix the algorithm. Reword it so that it works.
I understand that, but which part should I reword?
The logic I posted before. Obviously the if statements are wrong, so they must be changed. If you change them to how you would do it in real life...
Yes I know that. In real life it would be easy because if you told a person to compare all the numbers and see which one is the biggest and smallest they would be able to do it in a second. However, I do not know how to make it so the computer will understand MY logic; that's what I'm having trouble with. I don't know how to reword it so the computer understands what I'm talking about. I've been trying different ways but they don't work and I can't think of anything that will make it correct. You say it is a simple error but for me it is a complex error and I have been trying to figure this out for a week.
By the way, sorry if I come off as rude, I'm just very frustrated.
I understand that it might be frustrating. So let's try this.
Let's make this a game.
I have a number. And I want you to help me find the biggest and smallest digit in the number.
So I peel off two digits, write them onto cards and hand them to you.
You look at them, then put them away.
I give you two new cards, and so we repeat.
How would you go about finding the biggest and smallest digit?
I would compare the first 2 numbers and see which one is biggest and smallest and remember, then I would compare the numbers from the next pair to the 1st pair and see which one is the biggest and smallest, and repeat for each pair of cards.
Exactly. And that is exactly what we must make the computer do.
So, if we were to code the first part "see which one is biggest and smallest and remember", do you have any ideas on how we would do that?
We need to break it down into simpler instructions. Logically, how would we know which two of two numbers is biggest and which is smallest?
We would compare them like this:
Code:if( current1 > current2 ){
largeNum = current1;
smallNum = current2;
}