Thread: read 2 numbers each and compare them

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    read 2 numbers each and compare them

    lets say you keep reading two int numbers every round
    you save the first int numbers which they are not same and the last two numbers which they are not
    so if you read this
    2, 3
    4,5
    5,6
    7,7
    you print 2,3 and 5,6 which one is the best algorithm to code if you are not able to use linked lists

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That's easy: record the first pair of numbers that is not the same. Record the most recent pair of numbers that is not the same. When you have finished processing all the pairs, the most recent pair of numbers that is not the same will also be the last pair that is not the same.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    thanks for the reply
    i have written the function which always saves the current numbers which are not the same but i cannot find how can i save the first numbers which are not the same as the function i have written always saves the current different numbers

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Set a flag variable when you first identify numbers that are not the same. By checking this flag, you thus avoid overwriting.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you save something when a condition is tue, then the value it holds at any given time will always be the last value where that condition was true.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2012, 06:54 PM
  2. Writing a macro to compare two numbers?
    By RichSelian in forum C Programming
    Replies: 17
    Last Post: 06-28-2011, 05:05 AM
  3. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  4. Replies: 16
    Last Post: 01-04-2007, 03:38 PM
  5. Replies: 1
    Last Post: 12-01-2002, 01:24 PM