Raptor Labs
Raptor Labs
Raptor Labs
During the execution of the previous example program, the variable X stored three distinct
values. Please note that the order of statements in a program is very important. If you re-
ordered these three assignment statements, the values stored into X would be different.
A variable can have its value set (or changed) in one of three ways:
By the value entered from an input statement.
By the value calculated from an equation in an assignment statement.
By a return value from a procedure call (more on this later).
It is variables, and their changing data values, that enable a program to act differently every
time it is executed.
All variables should be given meaningful and descriptive names by the programmer. Variable
names should relate to the purpose the variable serves in your program. A variable name must
start with a letter and can contain only letters, numerical digits, and underscores (but no
spaces or other special characters). If a variable name contains multiple "words," the name is
more "readable" if each word is separated by an underscore character. The table below shows
some examples of good, poor, and illegal variable names.
IMPORTANT: If you give each value in a program a meaningful, descriptive variable name,
it will help you think more clearly about the problem you are solving and it will help you find
errors in your program.
One way of understanding the purpose of variables is to think of them as a means to
communicate information between one part of a program and another. By using the same
variable name in different parts of your program you are using the value that is stored at that
location in different parts of your program. Think of the variable as a place holder or storage
area for values between each use in your program computations.
When a RAPTOR program begins execution, no variables exist. The first time RAPTOR
encounters a new variable name, it automatically creates a new memory location and
associates this variable name with the new memory. The variable will exist from that point in
the program execution until the program terminates. When a new variable is created, its
initial value determines whether the variable will store numerical data or textual data. This is
called the variable's data type. A variable's data type cannot change during the execution of a
program. In summary, variables are automatically created by RAPTOR and can hold either:
-4, 3.1415, 0.000371, or
Activity 1:
Understanding Program Structure
Solution:
A RAPTOR program is a set of connected symbols that represent actions to be performed.
The arrows that connect the symbols determine the order in which the actions are
performed. When executing a RAPTOR program, you begin at the Start symbol and follow
the arrows to execute the program. A RAPTOR program stops executing when the End
symbol is reached. The smallest RAPTOR program (which does nothing) is depicted at the
right. By placing additional RAPTOR statements between the Start and End symbols you
can create meaningful RAPTOR programs
Activity 2:
Getting Input from user
Solution:
An input statement/symbol allows the user of a program to enter a data value into a program variable
during program execution. It is important that a user know exactly what type of value is expected for
input. Therefore, when you define an input statement you specify a string of text that will be the prompt
that describes the required input. The prompt should be as explicit as possible. If the expected value needs
to be in particular units (e.g., feet, meters, or miles) you should mention the units in the prompt.
When you define an input statement, you must specify two things: a prompt and the variable that will be
assigned the value enter by the user at run-
right there are two types of input prompts: Text and Expression prompts. An Expression prompt enables
you to mix text and variables together like the following prompt:
At run-time, an input statement will display an input dialog box, an example of which is shown to the
right. After a user enters a value and hits the enter key (or clicks OK), the value entered by the user is
assigned to the input statement's variable.
Make sure you distinguish between the "definition of a statement" and the "execution of a statement".
The dialog box that is used to define a statement is totally different from the dialog box that is used at
run-time when a program is executing.
Activity 3:
Assigning value to a Variable:
Solution:
The assignment symbol is used to perform a computation and then
store the results in a variable. The definition of an assignment
statement is performed using the dialog box shown on the right. The
variable to be assigned a value is entering into the "Set" field, and the
computation to perform is enter into the "to" field. The example on the
right sets the value of the variable x to 0.707106781186547.
An assignment statement is displayed inside its RAPTOR symbol
using the syntax:
For example, the statement created by the dialog box to the right is displayed as:
One assignment statement can only change the value of a single variable, that is, the variable on the left
hand side of the arrow. If this variable did not exist prior to the statement, a new variable is created. If this
variable did exist prior to the statement, then its previous value is lost and its new value is based on the
computation that is performed. No variables on the right hand side of the arrow (i.e., the expression) are
ever changed by the assignment statement.
Activity 4:
Evaluating Expressions
Solution:
The expression (or computation) of an assignment statement can be any simple or complex equation that
computes a single value. An expression is a combination of values (either constants or variables) and
operators. Please carefully study the following rules for constructing valid expressions.
A computer can only perform one operation at a time. When an expression is computed, the operations of
the equation are not executed from left to right in the order that you typed them in. Rather, the operations
are performed based on a predefined "order of precedence." The order that operations are performed can
make a radical difference in the value that is computed. For example, consider the following two
examples:
In the first case, the variable x is assigned a value of 4, whereas in the second case, the variable x is
assigned the value of 6. As you can see from these examples, you can always explicitly control the order
in which operations are performed by grouping values and operators in parenthesis. The exact "order of
precedence" is
1. compute all functions, then
2. compute anything in parentheses, then
3. compute exponentiation (^,**) i.e., raise one number to a power, then
4. compute multiplications and divisions, left to right, and finally
5. compute additions and subtractions, left to right.
An operator or function directs the computer to perform some computation on data. Operators are placed
between the data being operated on (e.g. X/3) whereas functions use parentheses to indicate the data they
are operating on (e.g. sqrt(4.7) ). When executed, operators and functions perform their computation and
return their result. The following lists summarize the built-in operators and functions of RAPTOR.
basic math:
trigonometry
miscellaneous
The following table briefly describes these built-in operators and functions. Full details concerning these
operators and functions can be found in the RAPTOR help screens.
The result of evaluating of an expression in an assignment statement must be either a single number or a
single string of text. Most of your expressions will compute numbers, but you can also perform simple
text manipulation by using a plus sign (+) to join two or more strings of text into a single string. You can
also join numerical values with strings to create a single string. The following example assignment
statements demonstrate string manipulation.
RAPTOR defines several symbols that represent commonly used constants. You should use these
constant symbols when you need their corresponding values in computations.
is defined to be
is defined to be
Activity 5:
Showing Output
Solution:
Master Console window when it is executed. When you define an
output statement, the "Enter Output" dialog box asks you to
specify three things:
(computation)?
display?
The example output statement on the right will display the text,
"The sales tax is" on the output window and terminate the text
with a new line. Since the "End current line" is checked, any future
output will start on a new line below the displayed text.
When you select the "Output Text" option, the characters that you
type into the edit box will be displayed exactly as you typed them,
including any leading or trailing spaces. If you include quote
marks (") in the text, the quote marks will be displayed exactly as
you typed them.
When you select the "Output Expression" option, the text you type
into the edit box is treated as an expression to be evaluated. When
the output statement is executed at run-time, the expression is
evaluated and the resulting single value that was computed is
displayed. An example output statement that displays the results of an expression is shown on the right.
You can display multiple values with a single output statement by using the "Output Expression" option
and building a string of text using the string plus (+) operator. When you build a single string from two or
more values, you must distinguish the text from the values to be calculated by enclosing any text in quote
marks ("). In such cases, the quote marks are not displayed in the output window. For example, the
expression,
Notice that the quote marks are not displayed on the output device. The quote marks are used to surround
any text that is not part of an expression to be evaluated.
-friendly
Solution:
The RAPTOR development environment, like many other programming languages, allows comments to
be added to your program. Comments are used to explain some aspect of a program to a human reader,
especially in places where the program code is complex and hard to understand. Comments mean nothing
to the computer and are not executed. However, if comments are done well, they can make a program
much easier to understand for a human reader.
To add a comment to a statement, right-click your mouse over the statement symbol and select the
"Comment" line before releasing the mouse
button. Then enter the comment text into the
"Enter Comment" dialog box, an example of
which is shown to the right. The resulting
comment can be moved in the RAPTOR
window by dragging it, but you typically do
not need to move the default location of a
comment.
All the activities we have performed so far, we have done sequential execution
of the instructions. You have controlled which statement is executed next by
ordering them one after the other. Essentially you placed each statement in the order that you wanted
them to be executed. The first statement of a program that is executed is the first statement after the Start
statement. Once that statement is finished executing (i.e. the semantics associated with that statement
have been accomplished), then the statement immediately following that statement is executed. Once the
second statement is executed, the third is executed, and then the fourth, and so on until the End is reached.
As you can see by the inset diagram, the arrows linking the statements depict the execution flow. If you
have 20 programming statements in the instruction section, then when your program runs successfully, it
will execute those 20 statements in order and then quit.
Notice that you, as a programmer, have total control over which statements are executed before others,
merely by your placement of those instructions relative to each other in the instruction sequence. It is your
job as a programmer to determine the statement that is needed and its placement. Writing the correct
statement is one task. Determining where to place that statement is equally important. As an example,
when you want to get and process data from the user you have to the data before you can use it.
yet.
in the order you want them to be executed. Now, perform the following activities using the sequential
control structure.
Lab Task 1
A painter wants to know the amount of paint needed to paint only the walls and the interior side of the
door in a room. The chosen paint covers 100 square feet per gallon. There are two windows. Test the
problem with the following data:
The room is 12 feet long, 10 feet wide, and 8 feet tall.
The two windows are 5 by 3 feet, and 6 by 2 feet, respectively.
Lab Task 2
One of the jobs that Joe Roberts has been given at work is to order special paper for a report for a board
meeting. The paper comes in reams of 500 sheets. He always makes five more copies than the number of
people that will be there. Joe wants to know how many reams of paper he needs for a meeting. He can
order only whole, not partial, reams. Assume the required number of pages will not equal an exact
number of reams. Test your solution with the following data:
The report is 140 pages long.
There will be 25 people at the meeting.
Lab 07
Problem Solving with Decision Structure
Objective:
It will enable students to understand and use selection structures for data processing.
Activity Outcomes:
The students will be able to:
Solve problems related to selection control
Solve problems related to nested selection control
Instructor Note:
As a pre-
1) Useful Concepts
One of the most important parts of programming is controlling which statement will execute next. In this
reading, you will learn the statements, called control structures or control statements that enable you, as a
programmer, to determine the order in which your program statements are executed. Using these control
structures, you can determine the order that statements are executed and whether or not statements are
executed at all.
1. The if is a decision statement that allows the code to be executed based on a given
boolean condition.
There need not be a statement 2a or a statement 2b. Either path could be empty or could contain
several statements. It would be silly for both of them to be empty (or both have the exact same
statements), as your decision, Yes or No, would have no effect (nothing different would happen
based on the decision).
Solution:
The exact decision you write down affects whether the Yes or No paths are taken. It is very easy to
change the decision to switch the yes and no paths. For example, on the left below is a simple
list. Some people prefer the version on the left and some the version in the center. Very few prefer
the right because of the double negative implicit in the decision.
Activity 2:
Raptor flowchart to illustrate cascading decision structure
Solution:
Sometime you are not making a selection between two alternatives, but making a decision amongst
multiple alternatives. For example, if you are assigning a grade (A, B, C, D, or F) you need to select
between the multiple choices. The following RAPTOR program includes multiple selection
statements. If you follow the control logic, you can see how the selections cascade (as in water
cascading over a series of falls).
Activity 3:
The problem illustrated in this flowchart is to calculate the commission rate for a salesperson,
given the amount of sales. When the salesperson has sold less than or equal to $2,000 worth of
goods, the commission is 2%. When the sales total is more than $2,000 and less than or equal to
$4,000, the commission is 4%. When the sales total is more than $4,000 and less than or equal
to $6,000, the commission is 7%. When the person has sold more than $6,000, the commission is
10%.
Solution:
3. Graded Lab Tasks
Note: The instructor can design graded lab activities according to the level of difficulty and
complexity of the solved lab activities. The lab tasks assigned by the instructor should be evaluated
in the same lab.
Lab Task 1
Draw a flowchart to calculate the water bill given the cubic feet of water used for a Water Company,
which charges the homeowner one of the following:
1. A flat rate of $15.00 for usage up to and including 1000 cubic feet.
2. $0.0175 per cubic foot for usage over 1000 cubic feet and up to and including 2000 cubic feet.
3. $0.02 per cubic foot for usage over 2000 cubic feet and up to and including 3000 cubic feet.
4. A flat rate of $70.00 for usage over 3000 cubic feet.
Test your flowchart with actual data.
Lab Task 2
A company that issues check-cashing cards uses an algorithm to create card numbers. The algorithm
adds the digits of a four-digit number, and then adds a fifth digit of 0 or 1 to make the sum of the digits
even. The last digit in the number is called the check digit. Draw a flowchart to develop a solution that
accepts a four-digit number into one variable, adds the check digit, and prints the original number and
the new number. Test your flowchart with the following data:
Instructor Note:
As a pre-
1) Useful Concepts
Looping in programming languages is a feature which facilitates the
execution of a set of instructions/functions repeatedly while some
condition evaluates to true.
An iteration control statement controls how many times a block of code is
executed. Often you need to execute some code until a condition occurs.
As you may not know in advance how many times you will need to
number of times. Even if you did know how many times the code would
repeat, copying it that many times is not a good idea, as any error in your
code would be replicated that many times as well.
In the diagram on the right, statement 1 and statement 2a are always
executed. If the exit condition is true, then the loop exits and statement 3
is executed. If the exit condition is false the statement 2b and then
statement 2a are executed and the exit condition is checked again. As long
as the exit condition is false, statement 2b and then statement 2a will be
executed again and again. 9=]
The exit condition may never
later. You have to have some code, in statement 2a or statement 2b, that
makes a change that turns the exit condition to true, without that, if it
starts out being false, it will remain false forever.
As with the generic selection example, any of the statements in the
example could be replaced by several statements. As with the selection
statement, it is possible to nest loop statements. It is also possible to nest selection statements in loop
statements and vice versa.
Activity 1:
Raptor flowchart to illustrate Loop Control examples
Solution:
One of the most common uses of a loop is to validate user input. If
you want the user to input data that meets certain constraints, such as
entering a persons age (see example), or a number between 1 and 10,
then validating the user input will ensure such constraints are met before
processing continues.
As we do not know if, or how many times, the user will attempt to enter
data that does not meet the constraints, a loop must be used to ensure
that correct data is entered. The exit condition establishes exactly what
criteria must be met for the data to be validated.
Another common use of a loop is to execute code a specific number of
times. To do this you need to count the number of loop executions and
to exit. This type of loop is called a counter-controlled loop. In the
folowing example, the long box signifies some sort of processing that
must (in this example) be accomplished 100 times. The loop control
variable, in this example is Count. There is nothing magical about the
variable name Count, we could have used any legal, meaningful name.
The important thing is that the loop control variable must be initialized
before the loop, modified in the middle of the loop, and tested to see if the
loop has executed enough times. If any of these three elements are
missing, the loop will not execute the correct number of times.
Some loops are called infinite loops because the exit condition could
never be true. The three examples below are slight variations of the
counter-controlled loop example. Each of them has a problem which will
cause the loop to be infinite. See if you can spot the error in each piece of
code below.
Typically one or more variables are used to control whether the iteration
construct exits or loops again. The acronym I.T.E.M (Initialize, Test,
Execute, and Modify) can be used to check whether the loop and loop
control variable(s) are being used correctly. See if you can spot what is
wrong (I.T.E. or M.) with each of the code fragments below.
-
controlled loop example on the previous page.
Activity 2:
Raptor flowchart to illustrate sentinel controlled loop
Solution:
Sometimes you want users to enter a bunch of values that you can process. There are several
ways that this can be accomplished in RAPTOR. The first method is to have the user enter a
, called sentinel vaulue, that signifies they are finished entering data.
A second method is to ask the user in advance how many values that they will be entering and
then to use that value to control the loop that asks for the data. These two methods are depicted
below.
you only increment the Num_Positives variable when Data > 0 is true. By
replacing the guard test with some other test you can count some other event.
Activity 4:
Raptor flowchart combining decision and repitition controls
Solution:
The next example will be a complete program instead of snippets of code. We want to ask the user to type
in a number and then return all of the factors of the number or, if it has none, display that the number is
prime. Unfortunately, while the logic of the program that we write works well for small numbers, it would
take millions of years to find the factors for large 500 digit numbers.
One of the goals of CSC101 is to teach you how solve problems and program your solutions. Showing
you the code would not serve that purpose. Instead, we will describe the process by which the program
was developed, and then show you the code. By reading about the process of solving a problem like that
above, and then trying it out yourself in the lab, we hope to give you the skills to do it on your own.
A good way to start writing a program is to clearly understand what the inputs to the program are and
what outputs the program should produce. It often helps to have several specific examples of this
We want our program to exhibit the following behavior. If, for example, the user types in a 77, the
We can understand
and then use that process as the code for your program. Before you can develop a program for a problem
you must be able to do the following:
(1) Solve the problem yourself for particular instances of the problem (like 41 and 77).
(2) Be able to clearly describe the process by which you arrived at the answer, and have that process
be generic enough to be used for any number (not just the two examples).
It is critical that it be a general process. While you may be able to describe exactly how you determined
the factors of 77 and that 41 was prime that is not sufficient. You must be able to describe a process that
could find all of the factors of any number, and whether any number was prime. If you cannot do so then
it will be VERY difficult to write a program to do so!
The following code snippet is a naïve attempt to solve the problem:
Obviously there are too many different numbers that could be entered by the user for you to code all of
them. In addition, if you were to attempt to code a solution to the problem using code like that above, you
would be doing the grunt work of determining what the factors of a number were and whether it was
prime or not, not the computer. The point of using a computer is for it to do all of the tedious work. You,
as the programmer, do the work that requires brains. You need to come up with a process and then
instruct the computer by writing code that it can use to follow the process and arrive at a solution. Think
about how you would describe to anyone, let alone a computer, how to determine all of the factors of a
number and whether a number was prime or n
One way to determine the factors of a number, X, is to start at the number 2 and then check all of the
numbers between 2 and X - 1 to see if any evenly divide X. If none of the numbers in that range evenly
divide X then we know X is prime. If any numbers in that range do evenly divide X, then they are factors
and we can display them to the user.
The above text describes a process that could be used to determine the factors of any number and if it had
none declare the number to be prime. Sometimes just describing a process like that is good enough to start
coding from. In this case, because it is the first really complex program you are seeing, we will take the
narrative form of the process one step further and create an algorithm, using pseudo code, which makes
the steps in the process more like the eventual code.
-1
Rememb
1,
Once we have checked all of the numbers in the range we will have displayed all of the factors or there
were no factors. In the latter case we c
So far we have accomplished the first two steps of programming. We have understood the problem and
better characterized the problem by describing its input/output behavior. We have also designed a solution
to the problem that appears to work for all positive integers. The third step is to translate the solution
(algorithm) to RAPTOR code. The following code is the translated solution. We have annotated it to
make it easier for you to understand. Review the program and make sure you understand how the code
relates to the algorithm and the process we came up with for solving the problem.
factor
number.