Sort Program Help

This is a discussion on Sort Program Help within the C Programming forums, part of the General Programming Boards category; Hi, needing help with c programming. Im suppose to make a "poor mans" variation to the Sort function built into ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    48

    Sort Program Help

    Hi, needing help with c programming. Im suppose to make a "poor mans" variation to the Sort function built into unix. The program is suppose to read in a file and sort the contents of the file. So its a variation of the Unix Sort feature. I have remade the readLine function we were provided so that it doesnt use fgets. I need help on where to go from here, Not sure on how to make a sort function. Here are the reqirements of the program:
    Code:
    • Re-implement the readLine() function so that it no longer makes use of fgets(). Instead,
    process the input on a given line character-by-character.
    • Provide code which will create a data structure similar to argv to hold all of the words to be
    sorted. Use malloc() to ensure that each entry has just the required number of bytes needed
    to store the words. The final entry in your array should be the NULL pointer.
    • Implement a sort() function which will rearrange the words in sorted order. To swap two
    words in your array, note that only a pair of pointers need to move. The strings themselves,
    once established, will never move.
    Heres what i have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_LINES 1000   /* maximum number of reminders */
    #define WORD_LENGTH 10      /* max length of reminder message */
    
    int read_line(char str[], int n);
    
    int main(int argc, char *argv[])
    {
        char *lines[MAX_LINES];
        char ch, line = argv[1];
        char line[WORD_LENGTH + 1];
        bool lineOverflow;   /* not enough room in char array? */
        int day, i, j, num_lines = 0;
    
        fp = fopen(FILE_NAME, "r");
        if (fp == NULL) {
            fprintf(stderr, "Error: can't open %s for reading.\n", FILE_NAME);
            exit(EXIT_FAILURE);
        }
    
        FILE *fp = fopen(argv[1], "r");
        if (fp == NULL) {
        fprintf(stderr, "Error: can't open %s for reading.\n", argv[1]);
        exit(EXIT_FAILURE);
        }
    
        while(readLine(line, sizeof(line), fp, &lineOverflow) != NULL) {
        printf("%s", line);
        }
        
        fclose(fp);
      
    }
    return(0);
    }
        
    char *readLine(char *s, int n, FILE *fp, bool *overflow) {
    
        int length;
        int ch;
        int skipped;     /* the number of NON-newline characters skipped over
                         for lines too long to fit in the given char array */
        int i = 0;
    
        ch = getc(fp);   /* read a line */
        if (ch == EOF) {
          *overflow = false;
          return NULL;    /* either stream is at eof or there was a read error */
        }
        
        while(ch != '\n' && i < (n - 1)) {
          s[i++] = ch;
          ch = getc(fp);
        }
        
        /* read was successful:  did we get the entire line? */
        length = strlen(s);
        if (s[length - 1] == '\n') {
            s[length - 1] = '\0';    /* we don't want to store the newline */
            *overflow = false;
        }
        else {
            /* skip to end of current line */
            skipped = 0;
            while ((ch = getc(fp)) != '\n' && (ch != EOF))
                skipped++;
            *overflow = (skipped > 0);
        }
        return s;
    } 
    
    
    void Sort( ) {
    
    }
    Last edited by clmoore3; 02-28-2013 at 08:02 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Unix sort command uses an "R-way" Merge sort. That's a complex sort algorithm, since it adapts to the system it's running on, when it starts.

    My question to you is do you need to code up an R-way Merge sort, OR can you use any good one threaded/one process sort algorithm?

    So how "poor" is "poor"?

    Later, we'll get into the definition of what "is", is. < ROFL! >

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Poor means poooorrrr lol. It can be simple as possible, all it needs to do is sort the contents of a file. like a .txt that has pig\ncat\ndog\n as the contents, it will print the contents of the file sorted out.

    ---- just wanted to add, that that isnt my entire read line function, something got lost in copying and pasting, editing now---

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Ok updated my readLine() code, can anyone help me with a Sort function now? Suppose to just put the contents of a file in order then print it out in the terminal, i dont even have to output it back to the file or anything. readLine( ) is reading and storing a line of input.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    I think he mightve mentioned something about selection sort, but i cant find anything in my book about sorting, except searching and sorting utilities.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    Can anyone help?? Its crucial i have this done. and i dont know how to do it

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If someone enters sort at the command line ("CL"), and then enters a filename, the filename is printed - but the file is unchanged (this is in Windows, and I believe it's the same in Unix). Only if you direct the output to another file, will the file's contents be changed.

    So I think your program should write the sorted contents to another file - a temporary file, and display that file's contents when an explicit filename isn't entered. Then delete that temporary file.

    If output is directed to another file, then follow the same process, but send the data to the file as directed, instead of sending it to a temporary file.

    As far as the sort itself, any sort should be OK. Remember, you're sorting the entire line - not sorting the line of text's letters themselves:

    cat
    bat
    ant

    becomes:
    ant
    bat
    cat

    not:
    abt
    act
    ant


    Remember, you have to compare WHOLE LINES of text, so you can't use line1==line2 kind of logic. You need to use result=strcmp(line1,line2), and then check result variable - if it's >0 then line 1 is greater than line2, if it's 0, then the two lines are the same, if it's -1 then line1 is less than line2.

    There are a zillion example of folding strings in C, available for study, on the net. Be sure to remember that you are NOT sorting the letters in each line, you are sorting WHOLE LINES only.

    See what you can start out with. Don't expect code to start your sort function, from a programming forum. We don't do that. We help YOUR code, but we don't start it out. That's all up to you.
    Last edited by Adak; 02-28-2013 at 09:25 PM.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    48
    So something like this?

    Code:
    void Sort(char *s, int n, FILE *fp) {
        
       string line1 = readLine(char *s, int n, FILE *fp);
    
       while(readLine(line2, sizeof(line2), fp) !=NULL) {
           int result = strcmp(line1, line2);
    
           if(result > 0) {
           printf("%s", line1);
           printf("%s", line2);
           }
           else(result < 0) {
               printf("%s", line2);
               printf("%s", line1);
           }
    }

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Before you go further, get main() right enough to compile. I can't tell where FILE_NAME is declared. After you get the basics of main() working (you don't need all the details for how it will look, etc., but it must have all the functional parts).

    Then use a test file with good ol' test.txt having
    cat
    bat
    ant
    in it, and see if you can open it, and use readline() to get it. Print it to be sure. The line should wind up in a 2D array, having a pointer to each row of text in the file - and each row malloc'd to the right size (according to your assignment). I don't see a malloc in there yet.

    Get main() and readline() working, before you work on sorting. I don't believe you can work with argv[] - your program is supposed to mimic it, instead - as I understand it anyway.

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    333
    Just use a bubble sort to get the thing working. It's very simple to implement. Step throguh the array from 0 - N-2, if the elements i, i+1 are in revrse order, swap them and set a flag. When you do a pass that sets no flags, the data is sorted.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sort Program
    By clmoore3 in forum C Programming
    Replies: 1
    Last Post: 02-27-2013, 10:58 PM
  2. Help with sort program
    By leroyjenkens in forum C Programming
    Replies: 8
    Last Post: 06-20-2012, 02:03 PM
  3. quick sort program
    By rakeshkool27 in forum C Programming
    Replies: 7
    Last Post: 05-21-2010, 02:29 AM
  4. Help On A Quick Sort Program
    By nick4 in forum C++ Programming
    Replies: 11
    Last Post: 12-06-2004, 09:51 AM
  5. Shell Sort vs Heap Sort vs Quick Sort
    By mackol in forum C Programming
    Replies: 6
    Last Post: 11-22-2002, 07:05 PM

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