-2

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.

  • 4
    `Dir c[n];` is a variable length array, which is not standard in C++. But you do not need it at all, you can simplify your loop to `for (int i = 0; i < n; i++) if (below(someFun())) numCicle++;` – mch Dec 27 '20 at 15:40
  • 2
    [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) Rather use a `std::vector` when you need an array of dynamic size – 463035818_is_not_an_ai Dec 27 '20 at 15:42
  • If you want to use dynamic arrays, go for `std::vector`. Also you should really make it your standard routine to properly initialize variables, that is `double pi = 0.;` instead of `double pi;`. Won't bite you here, but might easily bite you in future code, and having proper initialization as a standard counters that. – Aziuth Dec 27 '20 at 15:42
  • 1
    Also, for your question, you have only stack variables. Those deallocate themselves when the scope is left (in this case, your function). Memory management is relevant when you use heap variables, like if you wrote `Dir* numCicle = new Dir[n];`, which is how you'd initialize a dynamic raw array on the heap - but `std::vector` is in almost any case the better choice. – Aziuth Dec 27 '20 at 15:45
  • Consider reading http://www.cplusplus.com/doc/tutorial/dynamic/ (and possibly other parts of that or another tutorial). – Aziuth Dec 27 '20 at 15:47

1 Answers1

0

You have to initialize numCicle before adding something to that.

In other words,

   double numCicle;

should be

   double numCicle = 0;
MikeCAT
  • 73,922
  • 11
  • 45
  • 70