sorting an array using pointers 
Author Message
 sorting an array using pointers

How does one do this? Here is wat I have written so far.
thanks, :-) Randy
int main ()
{
        #define MAXVAL 100

        int table_fill(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;
                        ptr++)
        {
                if (r != 1)
                        break;
                if (ptr == endptr)
                        break;
        }

        return (int)(ptr - a);

Quote:
}

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

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

Quote:
}



Thu, 12 Aug 1999 03:00:00 GMT  
 sorting an array using pointers

How does one do this? Here is wat I have written so far.

int main ()
{
        #define MAXVAL 100

        int table_fill(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;
                        ptr++)
        {
                if (r != 1)
                        break;
                if (ptr == endptr)
                        break;
        }

        return (int)(ptr - a);

Quote:
}

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

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

Quote:
}



Thu, 12 Aug 1999 03:00:00 GMT  
 sorting an array using pointers


Quote:
>How does one do this? Here is wat I have written so far.

[somewhat odd looking, but otherwise reasonable, code omitted]

It depends: are you permitted to use the standard qsort() function?

If not, then you can just use any standard array-sort routine
using pointers instead of indexes just as you did with your
other functions.

If so, read its documentation.  Here is one possible comparison
function you can pass as its fourth option:
  int compare(const void *a, const void *b) {
    int aa = *(const int *)a;
    int bb = *(const int *)b;
    return (aa>bb) - (aa<bb);
  }

                --Ken Pizzini



Fri, 13 Aug 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Sorting an array using pointers

2. Can I sort a structured array using Array.Sort? USING C#

3. Can I sort a structured array using Array.Sort? USING C#

4. Can I sort a structured array using Array.Sort? Using C#

5. Sorting array of pointers to int

6. Sorting an Array of pointers to structures

7. pointers, arrays and sorting

8. sorting , pointers and arrays

9. soritng a const char[][50] array using sort ?

10. Help using qsort to sort pointers to a structure

11. I want to be able to sort the program using pointer and structure in C progr

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

 

 
Powered by phpBB® Forum Software