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
		}
 
	}
}
You have sixty bins, you're just printing all of them 5000 times.

The for loop responsible for printing the values in your array is located within the body of the while loop, which iterates 5000 times.
Last edited on
I am not sure why you think there are more than 60 bins?

Try this and control each move.
There are always 60 bins shown.

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
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

using namespace std;

int main()
{
	float t=0, y=0, z;
	int size=5000, x=0,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
		cout << t << endl;
		cout << z << endl;
		cout << y << endl;
		system("pause");
		if (z>=y)
            {
 			it=floor(2*t);  //bin number
 			cout << it << endl;
 			cout << bin[it] << endl;
 			system("pause");
			bin[it]=bin[it]+1;  //I think it SHOULD add one to current bin??
			cout << bin[it] << endl;
			system("pause");
			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
		}
	}
}
solved
Topic archived. No new replies allowed.