Introduction to Fortran

Simple Program Construction in Fortran » Arrays of Clothing

You may have thought to yourself that you might want to just declare a character variable for top and bottom. This would work just fine, but then you would have to interpret that top or bottom to figure out what to subtract from your wardrobe. That is an extra step. And extra steps are more computing time and more room for mistakes in your code. So there has to be another way to do this that will work.

Let’s start with our wardrobe though.

 Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants.

Question 1 of 2

Based on our pseudocode, how many different types of clothing do we have to select from?

The correct answer is d.

We have eight different types of clothing:

  • Short-sleeved tops
  • Long-sleeved dress tops
  • T-shirts
  • Long-sleeved tops
  • Shorts
  • Pants
  • Athletic shorts
  • Pajama pants

As was stated before, we don’t need to track individual pieces of clothing, just the number of each type of clothing. This is part of how we can convert our wardrobe into numbers.

Please make a selection.

The other part of this that makes things even easier is to make is what is called an array. We can make an array of clothing that is we’ll call your wardrobe. It can be conceptualized much like we did before with the table of wardrobe data:

Tops

Bottoms

Short-sleeved

Long-sleeved dress

T-shirt

Long-sleeved

Shorts

Pants

Athletic shorts

Pajama pants

6

3

3

2

5

5

3

2

We don’t need all of this information in the array, just the numbers. Let’s declare this array much like we would a variable. It even gets declared at the top of the program before any lines of code get executed.

 integer wardrobe(8) 

When declaring an array, you have to assign it to one of the basic variable types and give it a length (called the number of elements). In this case, we have an integer because you can only have whole pieces of clothing, and it has 8 elements inside of it to match the number of types of clothing we have. You can think of this 8 element array (wardrobe) just like the above table. In fact, you can assign values to the array, in a very similar fashion to this table.

 integer wardrobe(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  wardrobe = (/ 6, 3, 3, 2, 5, 5, 3, 2 /) 

To get an individual value from the array, you can call it by its element number or index. To find out how many athletic shorts are clean in your wardrobe right now, you could call:

 integer clean_athletic_shorts  clean_athletic_shorts = wardrobe(7) 

If you printed out the value of clean_athletic_shorts, it would be 3.

All Fortran arrays (unless otherwise identified) will start from an index of 1 and end at the index given in the declaration of the array (8).

Question 2 of 2

Based on the wardrobe definition below, fill in the following statements.

Tops

Bottoms

Short-sleeved

Long-sleeved dress

T-shirt

Long-sleeved

Shorts

Pants

Athletic shorts

Pajama pants

6

3

3

2

5

5

3

2

a) To find out how many clean t-shirts we have, we would call wardrobe with an index of .
b) If you had selected an outfit that consisted of a long-sleeved top and a pair of athletic shorts, you could reassign the entire array to have , , , , , , ,
c) If you wanted to subtract a pair of pants from your wardrobe, you could make a call on wardrobe with index and assign its new value to .

“wardrobe(3)” would give you the number of clean t-shirts you have.

wardrobe = (/ 6, 3, 3, 1, 5, 5, 2, 2 /)

wardrobe(6) = 4

That last example did the job, but wasn’t it painful to get through? Isn’t there a better way to make the wardrobe get smaller based on your outfit?