Array Binning Problems

Hello,

For a class I am having to make a simulation of a lab we have done. The problem I'm having is increment the value stored in each bin. It seems to keep adding bins as when I run the program I get much more than 60 bins.

The program generates a two random numbers (one between 0 and 30 and one between 0 and 1) then checks if the values fall within/under the exponential decay curve. If it falls under the curve, it accepts the value. Once the value is accepted it should be binned. There should be 60 time bins incremented by .5 minutes therefore .5*60=30 minutes total (i.e. the number between 0 and 30.

I am getting the right values for t, y, and it when displayed but when I go to display the counts in each bin there are more than 60 bins. Does anyone know what I am doing wrong?

Thank you in advance for your time and help

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
 
using namespace std;
 
int main(){
	float t=0;
	float y=0;
	float z;
	int size=5000;
	int x=0;
 	int it;
	int bin[60]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
 
	while(x<size){
		t= (rand() % 30001)/1000.0; //gives random number between 0 and 30
		y= (rand() % 1001)/1000.0;  //gives random number between 0 and 1
		z= (exp(-t/3.896)+(72.433/28209.33));  //exponential curve to check if y is under
		if (z>=y){
 			it=floor(2*t);  //bin number
			bin[it]=bin[it]+1;  //I think it SHOULD add one to current bin??
			x=x+1;  //increment x
			//cout << t << "		" << y << "		" << it << endl;
        	
        	}
		for(int l=0; l<60; l++){
			cout << bin[l] << endl;  //display number of counts in each bin
		}
 
	}
}
Last edited on
Solved
Topic archived. No new replies allowed.