Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- /* is x in the array values, which has size elements? */
- int isIn(int x, int values[], int size)
- {
- int pos;
- for (pos=0; pos<size; pos++)
- if (x==values[pos]) return 1;
- return 0;
- }
- int main()
- {
- int a[5]= {2,2,3,3,5};
- // how many elements in a?
- int max =sizeof(a)/sizeof(a[0]);
- int pos;
- int loc=0;
- int temp[max]; // make a temporary array
- for (pos=0; pos<max; pos++) // go through a
- {
- // if its not already there..
- if ( !isIn(a[pos], temp, loc))
- {
- // copy into temp
- temp[loc]=a[pos];
- loc++; // we've moved in 'loc' values
- }
- }
- // make result array correct size
- int result[loc];
- for (pos=0; pos<loc; pos++)
- // copy values into it
- result[pos]=temp[pos];
- // show result
- for (pos=0; pos<loc; pos++)
- printf("%d %d\n", pos, result[pos]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment