Code Check Requested! (Alphabetize entries?)

This is a discussion on Code Check Requested! (Alphabetize entries?) within the C Programming forums, part of the General Programming Boards category; Hey all. Tasked with creating a program that will take words from a file, alphabetize them, and write them to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    7

    Code Check Requested! (Alphabetize entries?)

    Hey all. Tasked with creating a program that will take words from a file, alphabetize them, and write them to an output file.

    i.e.

    Bananas
    Apples
    Falcon

    Should become:

    Apples
    Bananas
    Falcon

    I've got code that should theoretically work under the guidelines I've been given, and it arranges the values in some order, just not alphabetical. Thusly, I am confused.

    *EDIT*

    Haha matsp. I wouldn't call it unreadable but It's been a while. And I'm working with my younger cousin, who is currently taking C. I'm trying to give him a hand. Anyways, we'll rewrite the code to make it more manageable.
    Last edited by theonetruehero; 04-15-2008 at 10:36 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is this an entry for obfuscated C competition?

    I mean, this is hardly "readable code":
    Code:
    ++n; fscanf(fp,"%[^\n]%*c",t);
    sp=n==1?(char *)malloc(sizeof(char *)):realloc(sp,(n)*sizeof(char *))
    Or did you copy this from a post where someone else was "helping" you solve a problem, perhaps?

    In which case, I can tell you that it wasn't real help... I'd fail such a thing for a submission of homework for "unreadable, unmaintainable and unlikely to be originally by a student".

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

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,450
    Quote Originally Posted by theonetruehero View Post
    *EDIT*

    Haha matsp. I wouldn't call it unreadable but It's been a while. And I'm working with my younger cousin, who is currently taking C. I'm trying to give him a hand. Anyways, we'll rewrite the code to make it more manageable.
    Then all the better that you write properly indented, spaced, readable code
    http://cpwiki.sourceforge.net/Indentation
    http://cpwiki.sourceforge.net/User:Elysia/Indentation
    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. #4
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Quote Originally Posted by theonetruehero View Post
    Hey all. Tasked with creating a program that will take words from a file, alphabetize them, and write them to an output file.

    i.e.

    Bananas
    Apples
    Falcon

    Should become:

    Apples
    Bananas
    Falcon

    I've got code that should theoretically work under the guidelines I've been given, and it arranges the values in some order, just not alphabetical. Thusly, I am confused.

    *EDIT*

    Haha matsp. I wouldn't call it unreadable but It's been a while. And I'm working with my younger cousin, who is currently taking C. I'm trying to give him a hand. Anyways, we'll rewrite the code to make it more manageable.
    It is unreadable. Not in the sense that it is impossible to comprehend, but in the sense that it's too ugly to be bothered with.

    FYI this:

    Code:
    ++n; fscanf(fp,"%[^\n]%*c",t);
    sp=n==1?(char *)malloc(sizeof(char *)):realloc(sp,(n)*sizeof(char *))
    Should look something like:

    Code:
    { // Random bracket to emphasize the importance of indentation.
         ++n; 
    
         // Read characters until newline
         fscanf(fp, "%[^\n]%*c", t); 
         // Malloc data depending on the value of n.
         sp = (n==1) ? (char*)malloc(sizeof(char *)) : realloc(sp, n * sizeof(char *))
    }
    Comments are mandatory - especially with such cryptic code. The ones I wrote were just examples.
    Code in red needs to be removed / changed - here are the items in order of appearance:

    1. %*c ignores a character. If you're reading up to a newline, the character after that hardly needs to be ignored as it wouldn't be read in the first place?
    2. Don't cast malloc
    3. You don't want to allocate char pointers, but chars.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    33,117
    Not to mention that calling malloc before realloc is unnecessary (set p = NULL first), and that calling realloc as
    p = realloc( p, size );
    is a memory leak waiting to happen.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    It is very interesting how one can write such code:

    Code:
    +n; fscanf(fp,"%[^\n]%*c",t);
    sp=n==1?(char *)malloc(sizeof(char *)):realloc(sp,(n)*sizeof(char *))
    then turn around to ask how to do rudimentary tasks.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I would prefer:

    Code:
    { // Random bracket to emphasize the importance of indentation.
         ++n; 
    
         // Read characters until newline
         fscanf(fp, "%[^\n]%*c", t); 
         // Malloc data depending on the value of n.
         if (n==1)  
         {
             sp = malloc(sizeof(char *)) 
         }
         else 
         {
             sp = realloc(sp, n * sizeof(char *));
         }
    }
    Of course, the whole if-statement is not needed if you use sp = NULL and realloc(sp, n * sizeof(char *)), but that's a different matter.

    Using a terniary operator should be done when it's clear what the code does, e.g.
    Code:
    max = (a > b)?a:b;
    , but if you call a function on both sides of the ? and :, then I would say that a set of if-else is much more appropriate (with the possible exception that if it's part of for example a complex printf - but putting an if-else BEFORE such a printf is a much better option).

    I still think this was code posted on some other forum as a "cruel joke" to solve someones request for code to solve a problem. [I've made such cruel jokes myself on the odd occassion when the request is really trivial].

    --
    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. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. Can you check if my code and descriptions are correct?
    By Cyberman86 in forum C Programming
    Replies: 1
    Last Post: 02-06-2009, 12:22 PM
  3. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM
  4. Can anyone check this code
    By a_learner in forum C Programming
    Replies: 5
    Last Post: 10-06-2001, 02:41 PM
  5. check my code ( if statement, file existance )
    By Shadow in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 11:13 AM

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