blob: 1f775a04e76deffe9c5d715c09cf5e20fdae8127 (
plain)
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
|
// RandDemo.cc
// features:
// * uses random() to get a random integer
// * gets interactive user input
// * uses ternary operator "q ? a : b"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
#define ranf() \
((double)random()/(1.0+(double)RAND_MAX)) // Uniform from interval [0,1) */
int main()
{
int outcome, N=0, count_in=0 ;
double fraction_in ;
// Initialise random number generator with value of system time.
srandom(time(NULL));
// Get user input in correct range.
while(N<1)
{
cout << "Input the number of experiments: ";
cin >> N;
}
// Perform N experiments.
for(int n=1; n<=N; n++)
{
double x = ranf();
double y = ranf();
outcome = ( x*x + y*y > 1.0 ) ? 0 : 1 ;
if(outcome==1) count_in++;
cout << outcome << "\t" << x << "\t" << y << "\t"
<< count_in << "\t" << n << endl;
}
// Sample goto to raise a violation
goto L1;
//Sample switch with default
switch (bob)
{
case 1: {
cout << "1";
break;
}
case 2:
{
cout <<"2";
break;
}
default:
{
cout << "3";
}
}
//Sample switch without default
switch (bob)
{
case 1: {
cout << "1";
break;
}
case 2:
{
cout <<"2";
break;
}
}
//Integer variables must be converted (cast) for correct division
fraction_in = static_cast<double>(count_in)/N;
// Output results
cout << "# Proportion of outcomes 'in' "
<< fraction_in << endl;
// Output results
cout << "# pi-hat = "
<< 4.0 * fraction_in << endl;
return 0;
}
|