-
Notifications
You must be signed in to change notification settings - Fork 0
/
qprob.c
73 lines (56 loc) · 1.32 KB
/
qprob.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <limits.h>
#include <time.h>
/**
* This program is designed to simulate and solve the following problem. Given three
* random values, a, b, and c, all of which are bounded by [1, n], what is the probability
* that two of the values sum to the third?
*/
void ptrPrint(int *nums, int len) {
int i = 0;
for (i = 0; i < len; i++) {
printf("%d\n", *(nums + i));
}
}
int *randNum(int num, int max) {
int *randnums = calloc(num, sizeof(int));
int i;
int r = rand();
for (i = 0; i < num; i++) {
r = rand();
*(randnums + i) = (int)((r / (double)RAND_MAX) * max) + 1;
}
return(randnums);
}
int cond(int a, int b, int c) {
if (a + b == c) {
return 1;
} else if (a + c == b) {
return 1;
} else if (b + c == a) {
return 1;
}
return 0;
}
int main(int argc, char *argv[]) {
int n = atoi(argv[1]);
char *temp = argv[2];
char *e;
long sim = strtol(temp, &e, 0);
int success = 0;
long i = 0;
int *randnums = calloc(3, sizeof(int));
srand(time(NULL));
for (i = 0; i < sim; i++) {
randnums = randNum(3, n);
if (cond(*randnums, *(randnums + 1), *(randnums + 2))) {
success++;
}
}
free(randnums);
printf("Success: %d\nSimulations: %ld\n", success, sim);
printf("Probability of success: %f\n", (double)success / sim);
return 0;
}