training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

dups.c (721B)


      1 /* Exercise 6.28 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 #define SIZE 20
      8 
      9 int ison(int, int [], int);
     10 
     11 int main(void)
     12 {
     13    int tree[SIZE] = { 0 };
     14    int num, p = 0; /* the number and its position */
     15 
     16    int i; /* just a counter */
     17 
     18    srand( time(NULL) );
     19 
     20    for(i = 1; i <= SIZE; i++) {
     21       num = 1 + rand() % 20;
     22       if( !ison(num, tree, SIZE) )
     23 	 tree[p++] = num;
     24    }
     25 
     26    for(i = 0; i < SIZE; i++)
     27       if(tree[i])
     28          printf("%d ", tree[i]);
     29 
     30    printf("\n");
     31 
     32    return 0;
     33 } /* E0F main */
     34 
     35 /* check if 'n' in on 'v[]'.
     36  * If yes return its position, else 0 */
     37 int ison(int n, int v[], int s)
     38 {
     39    int c;
     40 
     41    for(c = 0; c < s; c++)
     42       if(v[c] == n)
     43 	 return c;
     44 
     45    return 0;
     46 }
     47