waltermilner

Untitled

Dec 13th, 2014
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.95 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. /* is x in the array values, which has size elements? */
  4. int isIn(int x, int values[], int size)
  5. {
  6.     int pos;
  7.     for (pos=0; pos<size; pos++)
  8.         if (x==values[pos]) return 1;
  9.     return 0;
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15.     int a[5]= {2,2,3,3,5};
  16.     // how many elements in a?
  17.     int max =sizeof(a)/sizeof(a[0]);
  18.  
  19.     int pos;
  20.     int loc=0;
  21.     int temp[max]; // make a temporary array
  22.     for (pos=0; pos<max; pos++) // go through a
  23.     {
  24.         // if its not already there..
  25.         if ( !isIn(a[pos], temp, loc))
  26.         {
  27.             // copy into temp
  28.             temp[loc]=a[pos];
  29.             loc++; // we've moved in 'loc' values
  30.         }
  31.  
  32.     }
  33.  
  34. // make result array correct size
  35.     int result[loc];
  36.     for (pos=0; pos<loc; pos++)
  37.         // copy values into it
  38.         result[pos]=temp[pos];
  39.     // show result
  40.     for (pos=0; pos<loc; pos++)
  41.         printf("%d %d\n", pos, result[pos]);
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment