0

Here is what I came up with, but I want it where if I input 100 iterations it will give me 100 of those points and put it in a textfile. I want it to also tell me the final estimation of pi as well. So here are the two pieces of code:

#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;


// function main begins program execution
int main () {
ofstream myfile;
//opens the file myfile.txt
myfile.open("myfile.txt");
    cout<<"Random numbers generated between 0 and 1:"<<endl;
    srand(time(0));
//for loop to write the values to the file
int n;
cout <<"Enter the number of iterations: " <<endl;
cin >> n;
for(int i=0;i<n;i++){
myfile<<(float) rand()/RAND_MAX<<","<<(float) rand()/RAND_MAX<<endl;
}
cout<<"Printed numbers to file myfile.txt\n";
}

This is the other piece of code I want to combine it with:

/* C++ program for estimation of Pi using Monte
Carlo Simulation */
#include <bits/stdc++.h>

// Defines precision for x and y values. More the
// interval, more the number of significant digits
#define INTERVAL 10000
using namespace std;

int main()
{
    int interval, i;
    double rand_x, rand_y, origin_dist, pi;
    int circle_points = 0, square_points = 0;

    // Initializing rand()
    srand(time(NULL));

    // Total Random numbers generated = possible x
    // values * possible y values
    for (i = 0; i < (INTERVAL * INTERVAL); i++) {

        // Randomly generated x and y values
        rand_x = double(rand() % (INTERVAL + 1)) / INTERVAL;
        rand_y = double(rand() % (INTERVAL + 1)) / INTERVAL;

        // Distance between (x, y) from the origin
        origin_dist = rand_x * rand_x + rand_y * rand_y;

        // Checking if (x, y) lies inside the define
        // circle with R=1
        if (origin_dist <= 1)
            circle_points++;

        // Total number of points generated
        square_points++;

        // estimated pi after this iteration
        pi = double(4 * circle_points) / square_points;

        // For visual understanding (Optional)
        cout << rand_x << " " << rand_y << " "
            << circle_points << " " << square_points
            << " - " << pi << endl
            << endl;

        // Pausing estimation for first 10 values (Optional)
        if (i < 20)
            getchar();
    }

    // Final Estimated Value
    cout << "\nFinal Estimation of Pi = " << pi;

    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Asim Malik
  • 29
  • 5
  • [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Jesper Juhl Sep 22 '22 at 08:54
  • 1
    Maybe you're not going to like this, but I really think you would learn more about programming if you at least tried to do this yourself. If you run into a specific issue that you don't understand then by all means ask about that. But for now, try writing your own code. Long term this is the best approach. – john Sep 22 '22 at 08:58
  • Main problem here is main! Please learn to split code into smaller function ASAP. The lounger function is it is harder to read understand and maintain. – Marek R Sep 22 '22 at 09:13

0 Answers0