Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. /* Program Threesum_C.c
  2. **
  3. ** Counts the occurances of a specified number in an array.
  4. **
  5. **
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <time.h>
  10. #include <stdlib.h>
  11.  
  12. long int count(long int *a); // prototype function
  13.  
  14. int main()
  15. {
  16. long int arr[10000], i=0;
  17. int value;
  18. clock_t start_t, end_t, total_t;
  19. FILE *fp;
  20. fp = fopen("numbers1.dat", "r");
  21.  
  22.  
  23. if ((fp = fopen ("numbers1.dat", "r")) == NULL)
  24. return 1;
  25.  
  26. start_t = clock();
  27. printf("Starting of the program: = %ld\n", start_t);
  28. fscanf(fp,"%d", &arr[i]);
  29.  
  30. while(i < 10000)
  31. {
  32. fscanf(fp,"%d", &arr[i]);
  33. printf("%d", count(&arr[0]));
  34. ++i;
  35. }
  36. fclose(fp);
  37. end_t = clock();
  38.  
  39. total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
  40. printf("Total time taken by CPU: %f\n", total_t );
  41. return 0;
  42. }
  43.  
  44.  
  45. long int count(long int *a)
  46. { int N = 10000;
  47. int counting = 0;
  48. for (int i = 0; i < N-2; i++)
  49. for (int j = i+1; j < N-1; j++)
  50. for (int k = j+1; k < N; k++)
  51. if ((a[i]+a[j]+a[k]) == 0)
  52. {
  53. counting++;
  54. printf("Triplet Found : %d, %d, %d", a[i], a[j], a[k]);
  55. }
  56. return counting;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement