VB icon

Sample fluctuation

Email
Submitted on: 6/2/2016 9:14:53 PM
By: CodeAddiction 
Level: Beginner
User Rating: Unrated
Compatibility: C++ (general)
Views: 1014
author picture
 
     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)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// 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
//**************************************

#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;
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.