Thread: Extracting and comparing numbers

  1. #16
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Modulo 'num', check the number ('curr1'), modulo 'num', compare current number to last number ('curr2' to 'curr1'), loop

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Okay, what is missing?

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    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')

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    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?

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what is that going to do? It makes no sense. You need to fix the algorithm. Reword it so that it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    I understand that, but which part should I reword?

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    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.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    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.

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    We would compare them like this:
    Code:
    if( current1 > current2 ){
    			largeNum = current1;
    			smallNum = current2;
    		}

Popular pages Recent additions subscribe to a feed