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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array troubles help appreciated

Status
Not open for further replies.

DAG12345

Technical User
Oct 18, 2007
3
Ok so I have to write a program for my program class and im having a little trouble. The assignment is to collect data about rainfall for the 12 months of the year, find the average, the total rainfall through the year, and the months with the heighest and lowest amount of rain, and to automatically exit the program when a negative number is entered for amount of rain. What I am stuck on is how to match the highest and lowest amounts of rain with the months and to exit when a negative number is entered. Its weird that I can't get it to exit when a negative number is entered considering i have done programs doing that. I tried using a Do while loop but i keep getting errors, i think it is because I may be making mistakes coupling the do while loop and do counter. Any help would be really appreciated. Thanks guys.

Code:
!The purpose of this program is to collect data of the amount of rainfall for twelve months.
!Then find the average rainfall per month, total rainfall per year and the months with the lowest
!and highest rainfall.
!R_fall (rainfall per month)
!average= total/12 ( average rainfall per month)
!Total=total+r_fall(i) (the total rainfall of all months)
!hi= the heighest amount of rain from the range of months
!low= the lowest amount of rain frome the range of months
!month (month of the year)
!
!

Program Rain

Implicit none

Integer:: I
real:: total=0, average
real, dimension(12):: r_fall, hi, low
character, dimension(12):: month



A: Do I= 1, 12

Print*, "enter month:"
Read*, month(I)

Print*,"Enter negative amount of rain to exit."

Print*,"enter rain fall:"
Read*,r_fall(I)

total= total+r_fall(I)

End do A


average= total/12

hi= R_fall(1) !This assigns the initial highest value, in this case the first entry
low= R_fall(1)!This assigns the initial lowest value, in this case the first entry

B: Do i= 1, 12

If(R_fall(I)>R_fall(1)) Then !This evaluates the first entry of rainfall and compares
!it to the initial assigned value of rainfall.

hi= R_fall(I) !If the first entry is higher than the initial assigned highest value
!of rainfall then the first entry is made to be the highest value.



End if

If(R_fall(I)<R_fall(1)) Then !This evaluates the first entry of rainfall and compares
!it to the initial assigned value of rainfall.

low = R_fall(I) !If the first entry is lower than the initial assigned lowest value
!of rainfall then the first entry is made to be the lowest value.


end if

end Do B

PRint*,"Month of heighest rain:", ! The problem is here, trying to match the heighest and lowest numbers !
PRINT*,"Month of lowest rain:", ! for r_fall with the respective months
Print*,"Total rain:",total
Print*,"Average monthly rain:",average


End Program Rain

Code:
 
If anybody has any help to offer, especially with matching the months with the high and the low it would be a big help, I got class comming up on friday and I could use some help, thanks.
 
Code:
  if(R_fall(I)>hi) Then
    hi= R_fall(I)
  end if

  if(R_fall(I)<low) Then
    low = R_fall(I)
  end if
 
You can simply use array intrinsics

MAXVAL/MINVAL to give the max and min values of an array, and:
MAXLOC/MINLOC to give the array position of those values.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top