Problem with arrays structures sorting.

This is a discussion on Problem with arrays structures sorting. within the C Programming forums, part of the General Programming Boards category; replace the return in cmp_name function with this: Code: in this function: int cmp_name(const void *a, const void *b) { ...

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    replace the return in cmp_name function with this:

    Code:
    in this function:
    
    int cmp_name(const void *a, const void *b)
    {
        return(strcmp(a,b));
        //return strcmp(((struct element*)a)->name, ((struct element*)b)->name);
    }
    
    This is an elegant program, but clearly several steps above where you are with C. Nice as it is, I'd look
    for code help in the future, that was more at your level, or just above it.
    
    I don't think you learn much from turning in someone else's program "whole hog". This program looks
    nothing like a program from a beginner.
    and it works ok, in very limited testing.
    Last edited by Adak; 02-08-2009 at 07:26 PM.

  2. #32
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by pitifulworm View Post
    nope, same problem occurs
    That's a completely meaningless response!
    You haven't demonstrated that you've even understood the advice given, you haven't answered the question, and you didn't say what the value of n was when you printed it out after the loop.
    I haven't got a clue what change you might have made.
    How about posting a sample of the contents of your data file too!

    You are definitely someone who needs to read "How to ask questions the smart way".
    Last edited by iMalc; 02-08-2009 at 10:15 PM.
    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"

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    She previously posted this as her input:

    a sample input is:

    Oxygen 15
    Carbon 60
    Hydrogen 2
    Nitrogen 20
    Boron 3
    Making the one line change I noted above, causes the program she posted, to work, in very limited testing.

  4. #34
    Registered User
    Join Date
    Feb 2009
    Posts
    21
    OP, a tactic I use when debugging is to comment out everything apart from main and return. Then slowly bring things back in to the code and see where the error lies.

    Another tactic I use when it compiles and runs but doesn't get all the way through is to simply put a few printf lines in and see which ones it prints and which ones it doesn't print.

    Good luck in your CSP

  5. #35
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Adak View Post
    replace the return in cmp_name function with this:

    Code:
    in this function:
    
    int cmp_name(const void *a, const void *b)
    {
        return(strcmp(a,b));
        //return strcmp(((struct element*)a)->name, ((struct element*)b)->name);
    }
    
    This is an elegant program, but clearly several steps above where you are with C. Nice as it is, I'd look
    for code help in the future, that was more at your level, or just above it.
    
    I don't think you learn much from turning in someone else's program "whole hog". This program looks
    nothing like a program from a beginner.
    and it works ok, in very limited testing.
    you're passing struct element pointers to strcmp. that's a really bad idea because now the comparison routine depends on the order of members in the struct. say after testing you find out that you can save some memory by putting the int member first.
    Code:
    struct element
    {
        int abund;
        char name[128];
    };
    now the comparison will be messed up and you have to chase down a bug that's completely unrelated to the change you made.

  6. #36
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm with meldreth here. There are many ways that this could go wrong - if you move it to C++ it would fall apart if you introduce a virtual function [assuming with no other changes].

    --
    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.

  7. #37
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm sure you could "break" any program a thousand different ways - but the version of Meldreth's program that the OP put up, wouldn't run in Turbo C, either.

    With that one line change, it now runs OK. If hand waving and what if's would fix it, I'd be all for them.

  8. #38
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    it's not about breaking a program a thousand different ways. it's about making those breaks easier to find. your way is hard to find if they happen. i don't use turbo crap anymore so i can't give you any hand waving to make the code work, but i *can* tell you from experience that your bug will be a royal pain in the ass to troubleshoot. you need to find something else that compiles on your compiler or get a compiler that understands c better.

  9. #39
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I didn't run your program as you posted it, but I thought it looked good.

    When the OP came back and said it wouldn't run, then I tried it out, and found a fix for the program the OP posted up. What the OP posted up was perhaps not quite the same as your program.

    I have two version of Visual C/C++ (6.0 and 9), and an Intel compiler as well. They all have their uses.

    Whether it's a program, or a car, it's more important to keep it running, than to keep it easy to service. That has to be a secondary consideration.

  10. #40
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by Adak
    Whether it's a program, or a car, it's more important to keep it running, than to keep it easy to service.
    and just like with a car, easy to service means lower maintenance costs to keep it running.

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so first of all, the code posted [before the first one before two iMalc posts back] is broken because it printf's forever the uninitialized variable s. Removing that, it fails to sort the values in "abundance order" with an access to 0x34. It sorts correctly in alphabetical order - at least with the given input above.

    Trying it again, it doesn't appear to fail, so I'm not sure what is going on there....

    --
    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.

  12. #42
    Registered User
    Join Date
    Feb 2009
    Posts
    7
    I think i have got it working now, thanks to all for you help. now i've got to puts a whole lot of ifs in if the user enters crap...man.

  13. #43
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by Adak View Post
    She previously posted this as her input:
    That'll teach me to make a last minute edit.
    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"

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. Problem with structures
    By dereach in forum C Programming
    Replies: 2
    Last Post: 10-30-2007, 08:22 AM
  4. problem with arrays and structures
    By gell10 in forum C++ Programming
    Replies: 7
    Last Post: 11-02-2003, 11:02 PM
  5. Sorting an Array of Structures
    By bob2509 in forum C++ Programming
    Replies: 7
    Last Post: 05-12-2002, 08:26 AM

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