//************************************** // Name: Sample fluctuation // Description:Algorithm is testing for "sample fluctuations" used in stats. It is simulating a survey so you don't have to ask many people what would take much more time. Though, it only incrases the amount of participants because you know the theoretical result of the experience. At the end you have a txt file that recaps everything. Greetings! (sorry for my english) // By: CodeAddiction // // // Inputs:None // // Returns:None // //Assumes:None // //Side Effects:None //************************************** #include<time.h> #include<fstream> #include<cstdlib> #include<cmath> #include<iostream> using namespace std; ofstream reportfile; int main(){ reportfile.open("SamplingFluctuation-report.txt"); int total,ivhit,hitc,ssize; float ap,rn,res,ssizef; srand(time(NULL)); cout<<"Enter total of calculations: "; cin>>total; reportfile<<"Total of calculations: "<<total; cout<<"Enter sample size: "; cin>>ssize; reportfile<<"\nSample size: "<<ssize; cout<<"Enter tested percentage: "; cin>>ap; reportfile<<"\nTested percentage: "<<ap; ap=ap/100; cout<<"Results:\n"; reportfile<<"\nResults:\n"; for(int lp=1;lp<=total;lp++){ hitc=0; for(int calc=1;calc<=ssize;calc++){ rn=((double)rand()/(RAND_MAX)); if(rn<ap){ hitc++; } } ssizef=ssize; res=hitc/ssizef; cout<<res<<endl; reportfile<<res<<endl; if(res>ap-1/sqrt(ssize)&&res<ap+1/sqrt(ssize)){ ivhit++; } } cout<<"Interval hits: "<<ivhit<<endl; reportfile<<"Interval hits: "<<ivhit; reportfile.close(); cin.sync();cin.get(); return 0; }