Introduction

Hello World with LEDs

Computer programs, whether they are made for stylizing web pages or simulating thousands of years of the conditions of Earth’s climate, all consist of the same things: algorithms. An algorithm is a set of steps taken to accomplish a task. In scientific computing, many algorithms, sometimes very complex ones, are needed to accomplish various tasks. This 2-part module introduces users to the general structure of a computer program via a generic programming thought process called pseudocoding, that will guide the user through constructing their own algorithms. Along the way, we’ll learn the common syntax, functions, and more that are used to perform certain common parts of algorithms. Then, we’ll revisit the algorithm in a specific language to learn the basic syntax, purpose and advantages to using that language. Currently Fortran is available. Real-life examples of Fortran code are available afterward and interactions in which the user identifies parts of Fortran algorithms can be used for practice.

Pseudocoding

Though it doesn’t appear so when we use complicated words like “algorithm” to describe sometimes complex processes in computing, we all use algorithms every day.

A simple example would be deciding what to do when approaching a stop light: drive through it, slow down in preparation to stop, or stop fully. Your decision depends upon which color the light is.

Your thought process might go something like this:

  1. If light is green, drive through intersection
  2. If light is yellow, slow down and prepare to stop
  3. If light is red, come to quick stop before intersection

The above is an example of pseudocode - a humanized version of computer code meant to be written in sentence form instead of coded bytes. Pseudo-code has no standards except that it is an attempt at organizing your current thoughts into a form that would resemble the flow/sequence of a computer program.

Pseudocode is made to resemble the tasks you intend to have the computer perform. These tasks need to be in order for the computer to know how to execute some of the commands. For instance, if you would like to do some math with variables, the program would need to know the values of the variables before being able to do the math.

So, the order of pseudocode makes a difference. The specific words you choose to use don’t make a difference as long as the pseudocode makes sense to you. Your pseudocode may not come out in the right order on the first try. That isn’t a problem. You can always move pseudocode around without serious implications. Moving code snippets around after the program is written is more problematic.

Let’s now consider a more complex task: How does a person decide what to wear on any given day?

Every morning, we get up and have to decide what to wear. We make decisions based on if the weather is one way or another using variables like temperature, precipitation forecasts, or predicted cloud cover. We make different clothing decisions based on whether it is the weekend or not. And we make decisions based on the array of clothes we have that are clean. After picking out clothes for the day, we might even consider how that changes the amount of clean clothes remaining, since we have subtracted some clothes from our closet. We might mentally print a reminder: “Do laundry today!”

Each of the bold words in our paragraph hint at important parts of the “what to wear today” algorithm. Let’s now step through making an algorithm, or program, in pseudocode that will pick the appropriate clothing for us based on several inputs.

Simple Program Construction with Pseudocode

We’ll keep this “what to wear today” program relatively simple. Let’s walk through it together step-by-step.

In most programming languages, it is good to define variables first at the top of the program. This helps ensure one has thought through all of the necessary code to make a program run. Don’t worry if you don’t capture all information right away. We can always go back and add more later.

Simple Program Construction with Pseudocode » Collecting Information

Question 1 of 2

What information would you need to know to start the “what to wear today” program? List your answers below.


Some of the more important pieces of information you would need are: the weather (temperature, cloud cover, precipitation), your temperature thresholds for when you would where a short-sleeve top/shorts versus a long-sleeve top/pants, the day of the week (workday versus weekend; holiday; workday), and the number of clean pieces of clothing you have.

Simple Program Construction with Pseudocode » Making Decisions

So far our pseudocode looks something like this:

1. Use app to gather forecast low and high temperature, cloud cover,
and precipitation for today. 2. Use calendar to find whether it is a workday or weekend.

For the rest of this program, we will focus only on the high temperature as our weather variable. Let’s try to figure out what the pseudocode would look like for the decision-making process based on temperature. Let’s assume that your temperature threshold is a forecast high temperature at or above 70°F for lighter tops.

Question 1 of 5

Which two of the following pseudocode snippets would best represent the 70°F threshold for top selection? Select the best two pseudocode snippets.

The correct answers are a, d.

These two pseudocode snippets together with the forecast high temperature data would help you make the proper selection of tops. You may also be able to substitute the word “when” for “if” and the pseudocode would still make sense. Using “if” is preferable for reasons you will see later when we get to actually coding this example up. Notice that choices c and d are very similar - but only choice d is correct, given that you've already accounted for temperatures greater than AND equal to 70 as part of your threshold.

Please make a selection.

Question 3 of 5

Write a pseudocode snippet that makes the decision to wear pants (below 61°F) or shorts (above 60°F)?


Your pseudocode should look a lot like the following:

If the forecast temperature is above 60°F, select shorts,
else select pants.

You could have chosen to express this slightly differently by switching the order of the shorts and pants and thus the decision-making point. That would look like this:

If the forecast temperature is at or below 60°F, select pants,
else select shorts.

Question 4 of 5

Does the order of top and bottom decisions make a difference in your program?

If the temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved top. If the forecast temperature is above 60°F, select shorts,
else select pants.

The correct answer is b.

These two pieces of clothing are separate from each other, so the order doesn’t make a difference. Ok, ok, it makes a difference if you want the second piece of clothing to match the first piece, but we digress... so for now, no it doesn’t matter what order they go in.

Please make a selection.

Challenge Question

Write 1 pseudocode decision snippet based on the other weather information.


Some options include:

Based on precipitation probability, you might need to take an umbrella or raincoat.

If the forecast chance of precipitation is greater than 50%,
take an umbrella or a raincoat.

Based on cloud cover percentage, you might need sunglasses.

If the forecast cloud cover is less than or equal to 70%,
take sunglasses.

Based on the if the low temperature will be below a certain value and the high temperature will be above a certain value, you might want to layer your clothing.

If the forecast low is less than 50°F AND the forecast high is
greater than or equal to 70°F, wear layered short-sleeved top under a long-sleeved top.

There are numerous more options for pseudocode you could write based on the weather information gathered from the app. What if the temperatures are really low? Maybe checking the windchill becomes important, or maybe you need to wear gloves and a hat below a certain temperature. The list goes on and on.

We have other information that we can take advantage of too that will help us make the clothing decisions. We know whether it is a workday or a weekend. Most individuals will dress differently at work than on the weekend. Because of this we could add another decision point in our pseudocode to help us make a decision based on whether it is a workday or weekend.

Question 5 of 5

Based on the previous pseudocode, where would the new decision making point occur? Drag the new pseudocode snippet (first in the list) to the correct location.

  1. If today is a weekend… else ...
  2. Use app to gather forecast high temperature for today.
  3. Use calendar to find whether it is a workday or weekend.
  4. If the forecast temperature is at or above 70°F, select a short-sleeved top,
    else select a long-sleeved top.
  5. If the forecast temperature is above 60°F, select shorts,
    else select pants.

Simple Program Construction with Pseudocode » Nesting

A topic that is not completely obvious in pseudocoding is something we call nesting. Nesting some of our statements inside of one another is really useful especially for decision-making processes that are independent but related. In this case, making a clothing decision is based on the temperature outside, but it also matters whether it is a weekend or not because on the weekend you don’t need to dress as nicely. So you would be able to nest these decisions inside of each other like so:

Good Example (fewer lines) Content

In this example, the sub-bullets would be considered “nested” pseudocode as they would run inside of the “parent” pseudocode. The sub-bullets would typically be indented for readability.

1. If today is a weekend
	a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants.

Bad Example (more lines) Content

1. If the forecast temperature is at or above 70°F
	a. If today is a weekend, select a t-shirt,
else select a short-sleeved top Else a. If today is a weekend, select a long-sleeved top,
else select a long-sleeved dress top 2. If the forecast temperature is above 60°F, a. If today is a weekend, select athletic shorts,
else select shorts Else a. If today is a weekend, select pajama pants,
else select pants

The fewer decision points we have to go through to get the “answer” the better in any sort of decision tree when evaluating for computing efficiency.

Simple Program Construction with Pseudocode » Checking Your Closet

Let’s say we have made the decision now what clothing pieces we would like to wear based on the weather information and the workday with the following pseudocode:

1. Use app to gather forecast high temperature for today.
2. Use calendar to find whether it is a workday or weekend.
3. If today is a weekend
	a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants.

Do we have those decided-upon clothes available in our closet? We haven’t figured out a way to set that in our pseudocode yet. We need to define our entire wardrobe somewhere in this pseudocode.

Question 1 of 2

Use the dropdown to complete the following sentence.

a) In the wardrobe, we need to track different types of clothing based on the previous pseudocode.

The necessary clothing types we need to track are these:

  • Short-sleeved top
  • Long-sleeved dress top
  • T-shirt
  • Long-sleeved top
  • Shorts
  • Pants
  • Athletic shorts and
  • Pajama pants

Could we group all of those together into smaller groupings? Yes, we could. However, it would make it harder to track the differences we would like to make based on weekends and weather information.

Question 2 of 2

In the following text field, write out the necessary pseudocode that organizes your wardrobe based on the categories mentioned above.


Something like the following would suffice as pseudocode for filling your wardrobe.

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

That code should go near the top of your program as it is a definition of the wardrobe which is necessary to help us figure out whether we need to do laundry or not.

Our pseudocode now looks like this:

1. Use app to gather forecast high temperature for today.
2. Use calendar to find whether it is a workday or weekend.
3. Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants.
4. If today is a weekend a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants.

We just cataloged our entire clean wardrobe, so we can figure out whether we need to do laundry or not. Let’s see how we can figure that out.

Simple Program Construction with Pseudocode » Laundry!

We haven’t figured out when we should do laundry or even if we have enough clothes to wear for today. How are we going to make that happen?

Let’s assume that we don’t need to track individual pieces of clothing. We just need to track how many of each type of clothing we have. We could also then track how many of each type of clothing is still clean. It is really nice to think of your wardrobe as a table of 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

The first row of the table is unnecessary, but is here to make it clearer what each column is referring to. One could also make two separate tables, one for tops and one for bottoms. It is more efficient to use only one table, but it may be harder to think about that framework if this is your first time programming.

When we pick out an outfit, we would then need to subtract those pieces of clothing from our wardrobe. Let’s assume that if we get down to zero clean individual pieces of clothing, we would want to do laundry. So we would then also want to send a message to ourselves to do the laundry to make sure we do some before the next day.

Question 1 of 2

Using the pseudocode we have written so far, where would you put the subtraction from the wardrobe for the most efficient coding/least repetition of the same pseudocode? Type into the code in the box to add your subtraction text.


Although you may have been tempted to place the subtraction immediately after the selection of that piece of the outfit, it is making you repeat the pseudocode. Instead, it is best to place the subtraction at the end of the program so you would only have to do the subtraction once and you wouldn’t have to repeat the pseudocode four separate times.

Simple Program Construction with Pseudocode » Looping

We have a really robust outfit selector going. We could look through the entire clean wardrobe now to see if we have used all of one type of clothing. This looking through your wardrobe is called “looping”. You would have to go through your wardrobe to count how many of each type of clothing is left. The process of counting is exactly the same every time. Count the number of clean pieces of clothes in each clothing type. But this needs to be repeated until you are out of clean clothing to count.

An analog clock is a great example of a similar looping event. The only thing your clock’s second-hand does is rotate to the next tooth of the gear every second. Pseudocode for that might look like this:

1. For every second, do the following:
	a. Rotate second hand by 6°.
		

What could we do to our pseudocode to make it loop through the clean wardrobe appropriately?

1. Use app to gather forecast high temperature for today.
2. Use calendar to find whether it is a workday or weekend.
3. Catalog your entire wardrobe into short-sleeved, long-sleeved dress, t-shirt,
and long-sleeved tops, and shorts, pants, athletic shorts, and pajama pants. 4. If today is a weekend a. If the forecast temperature is at or above 70°F, select a t-shirt,
else select a long-sleeved top. b. If the forecast temperature is above 60°F, select athletic shorts,
else select pajama pants. else (today is a workday) a. If the forecast temperature is at or above 70°F, select a short-sleeved top,
else select a long-sleeved dress top. b. If the forecast temperature is above 60°F, select shorts,
else select pants. 5. Subtract today’s outfit from your clean wardrobe. 6. Look through clean clothes a. If no more clean clothes left i. Print out an alert to do laundry.

The indentation here is to help you see where the nesting occurs. The looping structure goes around the entire program.

That is all we have to do in our (simple) program. By writing your pseudocode out, you can easily then write this program in any algorithmic programming language.

Back to Top