Thread: Sorting!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    23

    Sorting!

    I have a script that will sort an input of integers that you want to sort, but this input is through terminal. I want to alter the script so that it sorts a set of integers that will be read from a text file. Can anyone post some code that would alter this code to do that?

    Code:
    #include <stdio.h>
     
    void bubble_sort(long [], long);
     
    int main()
    {
      long array[100], n, c, d, swap;
     
      printf("Enter number of elements\n");
      scanf("%ld", &n);
     
      printf("Enter %ld integers\n", n);
     
      for (c = 0; c < n; c++)
        scanf("%ld", &array[c]);
     
      bubble_sort(array, n);
     
      printf("Sorted list in ascending order:\n");
     
      for ( c = 0 ; c < n ; c++ )
         printf("%ld\n", array[c]);
     
      return 0;
    }
     
    void bubble_sort(long list[], long n)
    {
      long c, d, t;
     
      for (c = 0 ; c < ( n - 1 ); c++)
      {
        for (d = 0 ; d < n - c - 1; d++)
        {
          if (list[d] > list[d+1])
          {
            /* Swapping */
     
            t         = list[d];
            list[d]   = list[d+1];
            list[d+1] = t;
          }
        }
      }
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I can't just give away code, but I can give you some hints. You need to know the name of the file (perhaps it's constant, or you read it as a command line parameter or from the user). Second, you need to open that file for reading. Use the fopen function and make sure you verify it was opened correctly (check for NULL), and if it fails, print an error (perror or strerr/errno are good for this) and exit. Store the file pointer fopen returns in a variable, say 'fp'. Then, instead of scanf(...), you simply use fscanf(fp, ...) to read the numbers from the file you opened. Make sure to close the file when you're done.

    The nice thing about this is, you can easily make your program work with both file input and user/terminal input. If a file is specified, open that, and use that with fscanf. Otherwise, set your file pointer to stdin: fp = stdin; and continue using fscanf(fp, ...).

    EDIT: If you chose to support terminal and file input, you may want to disable printing the prompts if you're reading from a file.
    Last edited by anduril462; 11-14-2012 at 12:23 PM.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    23
    Quote Originally Posted by anduril462 View Post
    I can't just give away code, but I can give you some hints. You need to know the name of the file (perhaps it's constant, or you read it as a command line parameter or from the user). Second, you need to open that file for reading. Use the fopen function and make sure you verify it was opened correctly (check for NULL), and if it fails, print an error (perror or strerr/errno are good for this) and exit. Store the file pointer fopen returns in a variable, say 'fp'. Then, instead of scanf(...), you simply use fscanf(fp, ...) to read the numbers from the file you opened. Make sure to close the file when you're done.

    The nice thing about this is, you can easily make your program work with both file input and user/terminal input. If a file is specified, open that, and use that with fscanf. Otherwise, set your file pointer to stdin: fp = stdin; and continue using fscanf(fp, ...).

    EDIT: If you chose to support terminal and file input, you may want to disable printing the prompts if you're reading from a file.
    I understand you can't give away code, i meant more of the functions, not actually a whole working script. Whats the point of doing this if you don't learn!
    Thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 07-15-2012, 05:20 PM
  2. sorting
    By romy in forum C Programming
    Replies: 3
    Last Post: 10-21-2011, 10:31 PM
  3. Sorting
    By MarlonDean in forum C++ Programming
    Replies: 9
    Last Post: 05-16-2008, 01:56 AM
  4. Sorting
    By scottmanc in forum C++ Programming
    Replies: 4
    Last Post: 03-25-2003, 09:24 AM
  5. Sorting words with a fast, effincient sorting method
    By Unregistered in forum C++ Programming
    Replies: 19
    Last Post: 07-12-2002, 04:21 PM