chapter 4: thinking like a programmer - opfinderklubben.dk€¦ · chapter 4: thinking like a...

10
Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to learn is how to think like a programmer. A programmer is not created by simple books or classes but grows from within an individual. To become a "good" programmer takes passion for technology, self learning, basic intelligence, and a drive to create and explore. You are like the great explorers Christopher Columbus, Neil Armstrong, and Yuri Gagarin (the first human in space). You have an unlimited universe to explore and to create within the computer. The only restrictions on where you can go will be your creativity and willingness to learn. A program to develop a game or interesting application can often exceed several thousand lines of computer code. This can very quickly become overwhelming, even to the most experienced programmer. Often we programmers will approach a complex problem using a three step process, like: 1. Think about the problem. 2. Break the problem up into pieces and write them down formally. 3. Convert the pieces into the computer language you are using. Pseudocode: Pseudocode is a fancy word for writing out, step by step, what your program needs to be doing. The word pseudocode comes from the Greek prefix "pseudo-" meaning fake and "code" for the actual computer programming statements. It is not created for the computer to use directly but it is made to help you understand the complexity of a problem and to break it down into meaningful pieces. There is no single best way to write pseudocode. Dozens of standards exist © 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Upload: others

Post on 10-Jun-2020

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 53

Chapter 4: Thinking Like a Programmer

One of the hardest things to learn is how to think like a programmer. A programmer is not created by simple books or classes but grows from within an individual. To become a "good" programmer takes passion for technology, self learning, basic intelligence, and a drive to create and explore.

You are like the great explorers Christopher Columbus, Neil Armstrong, and Yuri Gagarin (the first human in space). You have an unlimited universe to explore and to create within the computer. The only restrictions on where you can go will be your creativity and willingness to learn.

A program to develop a game or interesting application can often exceed several thousand lines of computer code. This can very quickly become overwhelming, even to the most experienced programmer. Often we programmers will approach a complex problem using a three step process, like:

1. Think about the problem.2. Break the problem up into pieces and write them down formally.3. Convert the pieces into the computer language you are using.

Pseudocode:

Pseudocode is a fancy word for writing out, step by step, what your program needs to be doing. The word pseudocode comes from the Greek prefix "pseudo-" meaning fake and "code" for the actual computer programming statements. It is not created for the computer to use directly but it is made to help you understand the complexity of a problem and to break it down intomeaningful pieces.

There is no single best way to write pseudocode. Dozens of standards exist

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 2: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 54

and each one of them is very suited for a particular type of problem. In this introduction we will use simple English statements to understand our problems.

How would you go about writing a simple program to draw a school bus (like in Illustration 13)?

Let's break this problem into two steps:

• draw the wheels• draw the body

Now let's break the initial steps into smaller pieces and write our pseudocode:

Set color to black.Draw both wheels.Set color to yellow.Draw body of bus.Draw the front of bus.

Table 4: School Bus - Pseudocode

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Illustration 13: School Bus

Page 3: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 55

Now that we have our program worked out, all we need to do is write it:

Set color to black. color blackDraw both wheels. circle 50,120,20

circle 200,120,20Set color to yellow. color yellowDraw body of bus. rect 50,0,200,100Draw the front of bus. rect 0,50,50,50

Table 5: School Bus - Pseudocode with BASIC-256 Statements

The completed school bus program (Program 25) is listed below. Look at thefinished program and you will see comment statements used in the program to help the programmer remember the steps used during the initial problem solving.

1 # c4_schoolbus.kbs2 # draw a school bus34 clg56 # draw wheels7 color black8 circle 50,120,209 circle 200,120,201011 # draw bus body12 color yellow13 rect 50,0,200,10014 rect 0,50,50,50

Program 25: School Bus

In the school bus example we have just seen there were many different ways

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 4: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 56

to break up the problem. You could have drawn the bus first and the wheels last, you could have drawn the front before the back,... We could list dozens of different ways this simple problem could have been tackled.

One very important thing to remember, THERE IS NO WRONG WAY to approach a problem. Some ways are better than others (fewer instructions, easier to read, …), but the important thing is that you solved the problem.

Flowcharting:

Another technique that programmers use to understand a problem is called flowcharting. Following the old adage of "a picture is worth a thousand words", programmers will sometimes draw a diagram representing the logic of a program. Flowcharting is one of the oldest and commonly used methodsof drawing this structure.

This brief introduction to flowcharts will only cover a small part of what that can be done with them, but with a few simple symbols and connectors you will be able to model very complex processes. This technique will serve you well not only in programming but in solving many problems you will come across. Here are a few of the basic symbols:

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 5: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 57

Symbol Name and Description

Flow – An arrow represents moving from one symbol or step in the process to another. You must follow the direction of the arrowhead.

TerminatorTerminator – This symbol tells us where to start and finish the flowchart. Each flowchart should have two of these: a start and a finish.

Process

Process – This symbol represents activities or actions that the program will need to take. There should be only one arrow leaving a process.

Input andOutput

Input and Output (I/O) – This symbol representsdata or items being read by the system or being written out of the system. An example would be saving or loading files.

Decision

Decision – The decision diamond asks a simple yes/no or true/false question. There should be two arrows that leave a decision. Depending on the result of the question we will follow one pathout of the diamond.

Table 6: Essential Flowcharting Symbols

The best way to learn to flowchart is to look at some examples and to try your own hand it it.

Flowcharting Example One:

You just rolled out of bed and your mom has given you two choices for breakfast. You can have your favorite cold cereal or a scrambled egg. If youdo not choose one of those options you can go to school hungry.

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 6: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 58

Take a look at Illustration 14 (above) and follow all of the arrows. Do you see how that picture represents the scenario?

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Illustration 14: Breakfast - Flowchart

Start

Scrambledeggs?

Eat.

Cereal?

Fix eggs.

Get bowl, milk,and cereal.

Yes

No

No

Yes

Finish

Page 7: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 59

Flowcharting Example Two:

Another food example. You are thirsty and want a soda from the machine. Take a look at Illustration 15 (below).

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Illustration 15: Soda Machine - Flowchart

Start

Do we haveenough change

for the machine?

Make selection.

Insert coin.

Yes

No

No

YesHave weInserted enough?

Sold out?No Yes

Get can.

Get change if any.

Drink.

Finish

Page 8: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 60

Notice in the second flowchart that there are a couple of times that we may need to repeat a process. You have not seen how to do that in BASIC-256, but it will be covered in the next few chapters.

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 9: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 61

Exercises:

z d s y m b o l t r pe m e w t a f r m r ty d k c l u a v o s ep q o z i h p g r p rx r i c c s r n r e mz f o w o a i e i t ia u o c m d x o u s nq l h m e p u p n q af o i q m s t e d u tb n m h r u s w s b og e p r o b l e m p r

decision, flowchart, input, output, problem, process, programming,pseudocode, steps, symbol, terminator

4.1. In complete sentences can you write out the steps to make apeanut butter and jelly sandwich. Assume that the peanut butter jar, jelly jar, loaf of bread, place, and silverware are on the table in front of you. Can another person, who has never seen a PBJ, successfully make one using your directions?

4.2. In a flow chart (or in a similar diagram) diagram the process you go through to open the front door of your hours or apartment. Do you have your keys? Is the door locked? Is it already open?

4.3. In pseudocode (short statements) can you write out directions from your school or work to the nearest restaurant or gas station. Don't cheat and look the directions up on-line. Will the same directions get you back the same way or do the instructions need to be changed?

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)

Page 10: Chapter 4: Thinking Like a Programmer - opfinderklubben.dk€¦ · Chapter 4: Thinking Like a Programmer Page 53 Chapter 4: Thinking Like a Programmer One of the hardest things to

Chapter 4: Thinking Like a Programmer Page 62

© 2014 James M. Reneau (CC BY-NC-SA 3.0 US)