pointers, arrays and sorting 
Author Message
 pointers, arrays and sorting

Does anyone know how to use pointers to sort an array of positive
integers?

This is what I have done so far?

int main ()
{
        #define MAXVAL 100

        int table_fill(int a[], int max);
        void sort(int a[], int max);

        void print_table(int a[], int max);
        int t[MAXVAL];
        int n = table_fill(t, MAXVAL);

        print_table(t, n);
        return 0;

Quote:
}

#include <stdio.h>

int table_fill(int a[], int max)
{
        int *ptr = a;
        int *endptr = ptr + max;
        int r;

        for(ptr = a; (r = scanf("%i", ptr)) != EOF && *ptr >= 0 && ptr <
endptr;
                 sort(a[], max), ptr++)
        {
                if (r != 1)
                        break;
                if (ptr == endptr)
                        break;
        }

        return (int)(ptr - a);

Quote:
}

void sort(int a[], int max)
{
        int *ptr = max;

void  print_table(int a[], int max)
{
        int *endptr = a + max;
        int *ptr = a;

        while (ptr < endptr)
        {
                printf("%i\n", *ptr);
                ptr++;
        }

Quote:
}

Any help will be appreciated.


Thu, 12 Aug 1999 03:00:00 GMT  
 pointers, arrays and sorting

Does anyone know how to use pointers to sort an array of positive
integers?

This is what I have done so far?

int main ()
{
        #define MAXVAL 100

        int table_fill(int a[], int max);
        void sort(int a[], int max);

        void print_table(int a[], int max);
        int t[MAXVAL];
        int n = table_fill(t, MAXVAL);

        print_table(t, n);
        return 0;

Quote:
}

#include <stdio.h>

int table_fill(int a[], int max)
{
        int *ptr = a;
        int *endptr = ptr + max;
        int r;

        for(ptr = a; (r = scanf("%i", ptr)) != EOF && *ptr >= 0 && ptr <
endptr;
                 sort(a[], max), ptr++)
        {
                if (r != 1)
                        break;
                if (ptr == endptr)
                        break;
        }

        return (int)(ptr - a);

Quote:
}

void sort(int a[], int max)
{
        int *ptr = max;

void  print_table(int a[], int max)
{
        int *endptr = a + max;
        int *ptr = a;

        while (ptr < endptr)
        {
                printf("%i\n", *ptr);
                ptr++;
        }

Quote:
}

Any help will be appreciated.




Thu, 12 Aug 1999 03:00:00 GMT  
 pointers, arrays and sorting


Quote:
> Does anyone know how to use pointers to sort an array of positive
> integers?

    I'd suggest using qsort() instead of pointers. For example...

        int     icmp(const void *p0,const void *p1)
        {
        int     *a = p0, *b = p1;
            if (*a < *b) return(-1);
            if (*a > *b) return(1);
            return(0);
        }

        void    sort(int *a,int max)
        {
            if (max > 1) qsort(a,max,sizeof(int),icmp);
        }



Thu, 19 Aug 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sorting array of pointers to int

2. Sorting an Array of pointers to structures

3. sorting an array using pointers

4. sorting , pointers and arrays

5. Sorting an array using pointers

6. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

7. Array of pointers, pointer to array...

8. array pointer/pointer array

9. arrays pointers array of pointers

10. merge and sort arrays into one array.

11. Pointer of Pointers was Pointer of arrays...

12. Pointers: return of pointer to array of pointers to main

 

 
Powered by phpBB® Forum Software