Extracting and comparing numbers

This is a discussion on Extracting and comparing numbers within the C++ Programming forums, part of the General Programming Boards category; And if the reverse is true? And how would we code "then I would compare the numbers from the next ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    And if the reverse is true?
    And how would we code "then I would compare the numbers from the next pair to the 1st pair and see which one is the biggest and smallest"?
    Last edited by Elysia; 03-01-2011 at 05:24 AM.
    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.

  2. #32
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Code:
    if( current1 > current2 ){
    			largeNum = current1;
    			smallNum = current2;
    		}
    		if( current2 > current1 ){
    			largeNum = current2;
    			smallNum = current1;
    		}
    To check which one is the larger number and smaller number of the current pair... I don't know how to compare to the next pair though.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    But remember that you already checked which of the numbers in the first pass that was biggest and smallest and remembered that result (how did you remember it?)!
    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.

  4. #34
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    It was stored into largeNum and smallNum... Ohh, so do I compare smallNum and largeNum to current1 and current2?

  5. #35
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Exactly. So we're almost done.
    We have two cases... the first pass and the nth pass.
    Can you puzzle them together? It's possible.
    Hint: SmallNum and BigNum must of course be initialized before we can compare to them. So what should they be initialized to?
    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.

  6. #36
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    largeNum should be initialized to 0 and smallNum to 9... but why would we need to initialize them if we are making them current1 and current2 from this first pair?

  7. #37
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Well, we have two cases.
    In the first iteration, we want to compare current1 to current2 and assign BigNum from the biggest of current1 or current2.
    In the nth iteration, we want to compare BigNum to current1 and current2 and assign BigNum to current1 or current2, if any if bigger than BigNum.
    You could code both cases. Or you could integrate them into one case if you want. It just requires some thinking. Either way, it's up to you.
    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.

  8. #38
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Ok, so with
    Code:
    if( current1 > largeNum ){
    			largeNum = current1;
    			
    		}
    if( current2 > largeNum ){
    			largeNum = current2;
    		}
    I can find what the largest num is, but how do I integrate smallNum into this... or should I make different if statements for smallNum?

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Didn't you just post the logic required to find both max AND min a while ago?
    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.

  10. #40
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Ok, I figured it out for the most part :
    Code:
    while (num != 0){
    		
    		current1 = num % 10;
    		num /= 10;
    		cout <<"curr:" << current1 << endl;
    		if( current1 > largeNum ){
    			largeNum = current1;
    			
    		}
    		if( current1 < smallNum ){
    			smallNum = current1;
    		}
    		
    		}
    I decided that having curr2 was pointless because since smallNum and largeNum are initialized I can just compare them directly to curr1. However, the only issue I have now is when 'num' is 0. Then it only initializes the numbers and smallNum is printed out as 9 every time. How can I change this?

  11. #41
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Well, you could make sure the loop executes at least once. For example, a do ... while loop.
    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.

  12. #42
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    How about this?
    Code:
    if( num != 0 ){
    		while (num != 0){
    		
    		current1 = num % 10;
    		num /= 10;
    		cout <<"curr:" << current1 << endl;
    		if( current1 > largeNum ){
    			largeNum = current1;
    			
    		}
    		if( current1 < smallNum ){
    			smallNum = current1;
    		}
    		
    		}
    	}
    	else{
    		largeNum = 0;
    		smallNum = 0;
    	}

  13. #43
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Sure, it works. You can also verify that if you replace while with do while, it will also work.
    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.

  14. #44
    Registered User
    Join Date
    Oct 2010
    Posts
    79
    Also, if you don't mind, can you help me with one more thing? In this code I also need to find out how to find the total of all numbers extracted from 'num'.

  15. #45
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    First, formulate the logic.
    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.

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21