Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop

This is a discussion on *Extracting and comparing numbers* within the **C++ Programming** forums, part of the General Programming Boards category; Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop...

- 03-01-2011 #16

- Join Date
- Oct 2010
- Posts
- 79

Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop

- 03-01-2011 #17

- Join Date
- Oct 2007
- Posts
- 24,158

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.

- 03-01-2011 #18

- Join Date
- Oct 2010
- Posts
- 79

Okay, what is missing?

- 03-01-2011 #19

- Join Date
- Oct 2007
- Posts
- 24,158

- 03-01-2011 #20

- Join Date
- Oct 2010
- Posts
- 79

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')

- 03-01-2011 #21

- Join Date
- Oct 2007
- Posts
- 24,158

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?

- 03-01-2011 #22

- Join Date
- Oct 2010
- Posts
- 79

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?

- 03-01-2011 #23

- Join Date
- Oct 2007
- Posts
- 24,158

- 03-01-2011 #24

- Join Date
- Oct 2010
- Posts
- 79

I understand that, but which part should I reword?

- 03-01-2011 #25

- Join Date
- Oct 2007
- Posts
- 24,158

- 03-01-2011 #26

- Join Date
- Oct 2010
- Posts
- 79

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.

- 03-01-2011 #27

- Join Date
- Oct 2007
- Posts
- 24,158

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?

- 03-01-2011 #28

- Join Date
- Oct 2010
- Posts
- 79

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.

- 03-01-2011 #29

- Join Date
- Oct 2007
- Posts
- 24,158

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?

- 03-01-2011 #30

- Join Date
- Oct 2010
- Posts
- 79

We would compare them like this:

Code:if( current1 > current2 ){ largeNum = current1; smallNum = current2; }

- Exactly how to get started with C++ (or C) today
- C Tutorial
- C++ Tutorial
- 5 ways you can learn to program faster
- The 5 Most Common Problems New Programmers Face
- How to set up a compiler
- 8 Common programming Mistakes
- What is C++11?
- Creating a game, from start to finish

- How to create a shared library on Linux with GCC - December 30, 2011
- Enum classes and nullptr in C++11 - November 27, 2011
- Learn about The Hash Table - November 20, 2011
- Rvalue References and Move Semantics in C++11 - November 13, 2011
- C and C++ for Java Programmers - November 5, 2011
- A Gentle Introduction to C++ IO Streams - October 10, 2011