Difference between revisions of "DPS915 Toad"
(→Code) |
(→Code) |
||
Line 12: | Line 12: | ||
====Code==== | ====Code==== | ||
− | + | #include <cstdlib> | |
− | #include <cstdlib> | ||
#include <iostream> | #include <iostream> | ||
#include <iomanip> | #include <iomanip> | ||
Line 19: | Line 18: | ||
using namespace std; | using namespace std; | ||
− | |||
int main(int argc, char* argv[]) | int main(int argc, char* argv[]) | ||
{ | { | ||
− | + | if(argc < 2) | |
− | + | { | |
− | + | cout<<"Please input the number of circle points"<<endl; | |
− | + | } | |
− | + | int npoints = atoi(argv[1]); | |
− | + | int circle_count = 0; | |
− | + | srand(time(NULL)); | |
− | + | double xValue, yValue; | |
− | + | for(int i = 0; i < npoints; i++) | |
− | + | { | |
− | + | //Generate random numbers | |
− | + | xValue = (double) rand()/RAND_MAX; | |
− | + | yValue = (double) rand()/RAND_MAX; | |
− | + | if(sqrt((xValue*xValue)+(yValue*yValue)) <= 1) | |
− | + | { | |
− | + | circle_count++; | |
− | + | } | |
− | + | } | |
− | + | double pi, ds; | |
− | + | cout<<circle_count<<"/"<<npoints<<endl; | |
− | + | ds = (double)circle_count/npoints; | |
− | + | cout<<ds<<endl; | |
− | + | pi = ((4.0)*ds); | |
− | + | cout<<"PI = "<< pi <<endl; | |
− | + | return 0; | |
} | } |
Revision as of 13:52, 14 October 2015
Contents
Project Name Goes here
Team Members
Progress
Assignment 1
PI Calculation
Code
#include <cstdlib>
- include <iostream>
- include <iomanip>
- include <math.h>
using namespace std;
int main(int argc, char* argv[]) { if(argc < 2) { cout<<"Please input the number of circle points"<<endl; }
int npoints = atoi(argv[1]); int circle_count = 0;
srand(time(NULL));
double xValue, yValue; for(int i = 0; i < npoints; i++) { //Generate random numbers xValue = (double) rand()/RAND_MAX; yValue = (double) rand()/RAND_MAX;
if(sqrt((xValue*xValue)+(yValue*yValue)) <= 1) { circle_count++; } }
double pi, ds;
cout<<circle_count<<"/"<<npoints<<endl; ds = (double)circle_count/npoints; cout<<ds<<endl; pi = ((4.0)*ds);
cout<<"PI = "<< pi <<endl;
return 0;
}