I have a void function which I call in the main and I want it to use a new value for pi each time is called, and not adding them. I thing I should deallocate the memory for the array of type Dir, but I can't figure it out. The implementation is as follows:
void throwSeries(int n)
{
double pi;
double faults;
double numCicle;
Dir c[n];
int count = 0;
for (int i = 0; i < n; i++)
{
c[i] = someFun(); // Returns type Dir
if (below(c[i]))
{
numCicle++;
}
}
pi = 4 * (numCicle / (double) n);
faults = ((pi - M_PI) / M_PI) * 100;
cout << setw(15) << setfill(' ') << n;
cout << fixed << setprecision(5);
cout<< setw(15)<< setfill(' ') << pi << setw(15)<< setfill(' ') ;
cout << fixed << setprecision(1);
cout << faults << endl;
}
int main()
{
system("CLS");
srand(time(0));
cout << setw(15) << "n" << setw(15) << "pi" << setw(15) << "faults" << endl;
cout << setw(15) << setfill('-') << "|"<< setw(15) << setfill('-') << "|";
cout << "--------------" << endl;
int n = 0;
for (int i = 0; i < 100; i++)
{
n += 100;
throwSeries(n);
}
return 0;
}
A print out example is as follows:
n pi Rel. fault
--------------|--------------|--------------
100 3.12000 -0.7
200 4.66000 48.3
300 6.28000 99.9
400 7.80000 148.3
500 9.40000 199.2
600 10.76000 242.5
And the values of pi shouldn't be added in each iteration.