Introduction to Fortran

Simple Program Construction in Fortran » Arithmetic for Outfits

All the different types of arithmetic are available in Fortran.

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • ** (power)
  • SQRT(x) (square root)

All of these types of arithmetic functions can be run on any numeric variable type.

 high_temp_2times = high_temp + high_temp high_temp_times3 = high_temp * 3 high_temp_in_C = (high_temp - 32) * 5/9 

Arithmetic can even be run on arrays. This is where the big payoff comes from using an array for our wardrobe and also setting an array for our outfit.

 wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /) outfit = (/ 0, 0, 0, 1, 0, 0, 1, 0 /)  wardrobe = wardrobe - outfit 

Because these two arrays have the same dimensions, the math can work out great! If you printed out the clean wardrobe after these commands, you would find it would look like this:

 (6, 3, 3, 1, 5, 5, 2, 2) 

This is why we wanted to make sure that the outfit was numeric. It just makes life that much easier.

Here’s our code now:

 program pick_outfit implicit none  integer high_temp logical weekend integer wardrobe(8) integer outfit(8)  !The first four elements in the array are for tops !wardrobe(1) = short-sleeved top !wardrobe(2) = long-sleeved dress top !wardrobe(3) = t-shirt !wardrobe(4) = long-sleeved top  !the last four elements in the array are for bottoms !wardrobe(5) = shorts !wardrobe(6) = pants !wardrobe(7) = athletic shorts !wardrobe(8) = pajama pants  high_temp = 64 weekend = .TRUE. wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /) !outfit not set yet, so just setting it to all zeroes for safety outfit = (/ 0, 0, 0, 0, 0, 0, 0, 0 /)  if (weekend .EQ. .TRUE.) then      if (high_temp .GE. 70) then           outfit(3) = 1      else           outfit(4) = 1      end if      if (high_temp .GE. 60) then           outfit(7) = 1      else           outfit(8) = 1      end if else      if (high_temp .GE. 70) then           outfit(1) = 1      else           outfit(2) = 1      end if      if (high_temp .GE. 60) then           outfit(5) = 1      else           outfit(6) = 1      end if end if  wardrobe = wardrobe - outfit  end 

So we are so close to having an outfit. What are we still missing? We can’t see the outfit that our program chose yet. We need to print it out onto our screen so we can see what the program decided.