fork(1) download
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #define LIMIT 30000
  5.  
  6. void CreateArray (int **p, int N){
  7. int i;
  8. *p = (int*) malloc (N*sizeof(int));
  9. srand((long) 210);
  10. for (i=0; i<N; i++)
  11. (*p)[i] = rand() % LIMIT;
  12.  
  13. for (i=0; i<N; i++)
  14. printf("%d ", (*p)[i]);
  15. }
  16.  
  17. void Search (int *p, int N, int key){
  18. int comparisons = 0, success_search = 0;
  19. int i;
  20.  
  21. clock_t start, end;
  22. double elapsed;
  23. start = clock();
  24.  
  25. for (i=0; i<N; i++){
  26. if (key == p[i]){
  27. comparisons++;
  28. success_search++;
  29. printf("\nFound!");
  30. break;
  31. } else {
  32. comparisons++;
  33. printf("\nNot found!");
  34. }
  35. }
  36. end = clock();
  37. elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
  38.  
  39. printf("\nTotal comparisons: %d \n",comparisons);
  40. printf("Time elapsed: %f \n",elapsed);
  41. printf("Successful comparisons: %d \n\n",success_search);
  42. }
  43.  
  44. int main(void){
  45. int N, *p, key;
  46.  
  47. key = 1;
  48. CreateArray(&p, N = 7);
  49. Search(p, N, key);
  50. }
Success #stdin #stdout 0s 2300KB
stdin
Standard input is empty
stdout
15 14681 2612 4962 20160 28854 10119 
Not found!
Not found!
Not found!
Not found!
Not found!
Not found!
Not found!
Total comparisons: 7 
Time elapsed: 0.000001 
Successful comparisons: 0