read 2 numbers each and compare them

This is a discussion on read 2 numbers each and compare them within the C Programming forums, part of the General Programming Boards category; lets say you keep reading two int numbers every round you save the first int numbers which they are not ...

  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
    21,589
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,589
    Set a flag variable when you first identify numbers that are not the same. By checking this flag, you thus avoid overwriting.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,299
    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, 05: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, 11:55 AM
  4. Replies: 16
    Last Post: 01-04-2007, 02:38 PM
  5. Replies: 1
    Last Post: 12-01-2002, 12:24 PM

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