Quick Check QUIZ!

advertisement
Basics
Menu
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Learning Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 2
Problem Solving
Steps in the Problem Solving Process . . . . . . . . . . . . . . . . . .page 3
Problem Solving Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 4
Demonstration of a Program . . . . . . . . . . . . . . . . . . . . . . . . . .page 7
Languages, Syntax and Semantics
What Do Human Languages Look Like? . . . . . . . . . . . . . . . page 9
Language Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 10
Grammar Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 11
Computer Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 14
Debugging
Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 22
Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .page 23
More About Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . page 25
Basics
Learning Objectives
Upon completion of this lesson, the student will be able to:
1. Identify the steps in the problem solving process.
2. Illustrate the steps in a flowchart.
3. Sketch an IPO chart and a flowchart for a given problem.
4. Discuss the Edit-Compile-Link-Execute process in creating a
program.
5. State the difference between syntax and semantics in the contest of
programming.
6. Compare reserved words and user-defined variables.
7. List the different types of errors possible in a program.
Basics
Problem Solving
Steps in the Problem Solving Process
Basics
Problem Solving
IPO Charts – Input Process Output Charts
The IPO chart lists the input data, the output produced and the process
followed to reach the output from the input.
Basics
Problem Solving
IPO Charts – Input Process Output Charts
•
Input is the data or information supplied to the program by the
user or the programmer
• Output is the information produced by the program after
processing the input. This could be sent directly to a file, printed to
the printer, displayed to the user on the screen or channeled as
input to another program
• The process involves the steps to be followed in order to process the
input and produce the correct output
IPO charts are used to clearly define the inputs, outputs and processes
to be used in the program. As you advance further in the course
you will see why this kind of planning is important. As the saying
goes “Weeks of programming can save you hours of planning” –
author unknown
Basics
Problem Solving
Example IPO Chart
This is an example of an IPO Chart of a very basic program that adds two numbers and
displays the result to the user. Note that the first column contains the input
variables, the second column contains the steps of the process and the third column
contains the output. This is the standard format of the IPO chart.
Basics
Problem Solving
IPO Charts can be created for any program whether it involves the
Sequence
Instructions are executed sequentially, one after the other. No
instruction is skipped or repeated
The selection
The program makes a decision based on the value in a particular
variable. Based on this decision, one logical path or the other is
followed
or The repetition control structure
Some or all of the instructions in the program can be repeated n
number of times. n is based on the needs of the application or
the user
Basics
Problem Solving
Pseudocode
Pseudocode is the program instructions written in Englishlike manner. The following is the pseudocode for the
example II.
input iPhone price from the user
if price < 300 then
output “you can afford the iPhone”
else
output “no luck, dude!”
endif
Basics
Problem Solving
Problem Solving tools
Flowcharts
A flowchart is a pictorial
representation of the logical flow
of control in an algorithm or
program.
An algorithm is a series of language
free, step-by-step instructions
detailing the flow of control in a
program
Basics
Problem Solving
Flowchart Symbols – Start/Stop
Basics
Problem Solving
Flowchart Symbols- Process
Basics
Problem Solving
Flowchart Symbols – Decision
The decision symbol is the only one that has two lines of
flow coming out of it. Based on the result of a decision,
control may go to True side or the False side
Basics
Problem Solving
Flowchart Symbols – Input/Output
Basics
Problem Solving
Flowchart Symbols – Function Call
Basics
Problem Solving
Edit-CompileLink-Execute cycle
in a program
Basics
Problem Solving
Demonstration Of A Program
IDE – Integrated Development Environment
The IDE does a number of things for us
•
Has a built-in programmer’s editor
•
Manages files that make up our program
•
Manages the Compile and Link process for us
•
Let’s us run the program with a simple click of the
mouse
•
Even let’s us Debug our programs
Basics
Problem Solving
Demonstration Of A Program
Video Source:
http://youtube.com/watch?v=K5HdaQ5b9cQ
An old story says one of the first computer
programs failed to work because of a moth in a
relay. We say we “debug” programs to this day!
Basics
Problem Solving
Language, Syntax and Semantics
What do Human languages look like?
Humans use languages to express themselves. We will use
languages to tell computers what we want them to do!
Basics
Problem Solving
Language, Syntax and Semantics
Language Components
Humans construct Sentences out of words and punctuation
using rules of Grammar/
Language
Language, Syntax and Semantics
Sentences are a sequence of words that convey some meaning to
another person
2. Words are the basic building blocks of our language – we learn their
definitions and spelling
3. Punctuation marks are special sequences of characters (not words)
we use in our sentences
4. Grammar rules tell us how to order the words and punctuation to
convey our desired meaning
We call words and punctuation marks Tokens 5
5. Just a fancy word for a basic language component
And you use tokens to say what you want to say!
Basics
Problem Solving
Language, Syntax and Semantics - Learning the
Grammar Rules
The basic rules:
•A sentence is made up of a noun part followed by a verb part
•A noun part can be a simple noun or a determiner followed by a noun
•A verb part can be a simple verb or a verb followed by a propositional
phrase
Once you learn the rules, you just do it correctly and don’t even think
about it!
Basics
Problem Solving
Language, Syntax and Semantics
Why these rules?
•You follow the rules so your sentences make sense!
•You want the reader to understand the semantics – or the
meaning of your sentence
Basics
Problem Solving
Language, Syntax and Semantics - Following the
rules
•If you put your words and punctuation marks together
according to the rules, the sentence should make sense to
the reader - If not, you do not know what the sentence
means!
- You say the Syntax is wrong
Go lake in the jump!
The words are right, but the rules were not followed, so the
Basics
Problem Solving
Language, Syntax and Semantics - Computer Languages
Computers cannot understand human languages - at least not yet!
•We need to create simple programming languages for computers
•We still need to use words, punctuation and rules!
•The computer languages have their own rules:
- We have syntax rules for forming statements. Syntax is a set of
language rules that must be followed to make the program correct
- We have semantics for each statement which specifies what the
program will do when that statement is processed
Basics
Problem Solving
Language, Syntax and Semantics - Statements, Identifiers,
Punctuation marks
• Sentences in a program are called statements
• Statements are formed using identifiers and punctuation marks that
follow the rules of the language. Identifiers are reserved by the
language or defined by the programmer
• Words are called identifiers in the program.
• Special marks for things like math operators, parentheses, end of
statement etc.
• An identifier that has a predefined meaning is a reserved word
• Words created by the programmer to name some place in the
program or some piece of data being manipulated
Basics
Problem Solving
Language, Syntax and Semantics - Identifiers
Some names in your program are reserved by the language you are
using. Other names you choose to name data in your program.
Reserved words are language specific names used for a variety of
purposes:
Cout – used to direct output to the screen
Cin – used to read information from the keyboard
Basics
Problem Solving
Language, Syntax and Semantics - User Defined Variables
• User defined names you choose according to language rules:
• Names must start with a letter, not a digit
• Names might be case-sensitive depending on the language you use
• No blanks allowed in your names
• No special characters except for an underscore
• Variable names should be meaningful and descriptive, yet succinct
• (none of these are allowed: %$#@!)
Basics
Problem Solving
Debugging - Errors
There are two main reasons that a program
will not execute or do what it is supposed to do
– either you have made a mistake in the usage
of the language or you have a mistake in the
logic. In either one of these scenarios, there is
an error, or mistake, in the program. The
errors of language are called Syntax errors and
the errors of logic are runtime errors.
Basics
Problem Solving
Debugging - Syntax Errors
A syntax error is when the programmer failed to follow the rules of the
language. These could be rules of grammar or rules of usage. Each
programming language has its own strict rules that must be followed.
If a syntax error is detected, it must be corrected for the successful
compilation of the program.
Basics
Problem Solving
Debugging - Sample Syntax Errors
1. error C2146: syntax error : missing ';' before identifier 'outfile' Error
executing cl.exe.
2. error C2065: 'cout' : undeclared identifier
3. error C2065: 'end1' : undeclared identifier
4. fatal error C1004: unexpected end of file found
Some common types of syntax errors you may get in a C++ program are shown above. Read on to see a brief explanation
1. A semicolon is missing on the line before the one the editor will be pointing to. C++ requires each executable statement to
end with a semicolon. The first number you see C2146 is the error message number. You can refer to the manual of the
programming environment to see explanations of error messages. Each message is referred to by a unique number
2. An undeclared variable has been found in the program. A variable must be declared before it can be used. If it an
identifier that is found in a preexisting library, then that library must be included in the program. The first number you
see C2065 is the error message number. You can refer to the manual of the programming environment to see
explanations of error messages. Each message is referred to by a unique number
3. An undeclared variable has been found in the program. A variable must be declared before it can be used. If it an
identifier that is found in a preexisting library, then that library must be included in the program. The first number you
see C2065 is the error message number. You can refer to the manual of the programming environment to see
explanations of error messages. Each message is referred to by a unique number
3. The source code has come to an unexpected end. This is probably because you are missing an ending brace somewhere.
The first number you see C1004 is the error message number. You can refer to the manual of the programming
environment to see explanations of error messages. Each message is referred to by a unique number
Basics
Problem Solving
Debugging - Should you correct syntax errors?
The compiler will be unable to create an executable file if a program has
syntax errors. If you have a syntax error, you must fix the error before you
recompile. It does not serve any purpose to recompile without fixing the
error. That would be the same as continuing to drive a car with a flat tire in
the hope that the tire will fix itself!!
In order to see where the compiler thinks the error is occurring, double
click the error message and the cursor will be positioned on the line where
the alleged error is. Be aware that the error may be occurring one or more
lines before the place the compiler points you to. Also, you should know
that sometimes one simple error can generate multiple error messages.
Once you fix the single error all the error messages will go away.
Basics
Problem Solving
Debugging - Warning Errors
There are fatal syntax errors and warning errors. Warning errors will
not stop the execution of the program. However, a program should not
have warning errors either.
A warning error means that the potential exists for something to go
wrong in the program at some later point of time. This picture is of a
low tire pressure warning light from a Cadillac – a warning that
something may go wrong as you continue driving down the road
Basics
Problem Solving
Debugging - Logical Errors
Your program compiles, links and executes fine. However, you get bad
results. As an example, you are writing a program that converts from
feet to inches. You compile it – hurray! It compiles.
Then you run it and try to convert 12 feet to inches. You should get 144
inches as a result. However, much to your misery, you get 1 inch as a
result. What is the reason? There is an error in the logic of your
program. In this case your program needs to calculate the inches as
follows: Result = feet * 12;
The number of feet entered by the user multiplied by 12. However, you
wrote the statement as follows: Result = feet / 12;
You divided instead of multiplying!
Basics
Problem Solving
Debugging -Reasons for Logical
Errors
Logical errors always occur as a
result of poor design and planning
Basics
Problem Solving
Debugging - Run Time Errors
Run time errors are the hardest to detect. These could be errors in the
program, in your machine or in the interface of the program with the
machine. All possible error sources must be examined although most
run time errors result from defective programming
Basics
Problem Solving
Group / Pair Practice
1. Break up into groups of 3 or 4 based on the
instructions given by your professor
Scenario: You have just returned from a great
class! You are hungry and decide to bake a
healthy, low-fat cake. You reach into your
pantry and find a conveniently placed box of
cake mix. You turn it over and start to read
the directions.
2. Identify the inputs into the recipe.
3. Describe the cake making process. Create a
flowchart or write the pseudocode of this
process.
4. List the outputs.
5. Describe what could go wrong in the inputs,
the process and the output.
Basics
Problem Solving
Quick Check QUIZ!
The first step in the problem solving loop is:
Evaluate the
results
Build and test the idea
Identify the problem
Document what is
not there
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Input is:
Debugging the code to
find the errors
The steps to be followed to
produce the correct output from
the input
Affording an iPhone
Combination of planning,
analysis and design in the
problem solving loop
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Pseudocode is the programs instructions written in English-like
manner
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
A flowchart is:
The flow of traffic on a
highway
A diagram showing how to use
the IDE
A pictorial representation of the logic of
only the repetition control structures
A pictorial representation
of the logical flow of
control in a program
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
A linker:
Combines parts of a
program into a single
executable file
Causes the web browser to
follow a link in your program
Is a programming language
Is a type of compiler
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
If you use an IDE to write programs, you do not need a standalone
compiler to process the code.
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
There are no specific rules in programming languages that you must
follow when you create names for data:
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
A valid name for will hold the price for a car would be:
CarPrice
$Car Price
707carPrice
X-24
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
What does the term “semantics” mean in programming?
The correct way to write a
particular statement in a
programming language
The meaning of a statement in
the language
A special letter to your friend
Rules for user defined
names
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Programming languages use grammar rules to specify how to build
valid statements. Failing to follow these rules leads to :
Programs that will not
run correctly
Programs that cannot be linked
Syntax errors in your program
Warnings that your
program will run with
unpredictable results
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Which of the following is a valid literal value?
CarPrice
IF
XP42
123.0
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Reserved words:
Are used to prevent you
from picking names that
are worthless
Have a specified meaning in the
language and cannot be
redefined
Can be used to reserve a hotel room
Can be used to name data
in your program
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Microsoft Word is a good tool to use to write computer programs:
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
When you write a program in a language like C++, all of the code your
program will need to use must be written by you.
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Debugging is:
Making sure there are no
moths inside your
computer
Removing problems in your
program that prevent it from
producing the correct output
Linking source code to the data file
Converting your program
into another language that
the computer can actually
process it
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Quick Check QUIZ!
Computers can run programs written in C++ by loading the file
containing your source code into memory:
TRUE
OR
FALSE
TRY AGAIN!
CORRECT!
Basics
Problem Solving
Congratulations
on completing Basic problem solving
MAIN MENU
Download