Thread: qsort

  1. #1
    Unregistered
    Guest

    qsort

    the program gets 6 random numbers. i've been trying to use the qsort function to print them in ascending order.

    it seems to be printing the location in memory where they are stored rather than the numbers themselves..


    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h> //for seeding randomiser

    #define MAX 6

    int rnd(int range);
    void seedrnd(void);

    int intcmp(const void *v1, const void *v2);

    int main()
    {
    int x;
    int results[MAX];
    seedrnd(); //prepare random-number gen.
    //display 6 random numbers
    printf("Here are your six National Lottery numbers:\n\n");
    while (x<6)
    {
    results[0]=rand();
    {
    results[0]++;
    }

    }


    qsort(results, MAX, sizeof(results[0]), intcmp);

    for (x=0;x<6;x++)
    printf("\nresults[%d]", &results[x]);

    system("PAUSE");
    return 0;
    }

    int rnd(int range)
    {
    int r;

    r=(rand()%range)+1; //random numbers
    return(r);
    }

    void seedrnd(void) //seed the random number
    {

    srand((unsigned)time(NULL));
    }

    int intcmp(const void *v1, const void *v2)
    {
    return(*(int *)v1- *(int *)v2);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > it seems to be printing the location in memory where they are stored rather than the numbers themselves..
    Because that's what you asked it to do

    >printf("\nresults[%d]", &results[x]);

    Try
    printf("\nresults[%d]", results[x]);

  3. #3
    Unregistered
    Guest
    ok thanks i see that now...

    now i have the problem that the numbers generated are far too high..how can i edit the code so that the random numbers stay below 49?

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this:
    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <time.h> //for seeding randomiser
    
    #define MAX 6 
    
    int rnd(int range); 
    void seedrnd(void); 
    
    int intcmp(const void *v1, const void *v2); 
    
    int main() 
    { 
    int x; 
    int results[MAX]; 
    seedrnd(); //prepare random-number gen. 
    //display 6 random numbers 
    printf("Here are your six National Lottery numbers:\n\n"); 
    for (x=0; x<6; x++)
    {
    //Generates number from 1 to 48
    results[x]=(rand() % 48) + 1;
    } 
    
    qsort(results, MAX, sizeof(results[0]), intcmp); 
    
    for (x=0;x<6;x++) 
    printf("results[%d]\n", results[x]);
    
    system("PAUSE");
    return 0; 
    } 
    
    int rnd(int range) 
    { 
    int r; 
    
    r=(rand()%range)+1; //random numbers 
    return(r); 
    } 
    
    void seedrnd(void) //seed the random number 
    {
    srand((unsigned)time(NULL)); 
    } 
    
    int intcmp(const void *v1, const void *v2) 
    { 
    return(*(int *)v1- *(int *)v2); 
    }

  5. #5
    Unregistered
    Guest
    thanks, that worked perfect

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  3. trouble with qsort
    By qubit67 in forum C Programming
    Replies: 5
    Last Post: 04-29-2007, 10:23 PM
  4. Question About Using qsort
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 03:17 PM
  5. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 02:22 AM