Qsort help needed

This is a discussion on Qsort help needed within the C Programming forums, part of the General Programming Boards category; Hey I'm writing this program that is supposed to take a file with random numbers in it, and sort it ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    1

    Qsort help needed

    Hey I'm writing this program that is supposed to take a file with random numbers in it, and sort it in ascending order. I can't figure out how to get the qsort right because right now all my program does is print the numbers. Here's my code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int compare (const void * a, const void * b);
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    
    int main(){
     
     char values[500];
     char buffer[500];
     int size = 100;
     int i = 0;
     int n;
     FILE *fp;
     fp = fopen("nums.bin", "r");
    
     while(fgets(buffer, size, fp) != NULL)
     {
       values[i] = (int)buffer;
       i++;
     }
    
     qsort(values, 4, sizeof(int), compare);
     for (n=0; n<sizeof(values); n++){
       printf("%d ", values[n]);
     }
     
     return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Code:
    int compare (const void * a, const void * b);
    int compare (const void * a, const void * b)
    {
      return ( *(int*)a - *(int*)b );
    }
    You only need the prototype if the actual function comes after main (or wherever you call it from).

    Code:
     char values[500];
     char buffer[500];
    You declare these as char arrays, but compare and sort integers. Pick one type (probably ints) and stick with it.
    Code:
     fp = fopen("nums.bin", "r");
    Check if fopen failed (fp is NULL) before reading from it. If it's NULL, print an error with perror and exit your program.
    Code:
     while(fgets(buffer, size, fp) != NULL)
     {
       values[i] = (int)buffer;
       i++;
     }
    fgets is for text. If nums.bin is binary, as the file extension suggests, you need to use fread instead of fgets. Also, values[i] = (int)buffer assigns every spot in values to contain the address of buffer. fread can read an integer directly into values[i], and if you use fgets, look into sscanf for converting the line of text to ints.

    Code:
     qsort(values, 4, sizeof(int), compare);
    You're telling it to only sort 4 elements. Also, again, you're using sizeof(int), but values is a char array.
    Code:
     for (n=0; n<sizeof(values); n++){
       printf("%d ", values[n]);
     }
    sizeof(values) will not give you what you want if you convert those arrays to ints (it only works for a char array by virtue of the fact that a char always has a size of 1). You need to give it the number of elements, not the number of bytes all the elements take up.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Apart fom all anduril462 said, your comparison function is wrong. You're telling it that those void pointers point to ints, but they don't, they point to chars. On platforms that don't allow misaligned reads, that code is going to crash.
    Your best bet is to read the documentation about the qsort function: qsort - C++ Reference
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort example
    By blob84 in forum C Programming
    Replies: 7
    Last Post: 10-14-2010, 04:36 AM
  2. qsort
    By chrismiceli in forum C Programming
    Replies: 5
    Last Post: 04-04-2004, 05:56 PM
  3. qsort
    By linuxdude in forum C Programming
    Replies: 4
    Last Post: 03-08-2004, 01:29 PM
  4. qsort
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-30-2001, 05:35 PM
  5. Qsort
    By Tombear in forum C Programming
    Replies: 1
    Last Post: 10-28-2001, 09:06 AM

Tags for this Thread


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