-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkov.cxx
More file actions
94 lines (73 loc) · 1.38 KB
/
markov.cxx
File metadata and controls
94 lines (73 loc) · 1.38 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Directives
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
//Prototypes
double prob();
//Post condition: returns random value between 0 and 1
void print(int state);
//precondition: state is either 0 or 1
//postcondition: prints either G or B
//------------------------------------------------------------------------------
int main(const int argc, const char *argv[])
{
if(argc != 5)
{
fprintf(stderr, "usage: markov <q> <Q> <h> <n>");
return 1;
}
else
{
srand(time(NULL));
float q = atof(argv[1]);
float Q = atof(argv[2]);
float h = atof(argv[3]);
float n = atof(argv[4]);
int count;
int state;
state = 0;
// cout << n << endl;
for(count = 0; count < n; ++count)
{
double probvalue = prob();
if(state == 0 && probvalue < Q)
{
cout << state <<endl;
state = 1;
}
else if(state == 1)
{
if(probvalue < q && probvalue > h)
{
cout<< 0 << endl;
}
else if (probvalue > q)
{
cout<<state<<endl;
state = 0;
}
}
}
return EXIT_SUCCESS;
}
}
//------------------------------------------------------------------------------
double prob()
{
double max = RAND_MAX;
double random = rand();
return random/max;
}
void print(int value)
{
if (value == 0)
{
cout << "G" << endl;
}
else
{
cout << "B" << endl;
}
}