Created
March 27, 2017 22:31
-
-
Save axsddlr/2d3fe5b12b11a7a59bcb810761ef5009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Program Threesum_C.c | |
** | |
** Counts the occurances of a specified number in an array. | |
** | |
** | |
*/ | |
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
long int count(long int *a); // prototype function | |
int main() | |
{ | |
long int arr[10000], i=0; | |
int value; | |
clock_t start_t, end_t, total_t; | |
FILE *fp; | |
fp = fopen("numbers1.dat", "r"); | |
if ((fp = fopen ("numbers1.dat", "r")) == NULL) | |
return 1; | |
start_t = clock(); | |
printf("Starting of the program: = %ld\n", start_t); | |
fscanf(fp,"%d", &arr[i]); | |
while(i < 10000) | |
{ | |
fscanf(fp,"%d", &arr[i]); | |
printf("%d", count(&arr[0])); | |
++i; | |
} | |
fclose(fp); | |
end_t = clock(); | |
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC; | |
printf("Total time taken by CPU: %f\n", total_t ); | |
return 0; | |
} | |
long int count(long int *a) | |
{ int N = 10000; | |
int counting = 0; | |
for (int i = 0; i < N-2; i++) | |
for (int j = i+1; j < N-1; j++) | |
for (int k = j+1; k < N; k++) | |
if ((a[i]+a[j]+a[k]) == 0) | |
{ | |
counting++; | |
printf("Triplet Found : %d, %d, %d", a[i], a[j], a[k]); | |
} | |
return counting; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment