Introduction to Fortran

Simple Program Construction in Fortran » Print Out Outfit

Printing out variables and arrays is a very simple process. You can use the print command. If we wanted to print out our outfit, we could just print out our outfit array like so:

 print *, outfit 

Question 1 of 1

Based on our current code, what would you expect the output to be from the new print statement?

 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  print *, outfit  end 

The correct answer is c.

Hope you weren’t expecting to see a list of clothing options come out of this. We left that out for now as the math was more important to us than the actual name of the clothing pieces.

So this isn’t the most useful Fortran print statement for us to know our outfit for the day. There are a few ways we could print out the human-readable option for our outfit. The simplest right now looks like this:

  if (outfit(1) .EQ. 1) then      print *, “Short-sleeved Top“ if (outfit(2) .EQ. 1) then      print *, “Long-sleeved Dress Top“ if (outfit(3) .EQ. 1) then      print *, “T-shirt“ if (outfit(4) .EQ. 1) then      print *, “Long-sleeved Top“ if (outfit(5) .EQ. 1) then      print *, “Shorts” if (outfit(6) .EQ. 1) then      print *, “Pants” if (outfit(7) .EQ. 1) then      print *, “Athletic Shorts” if (outfit(8) .EQ. 1) then      print *, “Pajama Pants 

Isn’t there a simpler way to do that though? That just seems like a lot of coding to make it print out two pieces of clothing.

Please make a selection.

There is a another way!