Comparing adjacent values in a file

This is a discussion on Comparing adjacent values in a file within the C Programming forums, part of the General Programming Boards category; Hi all, after a fair amount of help from a couple of guys on here *tips hat to Adak* and ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    21

    Comparing adjacent values in a file

    Hi all,
    after a fair amount of help from a couple of guys on here *tips hat to Adak* and many hours hunched over my computer I've completed a problem I was set and added extra functions in for 'fun'.
    Unfortunately I look at the next problem and find myself at a loss for which function I need to use.

    I have two sets of data in a file, A and B. A is essentially just a numerical marker and B is the value assigned to that marker. B fluctuates, A rises +1 every time. I have managed to extract the data from the file (line by line) and then put it in a new file of the user's choice. However the task assigned was to do this yet in the middle, find out which are the peak values. For example, 0.1, 0.3, 0.5, 0.3, 0.7, 0.1, 0.2, 0.1.

    In this example 0.5, 0.7 and 0.2 are the peak values. I was hoping you guys would be able to inform me of a function (or hack of a function) that compares two adjacent values.

    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Forget about the coding part just yet; based on that dataset what do you think the algorithm should look like

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You wore out your welcome with me, Typhonius, or is it Typhos?

    Imagine my surprise to see your same question, that I already answered complete with code, over on the dev shed C forum, two days later.

    What's the deal here, you collect a bunch of answers to students assignments, and then sell them, or what?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    I have used structs to take the values from the file and I assume I need to do something like

    Code:
    for(i = 0; i < num - 1; i++)
       {
          for(j = i + 1; j < num; j++)
          if((i > j) && (i > k)
    I know that's horrible undefined code but I can't think of how to do it.
    With help I managed a sort yesterday and I used a similar notation to that.
    I think it was a bubble sort. It went through over and over comparing each element of the array to make an alphabetical list.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Quote Originally Posted by Adak View Post
    You wore out your welcome with me, Typhonius, or is it Typhos?

    Imagine my surprise to see your same question, that I already answered complete with code, over on the dev shed C forum, two days later.

    What's the deal here, you collect a bunch of answers to students assignments, and then sell them, or what?
    I'm not in devshed, I'm only on this one.
    Must be mistaken identity
    Sorry if there is confusion I'm genuinely only coding for me.

    Edit: It may be one of my classmates?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So you need to stop typing and think for a little bit, I guess. What does it look like to get a peak? What changes when you hit a peak? Think graphically.

    Once you have that then you can start looking at how to embody that in code.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    Graphically the sign of the differential changes.

    Going up a slope it would be a positive differential

    Going down a slope it will be a negative differential

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by typhonius View Post
    Graphically the sign of the differential changes.

    Going up a slope it would be a positive differential

    Going down a slope it will be a negative differential
    And you're done.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by typhonius View Post
    Graphically the sign of the differential changes.
    More specifically when the sign of the differential changes from + to - you have a peak. Changing from - to + would be a trough.

    So, is it all clear now?
    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"

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    I can understand that if we had the equation of the line, but we don't have that.
    We only have the values I mentioned :/

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by typhonius View Post
    I can understand that if we had the equation of the line, but we don't have that.
    We only have the values I mentioned :/
    So, how would you solve this problem if you had the numbers written down on cards, where you had to deal with two cards at any time (for example one in your left hand and one in your right hand)? Using this method should work for your example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

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