Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help checking Binning Code Array

Status
Not open for further replies.

hummus

Programmer
Jul 2, 2008
15
US
Hi, I want to be able to read numbers inputed from a user into an array (Neutrons), and then add a count to an appropriate bin. The code is below.

I want to make sure that the array I am using (Neutrons(1000)) which stores the inputed numbers is used correctly in the code.

Code:
	PROGRAM Binning
	IMPLICIT NONE

	INTEGER :: a, i, nrandoms
	INTEGER :: bin(20) !** change according to # of bins
	REAL :: Neutrons

	DIMENSION [b]Neutrons(1000)[/b]

	
	DO i=1, 20 !** change according to # of bins/ set bins initially to 0
		
		bin(i) = 0

	END DO

	
	WRITE(*,*) 'Enter number of randoms'
	
	READ(*,*) nrandoms

	
	WRITE(*,*) 'Enter randoms'

	 DO a=1, nrandoms

	  READ(*,*) [b]Neutrons(a) [/b] !** read the 1000 random numbers and store in an array

		IF [b](Neutrons(a)[/b] > .0288 .AND. [b]Neutrons(a)[/b] <= .0411) THEN
			
			bin(1) = bin(1) + 1
		
		ELSEIF (Neutrons(a) > .0411 .AND. Neutrons(a) <= .0538) THEN

			bin(2) = bin(2) + 1
		
		....
		
		END IF

         END DO

	WRITE(*,) (bin(i), i=1, 20) !** change according to # of bins

	END PROGRAM Binning

Thanks so much for your help!
 
That should work. However, it could be streamlined a little.
For instance, each time you are checking if the value is less or equal than a threshold, you do not need to check if it is above the other threshold value, since it already passed that test on the previous statements.

Basically, you could use something like

IF(neutron<=.0288) THEN
C whatever you do when it is lower than the lowest,
C possibly doinn nothing
ELSEIF(neutron<=.0411) THEN
bin(1)=bin(1)+1
ELSEIF(neutron<=.0538) THEN
bin(2)=bin(2)+1


and so on. You save one test each statement.


For even more flexibility, you can have a threshold array, let's call it "tbin" that contains the values to be compared with. You'd have your choice on how to populate it (user input, file input, static data statement/parameter) but it would allow collapsing all the tests for the 20 odd bin level into only one (you'd have to handle the less than lowest threshold "disregard" case through a specialized statement however) within a DO loop that scans for the right "bin" to target.


CBVG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top