Thread: Need Help with Pointers...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Need Help with Pointers...

    I need some help with pointers. We are starting to learn pointers in class, and just got an assignment. The assignment is to make code so that a user inputs integers into an array, and at the end, it will sort them through "selection sort".

    I get the idea, and have the idea, but I can not seem to grasp how to translate the selection sort part of the code into using pointers. Right now it is in the form of arrays...

    If you could help that would be great. Also I may need help with the swap part, which is used in the selection sort to swap the numbers....

    Thanks

    Code:
    #include<stdio.h>
    #define N 20
    
    void sort(const int *p, int len);
    void swap(const int *p, int a, int b);
    
    void sort(const int *p, int len)
    {
      int x, y, index_of_min, temp;
    
      for(x=0; x<len; x++)
        {
          index_of_min = x;
    
          for(y=x; y<len; y++)
            {
              if(array[index_of_min] < array[y])
                  index_of_min = y;
            }
    
          temp = array[x];
    
          array[x] = array[index_of_min];
    
          array[index_of_min] = temp;
        }
    
    
    }
    
    void swap(const int *p, int a, int b)
    {
    
    
    
    
    }
    
    
    int main(void)
    {
    
      int i, length;
      int array[N] = {0};
    
      printf("\nInput the length of array: ");
      scanf("%d", &length);
    
      printf("Input the elements of array\n");
    
      for(i=0; i< length; i++)
        {
          printf("\nElement %d:  ", i);
          scanf("%d", &array[i]);
        }
    
      printf("\nThe input elements of the array are:\n");
    
      for(i=0; i< length; i++)
        {
          printf("%d", array[i]);
          printf(", ");
        }
    
      sort(array, length);
    
    return(0);
    
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Perhaps:
    Code:
    void swap(const int *p, int a, int b)
    {
      int temp = p[a];
      p[a] = p[b];
      p[b] = temp;
    }
    I didn't really look at your other code though so feel free to change it as necessary.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    It's far easier if you define find_min(const int * arr, size_t n, size_t i), swap() and sort()/selection_sort()

    Then you can clean up the sort()
    Code:
    int find_min(const int * array, size_t n, size_t i)
    {
       int min = array[i++];
       for(; i < n; i++)
       {
          if(array[i] < min)
             min = array[i];
       }
       return min;
    }
    As for using pointers, you can either sort an array of pointers (only move the pointers around), or just pass a pointer to the array of integers around ... which it seems you are already doing. Note that, foo[i] is the same as, *(foo + i) if you use pointers. You have a choice if you want to use pointer arithmetic or array indicies.
    Last edited by zacs7; 11-03-2008 at 07:30 PM.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    well, the thing is, the professor does not want us to use any [] notations...so I suppose even though you are pointing to it with the parameters and arguments, the [] in the function need to be replaced by a pointer name...

    That's the hard part about the selection sort translation for me...Any ideas?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Mkay.

    Code:
    void swap(const int *p, int a, int b)
    {
      int temp = *(p+a);
      *(p+a) = *(p+b);
      *(p+b) = temp;
    }
    And for zacs7's

    Code:
    void find_min(const int * array, size_t n, size_t i)
    {
       int min = *(array + (i++));
       for(; i < n; i++, array++)
       {
          if(*array < min)
             min = *array;
       }
       return min;
    }

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    oh ok, now I see how pointers are used without the []...

    now, I don't understand what the find min function is for?

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't write it... I only changed it to the specification your professor requested... But it looks like find_min() replaces one of your nested for-loops.

  8. #8
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because it allows you to see how the sort works more clearly...
    Code:
    void sort(int * array, size_t n)
    {
       size_t i;
       size_t min;
       
       for(i = 0; i < n; i++)
       {
          min = find_min(array, n, i);
          swap(array, min, i);
       }
    }
    
    size_t find_min(const int * array, size_t n, size_t i)
    {
       size_t minPos = i++;
       for(; i < n; i++)
       {
          if(*(array + i) < *(array + minPos))
             minPos = i;
       }
       return minPos;
    }
    Notice I actually gave a useful find_min() now . The first was purely conceptual.
    Last edited by zacs7; 11-03-2008 at 07:32 PM. Reason: I can't spell, I blame public schooling.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I like it Plus your compiler will more than likely inline the function so you aren't actually going to branch anywhere.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    what is the size_t ? I suppose that is some type like int or float?

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes, the largest unsigned integer type the compiler supports. So it's good to use it for indexes of arrays.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Yes, the largest unsigned integer type the compiler supports. So it's good to use it for indexes of arrays.
    Actually, it may not be the largest integer the compiler supports - for example in a 32-bit compiler that supports long long, it is still an unsigned 32-bit values. The definition of size_t is roughly "an integer type that can hold the range of addressable memory", so for a 32-bit system, it will be a 32-bit unsigned, and for a 64-bit system it will be a 64-bit unsigned.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    ok, so here is my code:

    Code:
    #include<stdio.h>
    #define N 50
    
    void sort(int* const p, int len);
    void swap(int* const p, int a, int b);
    
    void sort(int* const p, int len)
    {
      int i, x, y, index_of_min;
    
      for(x=0; x<len; x++)
        {
          index_of_min = x;
    
          for(y=x; y<len; y++)
            {
              if(*(p+index_of_min) > *(p+y))
                index_of_min = y;
    
              swap(p, x, index_of_min);
            }
        }
    
      printf("\nThe sorted elements of array are:\n");
    
      for(i=0; i<len; i++)
        {
          printf("&#37;d, ", *(p+i));
        }
    
    }
    
    void swap(int* const p, int x, int index_of_min)
    {
      int temp;
    
      temp = *(p+x);
    
      *(p+x) = *(p + index_of_min);
    
      *(p + index_of_min) = temp;
    
    }
    
    
    int main(void)
    {
    
      int i, length;
      int array[N] = {0};
    
      printf("\nInput the length of array: ");
      scanf("%d", &length);
    
      printf("Input the elements of array\n");
    
      for(i=0; i< length; i++)
        {
          printf("\nElement %d:  ", i);
          scanf("%d", &array[i]);
        }
    
      printf("\nThe input elements of the array are:\n");
    
      for(i=0; i< length; i++)
        {
          printf("%d", array[i]);
          printf(", ");
        }
    
      sort(array, length);
    
      printf("\n\n");
    
      return(0);
    
    }
    It satisfies some test examples...This is the example the professor gave us...

    6, 3, 7, 86, 2 ----> output is 2, 3, 6, 7, 86 (So this works)

    But some other test examples don't work and some do like

    100, 2, 24, 86, 1 -----> output is 1, 2, 86, 24, 100 (does not work)

    -30, 5, 23, -2, 100, -65 ----> output is -65, -30, -2, 5, 23, 100 (works)

    -100, 65, 2, -42, 16 -----> output is -100, 2, 16, -42, 65 (does not work)


    So I cannot see where there is a problem in my code. It seems like it is sort of random...

    PLEASE HELP!!!

    Thanks

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    If your prof didn't want you to use a[i] notation, then using *(a+i) is really just cheating, since it's exactly the same thing. Here's a rewrite using only pointers. It still has your bug, though!
    Code:
    void sort( int *p, int *pend)
    {
        for ( ; p < pend; ++p)
        {
            int *q, *pmin = p;
            for ( q = p; q < pend; ++q)
            {
                if (*pmin > *q) pmin = q;
                swap( p, pmin);  // rewrite swap
            }
        }
    }
    Call it like this: sort( array, array + length);

    Also, you should move the printing of the array from sort to main, where it belongs.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You're calling swap() a lot more times than necessary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM