Translate

Search This Blog

Total Pageviews

Saturday, August 29, 2015

Running With RAPTOR

When developing a program, it is essential to plan out the program, normally using pseudocode. RAPTOR  allow you to practice this pseudocode and give you a chance to see a “real-life” application of programming skills.

RAPTOR is a visual programming environment based on flowcharts that are created to solve particular programming problems.
Normally, programmers use flowcharts to design a program or part of a program before writing the actual code. RAPTOR allows you to take flowcharting one step further by allowing you to test (run) your program design and to confirm that your logic works correctly to solve your problem.
In a programming logic course, RAPTOR is useful for several reasons. Compared with other programming languages, the RAPTOR language has minimal syntax (grammar), which means you spend less time learn it and debugging syntax errors.
You can concentrate on the most important aspect of programming: developing good logic and design. Also, the RAPTOR program is visual because it uses diagrams that allow you to understand the flow of control of the statements in your program.
RAPTOR screens
Several versions of RAPTOR are available. Download the last release (actually 4.0.6.004 from FALL 2014 VERSION (Updated 22 April 2015)) exist also portable version  and older releases too.
After you download and install RAPTOR, you can open the program by locating the little RAPTOR icon on your computer. You will see two screens(see Figure):
  • the RAPTOR work area screen 
  • and the MasterConsole screen .
When you first open RAPTOR your MasterConsole may be shrunk or hidden behind the main screen and you may have to click it to reveal it. You can resize the screens as you wish; it’s convenient to view them side-by-side.

When you execute a RAPTOR program you're in main tab, the program begins(refer to Work area) at the Start symbol at the top and follows the arrows to the End symbol(that's a template one can delete that symbols).
That's the smallest RAPTOR program (which does nothing). By placing additional RAPTOR statements between the Start and End symbols you create more meaningful RAPTOR programs.

 Prompts you after drag one symbol onto
the  Work area
To create a RAPTOR program, you drag instances(after dragging the flowcharting symbol change his colour from blue to red to show you the actual status) of each flowcharting symbol from the left side to the middle area(the joint point is shown trough a change in the mouse shape from ? to normal with a + to the right then you can drop the symbol) of the screen, as shown in Figure. RAPTOR will prompt you to save your file as soon as you drag one symbol onto the screen(Work area). Then you can begin to create your program. 

Backups

RAPTOR makes backup files of your programs automatically. After you work on a program for some time, if you look in Windows Explorer or MyComputer, you will see a group of files under your RAPTOR file. The RAPTOR file has the extension .rap and is identified by the RAPTOR icon. The backup files are named with the same name as your program but include .backup0, .backup1, and so on.
These files can be ignored and, when you are finished creating your program, can be deleted.
Entering information into a dialog box
To enter information into a symbol that you have dragged to the screen, double-click it. A dialog box (a small window) will open, which allows you to enter the information that is possible or required for that symbol. E.g. for an Assignment box, as shown in Figure, double-clicking the box will open the dialog box. Assignment boxes allow you to enter information about variables.


Variables

The identifiers in raptor aren't case sensitive so the identifier x is considered the same as X , so the statement Set X = 32 assigns the value of 32 to the variable named X.
In RAPTOR, an arrow (in Ubuntu can't notice any arrow except a space between an R-value and L-value) pointing left  assigns the value on the right-hand side to the variable named on the left-hand side. Therefore, X  32 assigns the value of 32 to the variable named X.
In Figure, the box directly under the Start symbol (X  32) assigns the value 32 to the variable X.
The next statement, X  X + 1 says “X now is assigned the value it had (32) plus 1.” So, after that statement executes, the previous value of
X is replaced by the value 33.
The next statement, X  X * 2 says “assign to the variable X the present value of X multiplied by 2.” In this case, X had the value 33 and, after the statement is executed, it contains the value 66(Notice it in the Watch window).
All RAPTOR variables are of type Number or String.
A Number can be either a whole number (an integer) or a decimal number (a floating point number).
RAPTOR does not distinguish between number types.
A String is a text value into " ", like "Hello, friend" or "pony" or a single character like "e" or "W" or even a number, as long as the number is not used for any computations. For example, a ­ telephone number such as "352-381-3829" is a String.
A variable can have its value set or changed/updated in RAPTOR by one of the three ways:
  • The value can be entered from an Input statement
  • The value can be calculated from an equation in an Assignment statement
  • The value can be returned from a procedure Call (more on this later)
As in most languages, it’s necessary to associate several things with a new variable.
A variable must be given:
  • a name, 
  • a data type, 
  • and often an initial value.

Declaring variables

When a RAPTOR program begins its 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. Therefore, in RAPTOR you don’t need to declare variables explicitly, as we probably do in the pseudocode with Declare statements.
The variable will exist from the point at which it is created in the program execution until the program terminates.

Initializing variables

Attempts to use a variable named X before it is created(had
been assigned any value).
When a new variable is created in RAPTOR, it must be given an initial value. This is slightly different from other languages where it is possible to declare a variable without giving it a value at that time.
The initial value in RAPTOR determines the variable’s type. If the value is a number, the variable is of type Number. If the initial value is text, the variable is of type String.
Note that any initial value that is enclosed in quotes is considered text(String) e.g. :
  • If the variable myNum is set to 34, it is stored with the Number type.
  • If the variable myWord is set to "34", it is stored with the String type.
A variable’s data type cannot change during the execution of a program.
Beware! While a variable in RAPTOR can be created “on the fly” (i.e., by simply entering a variable name and giving it a value), a variable cannot be used until it has been created(see Figure).

Common Errors and Their Causes

     The variable Y has not been given a value
Error 1: Variable ____ doesn't have a value.

There are two common reasons for this error:







     The variable (Mile) name is misspelled













Error 2: Variable ___ not found!
This error means you have tried to use a variable before you have declared it or assigned it an initial value(like the first case in Error 1).

Error 3: Can’t assign string to numeric variable _____.
Can’t assign numeric to string variable _____.

This error occurs if your statements attempt to change the data type of a variable.
In newer versions of RAPTOR, you may be allowed to change a data type “on the fly” but this is never a good programming practice.













RAPTOR Symbols

 3 of the 6 RAPTOR symbols and instructions
Now lets provide details about each of the basic symbols: Input,
Assignment, and Output. The other symbols will be covered later.






Input Symbol

Every programming language has statements that enable the program to get information from the user via the keyboard or mouse and display that information on the computer screen. This allows the user to enter instructions to the program or supply the program with necessary information.
In RAPTOR, when an Input symbol is executed, a prompt(as dialog window) is displayed prompting the user to enter some value; the user then enters a value which is stored in a given variable. The Enter Input dialog box (see Figure ) shows what is displayed when you drag an Input symbol onto your flowchart and double-click in the symbol.
In the Enter Prompt Here box at the top, you enter the text you want the user to see. The text typed in this box will be displayed to the user when the program is run and, therefore, should explain what information needs to be input. This text must be enclosed in quotes. Make sure your prompt is clear and complete.
The Enter ­Variable Here box holds the name of the variable where the information the user enters will be stored. You can enter the name of a variable you have previously declared or you can declare a new variable and enter it here.
  • The information entered will be stored in a variable and used later in the program. For example, if you are calculating the cost of buying widgets and the user enters $1.50 for the price of one widget, this will be stored in a String variable called Price; the dollar sign ($) prevents it from being considered a Number. This will give you an error when you use Price in a calculation. Since Price must be a Number, the dollar sign cannot be part of the user’s
    entry. Be sure to tell the user not to include units when entering numbers.
    For example, a good prompt might read, "Enter the price of a widget, without the $ (i.e., 1.50 for a widget that costs $1.50)."
     
  • If you are asking the user to enter a text response, you must be specific. For example, if you want the user to choose yes or no, you must tell the user what to type because, to a computer, "yes" is not the same as "y" or "YES". A good prompt for this example might read, "Do you want to continue? Type 'y' for yes or 'n' for no."


Nested Quotes

At this point it is helpful to point out a situation that occurs frequently when coding an output text. Imagine you want the following output to explain to the user how to enter a choice:
Do you want to play the game again? Type "y" for yes, "n" for no.
Often the output includes some text that needs to be wrapped in quotes. However, a String, to the computer, is any text enclosed in quotes. The computer understands that the beginning of a String starts with the first quote symbol (") and ends when it encounters a second quote symbol ("). If the output text shown above was to be stored in a variable named YourChoice and was coded as follows:
YourChoice = "Do you want to play the game again? Type "y" for yes,
"n" for no."
the computer would process "Do you want to play the game again? Type" as the String to be stored in YourChoice and then display an error because the next character, y, is unintelligible to a computer interpreter or compiler.
That is why we not must nest quotes within a String. We must use a different symbol for internal quotes (those that are part of the String text) than the outer quotes, which mark the beginning and end of the String. In this case, a single quote (') indicates that we are still inside the original text String. In some languages, the outer quotes must be double quotes ("), while in other languages double quotes can be nested inside single quotes as well as the other way around.
The correct way to write the code for the YourChoice variable, then is:
YourChoice = "Do you want to play the game again? Type 'y' for yes,
'n' for no."

Using the Input Box to Change the Value
of a Declared Variable



















Using the Input Box to Declare and Initialize
a Variable





















Assignment Symbol

The Assignment symbol is used to set the value of a variable or to perform a computation and then store the results in a variable. The value stored in the variable can be retrieved and used in a statement later in the program.
The dialog box shown in Figure is used to enter the name of the variable being assigned and its value.
The variable name is entered in the small text box: Set ________.
The value is entered in the large text box: to ________. This value can be a number, character, or a string of text. If this is the first time the variable has been used, its type is set by the value you enter. If you type a number, the variable becomes a Number type.
You can also type in a computation for the value of a variable. The result will be assigned to the variable.

Here is how RAPTOR processes an Assignment statement:
  • First, the expression on the right-hand side of the Assignment operator is evaluated.
  • Then, that value is placed in the memory location associated with the variable on the left side, replacing whatever data value had been stored there previously.

Expressions

The expression (or computation) of an Assignment statement can be any simple or complex equation that results in a single value. An expression is a combination of values (either constants or variables) and operators. A computer can perform only one operation at a time. RAPTOR follows the same mathematical order as used in other languages.
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 result of evaluating an expression in an Assignment statement must be either a single number or a single string of text. While many expressions will compute numbers, you can also perform simple text manipulation in RAPTOR by using a plus sign (+) to join (concatenate) 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 Assignment statement demonstrates string manipulation:
Full_name  = "Joe " + "Alexander " + "Smith"
The value of Full_name, after this Assignment statement, is Joe Alexander Smith.

Output Symbol

In RAPTOR, an Output statement displays a value to the MasterConsole screen when it is executed.
When you define an Output statement by clicking on the Output symbol in your flowchart, the Enter Output dialog box asks you to specify the text or expression to display and whether or not the output should be terminated by a newline character.
If you check the End current line box, any future output will start on a new line below the displayed text.
You can output text, the value of a variable, or any combination of text and variables.
Note: For the user we cannot just list the values of the variables; we need to add some explanation of what these values represent in the MasterConsole screen.

Comments

The RAPTOR development environment, like many programming languages, allows comments to be added to your program. All good programs include comments if the code itself is not self-explanatory. While in the beginning, comments often seem redundant or even downright silly since beginning programs are small and not particularly complex, it is a good idea to get into the habit of including comments.
To add a comment to a symbol in your flowchart, right-click the mouse button on the statement symbol(rclick in Linux get mostly in an unhandled exception so use Edit > Comments in Menu bar ) and select the Comment line before releasing the button. Then, enter the comment text into the Enter Comment dialog box. The resulting comment can be moved in the RAPTOR window by dragging it, but normally you would not need to move the default location of a comment.
Typically, you should not comment every statement in a program but should include comments before each new section of a program.

Documentation Is Always Important

Often a program can achieve the desired results in one of several ways. The decision about which code to use is up to you, the programmer. While many examples are short and simple,real programs may be much longer and more complicated.
P­rofessional programmers often work on code written by other people. It is easier for a programmer to edit someone else’s code if the programmer knows what the segment is designed to do. This is one reason why it is so important to use documentation in the form of comments to explain what your code is doing.

The Assignment Operator and the Comparison Operator in RAPTOR

RAPTOR allows you to use either a single or a double equals sign to indicate a comparison within a test condition. RAPTOR “understands” that a single equals sign indicates comparison by its placement in the diamond shape. RAPTOR also accepts the double equals sign as a comparison operator.
For example, if the condition you wish to check is “Is the value of Count equal to 5?” you could enter either of the following into the Enter Selection Condition  Count = 5 or Count == 5 . 
It's better use the double equals sign to indicate a comparison because it
is an important distinction which, in a typical programming language, can wreak havoc on a program if used incorrectly.

The Call Symbol and Subcharts

The Call symbol, is used to call submodules from the main program, as these submodules, named subcharts in RAPTOR, are needed.
Subcharts help break a RAPTOR program into logical parts, called as needed by the main program, to simplify design, ensure that flowcharts are manageable, and reduce the chance for errors.

When you start a RAPTOR program, you’ll see a main tab in the upper-left corner of the work area. To create a subchart, simply right-click the main tab and select Add subchart from the menu. Enter the name of your subchart and a new editing window will appear. The name you give your subchart is automatically created in this new window after you click yes when prompted.
 a new subchart named Calculations
Now you can begin to build your subchart. After you have created your program, to call a subchart, simply insert the Call symbol where you want it in your program(main tab), and enter the name of the subchart that you want it to be called.

A subchart may be called from the main flowchart or from other subcharts or even from within itself but be careful if you decide to try this! It can easily result in an infinite loop (a loop that runs forever).
As the program runs, when the Call is encountered, control will transfer to the subchart. After all the steps in the subchart are completed, control will automatically return to the next symbol after the Call.

In RAPTOR, each variable declaration uses a separate Assignment symbol. Initializing more than a few variables results in a long list of Assignment boxes that do not have much to do with the logic of the program. Therefore, for convenience, we can call a RAPTOR subchart as soon as the program begins, which will contain all our initial variable declarations. It keeps this long list of Assignment boxes out of the way and allows us to focus on the rest of the program.
You will now see your main work area and a new tab at the top, named Variables. You can click on the Variables tab and begin creating variables.
However, it is not necessary and, if you wish, you can initialize your variables in the main program. Since a variable’s data type in RAPTOR is created when the variable is given a value, we will create most of our number variables by giving them an initial value of 0 and most string variables by giving them an initial value of an empty space (" "). Some variables, our named constants, have predetermined values that will not change as the program runs. We can set these values now.
Testing the program at each submodule makes debugging a great deal easier than waiting for the end when you have many more combinations of tests to run and where it may be difficult to pinpoint the source of a bug. In RAPTOR, as you run your tests, look at the watch window on the left side to be sure that your variables are being assigned the correct values.


Resources

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.