www.esnips.com/user/vapremaims Page 1
Introduction to Programming with RAPTOR By V.A. PREM KUMAR.
B.Com,M.C.A,(M.Tech(CSE)).
What is RAPTOR?
RAPTOR is a visual programming development environment based on flowcharts. A flowchart is a collection of connected graphic symbols, where each symbol represents a specific type of instruction to be executed. The connections between symbols determine the order in which instructions are executed. These ideas will become clearer as you use RAPTOR to solve problems.
We use RAPTOR in CS110 for several reasons.
The RAPTOR development environment minimizes the amount of syntax you must learn to write correct program instructions.
The RAPTOR development environment is visual. RAPTOR programs are diagrams (directed graphs) that can be executed one symbol at a time. This will help you follow the flow of instruction execution in RAPTOR programs.
RAPTOR is designed for ease of use. (You might have to take our word for this, but other programming development environments are extremely complex.)
RAPTOR error messages are designed to be more readily understandable by beginning programmers.
Our goal is to teach you how to design and execute algorithms. These objectives do not require a heavy-weight commercial programming language such as C++ or Java.
RAPTOR Program Structure
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.
Introduction to RAPTOR Statements/Symbols
RAPTOR has six (6) basic symbols, where each symbol represents a unique type of instruction. The basic symbols are shown at the right. The top four statement types, Assignment, Call, Input,
www.esnips.com/user/vapremaims Page 2
and Output, are explained in this reading, The bottom two types, Selection and Loops, will be explained in a future reading.
The typical computer program has three basic components:
INPUT – get the data values that are needed to accomplish the task. PROCESSING – manipulate the data values to accomplish the task. OUTPUT – display (or save) the values which provide a solution to the
task. These three components have a direct correlation to RAPTOR instructions as shown in the following table.
Purpose Symbol Name Description
INPUT
input statement
Allow the user to enter data. Each data value is stored in a variable.
PROCESSING
assignment statement
Change the value of a variable using some type of mathematical calculation.
PROCESSING
procedure call Execute a group of instructions defined in the named procedure. In some cases some of the procedure arguments (i.e., variables) will be changed by the procedure's instructions.
OUTPUT
output statement
Display (or save to a file) the value of a variable.
The common thread among these four instructions is that they all do something to variables! To understand how to develop algorithms into working computer programs, you must understand the concept of a variable. Please study the next section carefully!
RAPTOR Variables
Variables are computer memory locations that hold a data value. At any given time a variable can only hold a single value. However, the value of a variable can vary (change) as a program executes. That's why we call them "variables"! As an example, study the following table that traces the value of a variable called X.
www.esnips.com/user/vapremaims Page 3
Description Value of X Program
When the program begins, no variables exist. In RAPTOR, variables are automatically created when they are first used in a statement.
Undefined
The first assignment statement, X←32, assigns the data value 32 to the variable X.
32
The next assignment statement, X←X+1, retrieves the current value of X, 32, adds 1 to it, and puts the result, 33, in the variable X.
33
The next assignment statement, X←X*2, retrieves the current value of X, 33, multiplies it by 2, and puts the result, 66, in the variable X.
66
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.
www.esnips.com/user/vapremaims Page 4
Good variable names Poor variable names Illegal variable names
tax_rate
sales_tax
distance_in_miles
mpg
a (not descriptive)
milesperhour (add underscores)
my4to (not descriptive)
4sale (does not start with a letter) sales tax (includes a space) sales$ (includes invalid character)
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:
Numbers e.g., 12, 567, -4, 3.1415, 0.000371, or Strings e.g., “Hello, how are you?”, “James Bond”, “The value of x is ”
Common errors when using variables:
Error 1: "Variable ____ does not have a value"
There are two common reasons for this error:
1) The variable has not been given a value. 2) The variable name was misspelled.
www.esnips.com/user/vapremaims Page 5
Error 2: "Can't assign string to numeric variable _____"
"Can't assign numeric to string variable _____"
This error will occur if your statements attempt to change the data type of a variable.
RAPTOR Statements/Symbols
The following four sections provide details about each of the four basic statements: Input, Assignment, Call, and Output.
Input Statement/Symbol
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
www.esnips.com/user/vapremaims Page 6
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-time. As you can see by the “Enter Input” dialog box at the 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: “Enter a number between ” + low + “ and ” + high + “: ”.
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.
www.esnips.com/user/vapremaims Page 7
Assignment Statement/Symbol
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:
Variable ← Expression
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.
Expressions
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.
www.esnips.com/user/vapremaims Page 8
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:
x ← (3+9)/3 x ← 3+(9/3)
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: +, -, *, /, ^, **, rem, mod, sqrt, log, abs, ceiling, floor
trigonometry: sin, cos, tan, cot, arcsin, arcos, arctan, arccot
miscellaneous: random, Length_of
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.
Operation Description Example
+ Addition 3+4 is 7
- Subtraction 3-4 is -1
- Negation -3 is a negative 3
* Multiplication 3*4 is 12
/ Division 3/4 is 0.75
www.esnips.com/user/vapremaims Page 9
^
**
exponentiation, raise a number to a power
3^4 is 3*3*3*3=81
3**4 is 81
rem
mod
remainder (what is left over) when the right operand divides the left operand
10 rem 3 is 1
10 mod 4 is 2
Sqrt square root sqrt(4) is 2
Log natural logarithm (base e) log(e) is 1
Abs absolute value abs(-9) is 9
Ceiling rounds up to a whole number ceiling(3.14159) is 4
Floor rounds down to a whole number floor(9.82) is 9
Sin trig sin(angle_in_radians) sin(pi/6) is 0.5
Cos trig cos(angle_in_radians) cos(pi/3) is 0.5
Tan trig tan(angle_in_radians) tan(pi/4) is 1.0
Cot trig cotangent(angle_in_radians) cot(pi/4) is 1
Arcsin trig sin-1(expression), returns radians arcsin(0.5) is pi/6
Arcos trig cos-1(expression), returns radians arccos(0.5) is pi/3
Arctan trig tan-1(y,x), returns radians arctan(10,3) is 1.2793
Arccot trig cot-1(x,y), returns radians arccot(10,3) is 0.29145
Random generates a random value in the range [1.0, 0.0)
random * 100 is some value between 0 and 99.9999
Length_of returns the number of characters in a string variable
Example ← "Sell now" Length_of(Example) is 8
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.
Full_name ← "Joe " + "Alexander " + "Smith"
Answer ← "The average is " + (Total / Number)
www.esnips.com/user/vapremaims Page 10
RAPTOR defines several symbols that represent commonly used constants. You should use these constant symbols when you need their corresponding values in computations.
pi is defined to be 3.14159274101257.
e is defined to be 2.71828174591064
Procedure Call Statement/Symbol
A procedure is a named collection of programming statements that accomplish a task. Calling a procedure suspends execution of your program, executes the instructions in the called procedure, and then resumes executing your program at the next statement. You need to know two things to correctly use a procedure: 1) the procedure's name and 2) the data values that the procedure needs to do its work, which are called arguments.
RAPTOR attempts to minimize the number of procedure names you need to memorize by displaying any procedure name that partially matches what you type into the "Enter Call" window. For example, after entering the single letter "d," the lower portion of the window will list all built-in procedures that start with the letter "d". The list also reminds you of each procedure's required arguments. In the example to the right, the lower box is telling you that the "Draw_Line" procedure needs 5 data values: the x and y coordinates of the starting location of the line, (x1, y1), the x and y coordinates of the ending location of the line, (x2, y2), and the line's color. The order of the argument values must match the arguments defined by the procedure. For example, Draw_Line(Blue, 3, 5, 100, 200) would generate an error because the color of the line must be the last argument value in the argument list.
When a procedure call is displayed in your RAPTOR program you can see the procedure's name and the argument values that will be sent to the procedure when it is called.
www.esnips.com/user/vapremaims Page 11
For example, when the first procedure call on the right is executed it will draw a red line from the point (1,1) to the point (100,200). The second procedure call will also draw a line, but since the arguments are variables, the exact location of the line will not be known until the program executes and all the argument variables have a value.
RAPTOR defines too many built-in procedures to describe them all here. You can find documentation on all built-in procedures in RAPTOR's help screens. In addition, your instructor will introduce relevant procedures as we tackle various problem solving tasks in the coming lessons.
Output Statement/Symbol
In RAPTOR, an output statement displays a value to the MasterConsole window when it is executed. When you define an output statement, the "Enter Output" dialog box asks you to specify three things:
Are you displaying text, or the results of an expression (computation)?
What is the text or expression to display? Should the output be terminated by a new
line character? 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.
www.esnips.com/user/vapremaims Page 12
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,
"Active Point = (" + x + "," + y + ")"
will display the following if x is 200 and y is 5:
Active Point = (200,5)
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.
Your instructor (or a homework assignment) will often say “Display the results in a user-friendly manner”. This means you should display some explanatory text explaining any numbers that are output to the MasterConsole window. An example of "non-user-friendly output" and "user-friendly output" is shown below.
Non-user-friendly output User-friendly output
Example output: 2.5678 Example output: Area = 2.5678 square inches
www.esnips.com/user/vapremaims Page 13
Comments in RAPTOR
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.
There are three general types of comments:
Programmer header – documents who wrote the program, when it was written, and a general description of what the program does. (Add to the "Start" symbol)
Section description – mark major sections of your program to make it easier for a programmer to understand the overall program structure.
Logic description – explain non-standard logic. Typically you should not comment every statement in a program. An example program that includes comments is shown below.
Start
Get inputs"Enter the cylider radius"
GET radius
"Enter the cylinder height"
GET height
volume ← pi * radius ^ 2 * height
surface_area ← 2 * pi * radius ^ 2 + 2 * pi *
radius * height
Output resultsPUT "Volume = " + Volume + " cube
units"¶
PUT "Surface area = " + Surface_area + "
square units"¶
End
Note: the surface area includes the top and bottom ends
Calculations
Written by: C4C Grant LogicDate: 8 July 2006Description: Calculate properties of a cylinder
www.esnips.com/user/vapremaims Page 14
What you have hopefully learned…
The basic structure and types of statements of a RAPTOR program.
What a variable is and how variables are used.
How to write computations (i.e., expressions) that calculate desired values.
How to get input values into a program and how to display output values.
How to add appropriate comments to make a program more readable.
Reading Self-Check
1. Label the following RAPTOR identifiers as (G) good, (P) poor, or (I) Illegal. If illegal then explain why.
____ 1) This_Is_A_Test
____ 2) U_2
____ 3) Money$
____ 4) Thisisanawfullylongidentifiername
____ 5) Mickey-Mouse
____ 6) 365_Days
____ 7) Variable
____ 8) Is This Identifier Legal
____ 9) Why_Isn’t_This_One_Legal
2. Why are comments important?
3. True or False. In RAPTOR, a variable does not have a value (in its memory location) until a program instruction gives it a value.
www.esnips.com/user/vapremaims Page 15
4. Calculate the result of the following expressions (or indicate if the expression contains errors)
Result
__________ 1) 46 / 2
__________ 2) 4 + 6 * 2
__________ 3) 12 / 3 / 2
__________ 4) (4 + 2) / (5 – 3) + 2
__________ 5) 46 / 3
__________ 6) 46 rem 3
__________ 7) 3(4 + 3)
__________ 8) 6 ** sqrt(4)
__________ 9) 77 + -11
www.esnips.com/user/vapremaims Page 16
COMPUTATIONAL THINKING SOLUTIONS
Module 2 Conditions Tasks
Task 1
Write a program to add two integers A and B.
Test Cases Input A Input B Output
1 20 10 30
2 30 20 50
3 40 20 60
WBS – Templete- Module Step 1: Identifying the problem
Input Input type is Integer, input variables are A,B Output Output type is integer, out variable is C Specification Map f(C)=A+B Constraints No Constraint Specified
Step 2: Constructing a solution
Input initialization A=0 and B=0 Output initialization C=0 Other variables initialization None State(Input, Output and other variables) State(A,B,C)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
www.esnips.com/user/vapremaims Page 17
Manager’s Initialization
A=user input,B=user input
Work Schedule
Worker Test Action Transfer control
W0 C=A+B W1
W1 Display the C value “STOP”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(20,10)=30 f(30,20)=50 f(40,20)=60 For the test cases given the test terminates
Test the Correctness f(20,10)=30 f(30,20)=50 f(40,20)=60 For the test cases given the return is correct
Prove the Termination The program does not have any loops and it consist of one step. So the program terminates.
Prove the Correctness The program is correct because it takes two integers inputs A,B and calculate the C value.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 18
Task 1 Output Module 2 Conditions
www.esnips.com/user/vapremaims Page 19
Task 2
Write a program to compute the square of an integer x.
Test Cases Input X Output
1 2 4
2 6 36
3 5 25
WBS-Templete Module Step 1: Identifying the problem
Input Integer Type Input variable x Output Integer type output variable s Specification Map f(s)=x*x Constraints No Constraints specified
Step 2: Constructing a solution
Input initialization x=0 Output initialization S=0 Other variables initialization None State(Input, Output and other variables) State (x,s)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
X=User Input,s=0
www.esnips.com/user/vapremaims Page 20
Work Schedule
Worker Test Action Transfer control
W0 s=x*x W1
W1 Display s “STOP”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(2)=4 f(6)=36 f((5)=25 For the test cases given the test terminates
Test the Correctness f(2)=4 f(6)=36 f(5)=25 For the test cases given the return is correct
Prove the Termination The program does not have any loops and it consist of one step. So the program terminates.
Prove the Correctness The Program is correct because it takes one integer
input and give the correct output Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 21
Task 2 Output Module 2 Conditions
www.esnips.com/user/vapremaims Page 22
Task 3
Write a program to find the bigger of 2 integers A and B.
Test Cases Input A Input B Output
1 4 19 19
2 101 100 101
3 122 123 123
WBS –Templete Module Step 1: Identifying the problem
Input Input integer type variable are A,B Output The output is a integer either A or B
Specification Map f(A,B)= A, if A>B
f(A,B)=B, otherwise Constraints A is not equal to B
Step 2: Constructing a solution
Input initialization A=0,B=0 Output initialization None Other variables initialization None State(Input, Output and other variables) The state is(A,B,AorB)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A=user input, B=user input
www.esnips.com/user/vapremaims Page 23
Work Schedule
Worker Test Action Transfer control
W0
A>B Display “The input A is Bigger”
“STOP”
A<B Display “The input B is Bigger”
“STOP”
Number of workers = 1
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(4,19)=19 F(101,100)=101 F(122,123)=123 For the test cases given the test terminates
Test the Correctness F(4,19)=19 F(101,100)=101 F(122,123)=123 For the test case the given return is correct
Prove the Termination The program does not have any loops and it consist selection statement . So the program terminates
Prove the Correctness The program is correct because it takes two integers inputs A,B and checks the condition A>B and display A or B
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 24
Task 3 Output Module 2 Conditions
www.esnips.com/user/vapremaims Page 25
Task 4
Write a program that takes two integers A and B and print
a. "Large", if sum of A and B is greater than 20. b. "Medium", if sum of A and B is in the range 10 and 20(inclusive). c. "Small", otherwise.
Test Cases Input A Input B Output
1 11 12 Large
2 7 6 Medium
3 7 2 Small
WBS –Templete Module
Step 1: Identifying the problem
Input Input integers are A,B Output Output is a string “large” Or “Medium” or “Small Specification Map f(A,B)=”large”, if A+B>20
f(A,B)=”Medium”,if A+B is between 10 and 20 f(A,B)=”Small”, Otherwise
Constraints None
Step 2: Constructing a solution
Input initialization A=0,B=0 Output initialization None Other variables initialization Sum State(Input, Output and other variables) The state is(A,B)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
www.esnips.com/user/vapremaims Page 26
The manager and all the workers act on the state tuple.
Manager’s Initialization
A=User input,B=User Input, sum=0
Work Schedule
Worker Test Action Transfer control
W0 Sum=A+B W1
W1 Sum>20 Display”large” “STOP”
Sum<=20 && sum>=10
Display”Medium” “STOP”
Sum<10 Display”small” “STOP”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(11,12)=large F(7,6)=Medium F(7,2)=small For the test cases given the test terminates
Test the Correctness F(11,12)=large F(7,6)=Medium F(7,2)=small For the test cases given the return is correct
Prove the Termination The program does not have any loops. It consist selection statement so it terminates
Prove the Correctness The program is correct, it takes two input values and
calculate the sum of the two given values and finally check sum is greater than 20 or sum is range in 10 and 20 or sum is less than 10
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 27
Task 4 Output Module 2 Conditions
www.esnips.com/user/vapremaims Page 28
Task 5
Write a program that takes two integers A and B and checks whether both are “double digit” numbers. If both are not double digit numbers then return “False” and end the program. If both are double digit numbers then return
a. “Odd Sum”, if sum of A and B is odd. b. “Even Sum”, otherwise.
Test Cases Input A Input B Output
1 11 100 False
2 12 10 Even Sum
3 30 21 Odd Sum
WBS –Templete Module Step 1: Identifying the problem
Input Input integers are A and B Output Output integer sum, string false Specification Map F(A,B)=false ,if A,B are not double digit nos
F(A,B)=”Even sum”, if sum/2==0 F(A,B)=”Odd sum", otherwise
Constraints None
Step 2: Constructing a solution
Input initialization A=0 and B=0 Output initialization String false or odd sum or even sum Other variables initialization Sum=0 State(Input, Output and other variables) The state is(A,B,sum)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
www.esnips.com/user/vapremaims Page 29
Manager’s Initialization
A=user Input,B=User Input,sum=0
Work Schedule
Worker Test Action Transfer control
W0 (A>9 && A<100)&&(B>9&&B<100)
Sum=A+B W1
(A>9||A<10)||(B>9|| B<100)
Display”False” “STOP”
W1 sum/2=0 Display”Even sum” “Stop”
Sum/2!=0 Display”Odd sum” “stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(11,100)=false F(12,10)=Even sum F((30,21)=odd sum For the test cases given the test terminates
Test the Correctness F(11,100)=false F(12,10)=Even sum F((30,21)=odd sum For the test cases the return is correct
Prove the Termination The program does not have any loops, it consists selection statements. So the program terminates
Prove the Correctness The program is correct because it takes two integers and the output is correct
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 30
Task 5 Output Module 2 Conditions
www.esnips.com/user/vapremaims Page 31
Module 3 Loops Tasks
Task 1
1) Write a program that asks the user to input a non-negative number N and prints the first N natural numbers.
Test Cases Input Output 1 2 [ 1 2] 2 4 [1 2 3 4]
3 7 [ 1 2 3 4 5 6 7]
WBS
Step 1: Identifying the problem
Input N Output N natural numbers Specification Map F(n)=1 2 3…..n Constraints None
Step 2: Constructing a solution
Input initialization n=0 Output initialization None Other variables initialization i=0 State(Input, Output and other variables) State is (n,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
n=user input, i=0
www.esnips.com/user/vapremaims Page 32
Work Schedule
Worker Test Action Transfer control
W0 i=1 W1
W1 i>n Skip “STOP”
i<n Display i W2
W2 i=i+1 W0
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(2) = [1 2] f(4) = [1 2 3 4] f(7) = [1 2 3 4 5 6 7] For the test cases given the test terminates.
Test the Correctness f(2) = [1 2] f(4) = [1 2 3 4] f(7) = [1 2 3 4 5 6 7] For the test cases given the result is correct.
Prove the Termination The program consists of a loop. The variable i increments from i = 1 to i = n and the program terminates when the value of i becomes larger than n and the condition becomes false.
Prove the Correctness The program is correct because the i increments for n times and the value is displayed. The loop terminates after n iterations.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 33
Task 1 Output Module 3 Loops
www.esnips.com/user/vapremaims Page 34
Task 2
Write a program to print the first N even numbers.
Test Cases Input Output 1 4 [ 2 4 6 8]
2 3 [ 2 4 6] 3 1 [2]
WBS – Table- Template
Step 1: Identifying the problem
Input N Output Even numbers Specification Map F(n)= 2,4,6,……n Constraints None
Step 2: Constructing a solution
Input initialization N=0 Output initialization None Other variables initialization i=0,t=0 State(Input, Output and other variables) State(n,i,t)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
n=user input,i=0,t=0
www.esnips.com/user/vapremaims Page 35
Work Schedule
Worker Test Action Transfer control
W0 t = 1 W1
W1 i = 1 W2
W2 t > N “Stop”
t <= N Skip W3
W3 i%2==0 Display i W4
i%2!=0 Skip W5
W4 t=t+1 W5
W5 i=i + 1 W2
Number of workers = 4
Program (Implementation in RAPTOR or Java or any other programming language)
Task 2 Output
www.esnips.com/user/vapremaims Page 36
Task 3
Write a program to check whether a given positive integer A is a prime number or not.
Test Cases Input Output 1 11 Prime
2 82 Not a Prime 3 17 Prime
WBS – Table- Template
Step 1: Identifying the problem
Input A Output String “prime” or “not prime” Specification Map F(A)=”Prime”, If the given no is prime
F(A)=”Not Prime”, otherwise Constraints None
Step 2: Constructing a solution
Input initialization A=0 Output initialization None Other variables initialization i=0,c=0 State(Input, Output and other variables) The state is(A,i,n)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A=user input,i=0,c=0
www.esnips.com/user/vapremaims Page 37
Work Schedule
Worker Test Action Transfer control
W0 i=1 W1
W1 i>A Skip W4
i<=n Skip W2
W2 A%i==0 c=c+1 W3
A%i!=0 Skip W3
W3 i=i+1 W0
W4 C>2 Display “not prime” Stop
C<=2 Display “Prime” Stop
Number of workers = 4
Program (Implementation in RAPTOR or Java or any other programming language)
www.esnips.com/user/vapremaims Page 38
Task Output 3 Module 3 Loops
www.esnips.com/user/vapremaims Page 39
Task 4
Write a program to print the pattern below by using 2 loops and without using any arrays.
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
WBS – Table- Template Step 1: Identifying the problem
Input Input Integers N Output Given Pattern is printed Specification Map F(N)= PATTERN Constraints None
Step 2: Constructing a solution
Input initialization N = 0 Output initialization None Other variables initialization I =0, J=0,P=1 State(Input, Output and other variables) State is (N,I,J,P)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
N = USER INPUT, I = 0, J=0,P=1
Work Schedule
Worker Test Action Transfer control
W0 I = 0 W1
www.esnips.com/user/vapremaims Page 40
W1 J = 0 W2
W2 P=1 W3
W3 I>=N “Stop”
I<N J=0 W4
W4 J>=N Display “New Line” W5
J<N Display “P” W6
W5 I=I+1 W3
W6 J=J+1 W4
Number of workers = 7
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(4) = PATTERN For the test cases given the test terminates.
Test the Correctness f(4) = PATTERN For the test cases given the result is correct.
Prove the Termination The program consists of two loops. The variable i increments from I = 1 to I = n and the second variable j also increments from j=0 to j=n. The program terminates when the value of I becomes larger than n and the result is printed.
Prove the Correctness The program is correct because the I increments for n times and j increments for n times and every time the p value is printed such that the pattern is printed. The loop terminates after n iterations and displays the result.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 41
Module 4 Arrays Tasks
Task 1
Write a program to add two arrays A and B of size 5.
Test Cases Input A Input B Output
1 [1 1 1 1 1] [2 2 2 2 2] [3 3 3 3 3]
2 [0 0 0 0 0] [1 2 3 4 5] [1 2 3 4 5]
3 [2 5 1 7 3] [3 2 1 8 0] [5 7 2 15 3]
WBS –Templete Module
Step 1: Identifying the problem
Input The input is an ordered pair of two arrays A and B. A = [A[1],A[2],A[3],A[4],A[5]] B = [B[1],B[2],B[3],B[4],B[5]]
Output The output is an array C= [C[1],C[2],C[3],C[4],C[5]]
Specification Map f (A,B) = A + B
Constraints None
Step 2: Constructing a solution
Input initialization A=0, B=0 Output initialization C=0 Other variables initialization i=1 State(Input, Output and other variables) State(A,B,C,i)
Management Model
www.esnips.com/user/vapremaims Page 42
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A = user input and B = user input, C = 0, i = 1
Work Schedule
Worker Test Action Transfer control
W1 C[i]=A[i]+B[i] W2
W2 i<=5 I=i+1 W1
i>5 Display C “stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f((1,1,1,1,1)(2,2,2,2,2))=(3,3,3,3,3) f((0,0,0,0,0)(1,2,3,4,5))=(1,2,3,4,5) f((2,5,1,7,3)(3,2,1,8,0))=(5,7,1,15,3) The program terminates for all the inputs.
Test the Correctness f((1,1,1,1,1)(2,2,2,2,2))=(3,3,3,3,3) f((0,0,0,0,0)(1,2,3,4,5))=(1,2,3,4,5) f((2,5,1,7,3)(3,2,1,8,0))=(5,7,1,15,3) The program output is correct for all inputs.
Prove the Termination The variable i starts at 1 and increments upto 5. When it reaches to 5, the loop is terminated.
Prove the Correctness The Program computes A+B by Computing A[i]+B[i] Step by step from i=1 to i=5
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 43
Task 2
Write a program to swap the first two elements and also the last two elements in an array A of size 6.
Test Cases Input Output
1 [1 2 3 4 5 6] [2 1 3 4 6 5]
2 [3 2 4 5 6 1] [2 3 4 5 1 6]
3 [11 5 7 3 4 1] [5 11 7 3 1 4]
WBS-Templete Module Step 1: Identifying the problem
Input The input (A) is an array A. A = [A[1],A[2],A[3],A[4],A[5],A[6]]
Output The output is an array A= [A[2],A[1],A[3],A[4],A[6],A[5]]
Specification Map f (A,B) = swap A[1] and A[2] also A[5] and A[6]
Constraints Only 1st , 2nd and 5th, 6th elements should be swapped
Step 2: Constructing a solution
Input initialization A=0 Output initialization None Other variables initialization i=1 State(Input, Output and other variables) State(A,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
www.esnips.com/user/vapremaims Page 44
Manager’s Initialization
A = user input, i = 1
Work Schedule
Worker Test Action Transfer control
W1 i==1||i==5 W2
W2 Swap A[i],A[i+1] W3
W3 i<=5 I=i+1 W1
i>5 Display C “stop”
Number of workers = 3
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(1,2,3,4,5)=(2,1,3,4,6,5) f(3,2,4,5,6,1)=(2,3,4,5,1,6) f(11,5,7,3,4,1)=(5,11,7,3,1,4) The program terminates for all the inputs.
Test the Correctness f(1,2,3,4,5)=(2,1,3,4,6,5) f(3,2,4,5,6,1)=(2,3,4,5,1,6) f(11,5,7,3,4,1)=(5,11,7,3,1,4) The program output is correct for all inputs.
Prove the Termination The variable i starts at 1 and increments upto 6. When it reaches to 6, the loop is terminated.
Prove the Correctness The Program swaps A[1],A[2] and A[5],A[6] Step by step from i=1 to i=5 only when I is at 1 and 5.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 45
Task 3
Write a program to print in the reverse order any given integer array A of any size.
Test Cases Input Output
1 [1 2 3 4 5 6] [6 5 4 3 2 1]
2 [3 1 2] [2 1 3]
3 [10 1 9 7] [7 9 1 10]
WBS-Templete Module Step 1: Identifying the problem
Input The input (A) is an ordered pair of two arrays A and B. A = [A[1],A[2],A[3],A[4],A[5]……..A[n], n
Output The output is an array A= [A[n],A[n-a],…….A[5],A[4],A[3],A[2],A[1]]
Specification Map f(A)= [A[n],A[n-1],A[5],A[4],A[3],A[2],A[1]] Constraints No Constraints are specified Step 2: Constructing a solution
Input initialization A=0,n=0 Output initialization None Other variables initialization i=1 State(Input, Output and other variables) (n,A,i)
Manager’s Initialization
A=User input,n=User input
Work Schedule
Worker Test Action Transfer control
W0 None i=n W1
W1 i<1 Display A[i] W1
www.esnips.com/user/vapremaims Page 46
i>1 None “Stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(1,2,3,4,5,6)=(6,5,4,3,2,1) f (3,1,2)=(2,1,3) f(10,1,9,7)=(7,9,1,10) The program terminates for all the inputs.
Test the Correctness f(1,2,3,4,5,6)=(6,5,4,3,2,1) f (3,1,2)=(2,1,3) f(10,1,9,7)=(7,9,1,10) For the test cases given the return is correct
Prove the Termination The variable i starts with 1 and increments at every step. When it is greater than 5 it terminates.
Prove the Correctness The program displays the given elements in an array in a reverse order.
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 47
Task 4
Write a program to print a 4 4 matrix using a two dimensional array.
Test Cases Input Output
1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
WBS –Templete Module Step 1: Identifying the problem
Input The input (A) is a two dimentional array A. A = [A[1][1],A[1][2],A[1] [1] [3],A[4], A[2][1], A[2][2], A[2][3], A[2][4], A[3][1], A[3][2], A[3][3], A[3][4], A[4][1], A[4][2], A[4][3], A[4][4]]
Output The output is an array A = [A[1][1],A[1][2],A[1] [1] [3],A[4], A[2][1], A[2][2], A[2][3], A[2][4], A[3][1], A[3][2], A[3][3], A[3][4], A[4][1], A[4][2], A[4][3], A[4][4]]
Specification Map f (A) = A
Constraints None
www.esnips.com/user/vapremaims Page 48
Step 2: Constructing a solution
Input initialization A=0 Output initialization None Other variables initialization i=1,j=1 State(Input, Output and other variables) State(A,I,j)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A = user input, i = 1,j=1
Work Schedule
Worker Test Action Transfer control
W0 Display A W1
W1 i<4&&j<4 “stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(1 1 1 1 f(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 1 1 1 1 1 1 1 1) 1 1 1 1) The program terminates for all the inputs.
Test the Correctness f(1 1 1 1 f(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 = 1 1 1 1 1 1 1 1) 1 1 1 1)
www.esnips.com/user/vapremaims Page 49
The program output is correct for all inputs. Prove the Termination The variables i and j starts at 1 and increments upto
4. When it reaches to 4, the loop is terminated. Prove the Correctness Program reads elements step by step from I and j
equal to 1 to 4 Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 50
Task 5
Write a program to add two 4 4 matrices A and B and store the result in matrix C.
Test Cases Input Output
1
Matrix A 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Matrix B 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Matrix C 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
2
Matrix A 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Matrix B 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Matrix C 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3
Matrix A 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Matrix B 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Matrix C 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
www.esnips.com/user/vapremaims Page 51
WBS –Templete Module Step 1: Identifying the problem
Input The input (A,B) is a pair of ordered multi dimensional arrays A, B. A = [A[1][1],A[1][2],….,A[4][4]] B = [B[1][1],B[1][2],….,B[4][4]
Output The output is an array C= [C[1][1],C[1][2],…..,C[4][4]] Specification Map C[i][j]=A[i][j]+B[i][j] Constraints None
Step 2: Constructing a solution
Input initialization A=0, B=0 Output initialization C=0 Other variables initialization i=1,j=1 State(Input, Output and other variables) State(A,B,C,I,j)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A = user input and B = user input, C = 0, i = 1,j=1
Work Schedule
Worker Test Action Transfer control
W0 i<=4 j=1 W1
i>4 Skip “Stop”
W1 j<=4 C[i][j]=A[i][j]+B[i][j] W1
j>4 Skip W0
www.esnips.com/user/vapremaims Page 52
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f((1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1) (2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3) f((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) f((4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4),(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6) The program terminates for all the inputs.
Test the Correctness f((1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1),(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3) f((0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0),(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) f((4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4),(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2))= (6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6) For the test cases given the return is correct
Prove the Termination The variable i and start at 1 and increments upto 4. When both I and j reaches to 4, the loop is terminated.
Prove the Correctness The Program computes C by Computing A[i][j]+B[i][j] Step by step from i=1 to i=4 and j=1 to j=4.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 53
Module 5 Strings Tasks
Task 1
Write a program that takes three string inputs S1 (your name), S2 (college name), and S3 (city) and prints the strings separated by the delimiter comma.
Test Cases Input S1 Input S2 Input S3 Output
1 Shyam A R College Mumbai Shyam,A R College,Mumbai
2 Venkat M V Degree College Hyderabad Venkat, M V Degree College, Hyderabad
3 Subhash Roy Engineering College
Kolkata Subhash, Roy Engineering College,Kolkata
WBS Step 1: Identifying the problem
Input S1, S2 and S3 are three Strings Output Printed in the format “S1,S2,S3” Specification Map f(S1,S2,S3)=S1,S2,S3 Constraints None
Step 2: Constructing a solution
Input initialization S1=” “, S2=” “s3=”” Output initialization None Other variables initialization None State(Input, Output and other variables) State(S1,S2,S3)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
www.esnips.com/user/vapremaims Page 54
Manager’s Initialization
S1=User input S2=User input S3=User input
Work Schedule
Worker
Test Action Transfer control
W1 Display s1+”,”+s2+”,”+s3
“Stop”
Number of workers = 1
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(Shyam,A R College,Mumbai)= Shyam,A R College,Mumbai f(Venkat, M V Degree College, Hyderabad)=Venkat, M V Degree College, Hyderabad f(Subhash, Roy Engineering College,Kolkata)= Subhash, Roy Engineering College,Kolkata The program terminates for all the inputs.
Test the Correctness f(Shyam,A R College,Mumbai)= Shyam,A R College,Mumbai f(Venkat, M V Degree College, Hyderabad)=Venkat, M V Degree College, Hyderabad f(Subhash, Roy Engineering College,Kolkata)= Subhash, Roy Engineering College,Kolkata The program is correct for all the inputs.
Prove the Termination The Program does not have any loops and program terminates when reading 3 strings.
Prove the Correctness The Program reads three strings and prints them by separating with commas.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 55
Task 1 Output Module 5 Strings
www.esnips.com/user/vapremaims Page 56
Task 2
Write a program that takes two strings S1 and S2 as inputs and concatenates them.
Test Cases Input S1 Input S2 Output
1 Venkat Swamy VenkatSwamy
2 Hari Kishore HariKishore
3 Ramesh Rao RameshRao
WBS –Templete Module
Step 1: Identifying the problem
Input S1, S2 are two Strings Output Display output string Specification Map f(S1,S2)=S1S2 Constraints None
Step 2: Constructing a solution
Input initialization S1=” “, S2=” “ Output initialization None Other variables initialization None State(Input, Output and other variables) State(S1,S2)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S1=User input S2=User input
www.esnips.com/user/vapremaims Page 57
Work Schedule
Worker Test Action Transfer control
W0 Display s1+s2 “stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(Venkat,Swamy)= VenkatSwamy f(Hari,Kishore)= HariKishore f(Ramesh,Rao)=RameshRao The program terminates for all the inputs.
Test the Correctness f(Venkat,Swamy)= VenkatSwamy f(Hari,Kishore)= HariKishore f(Ramesh,Rao)=RameshRao The program is correct for all the inputs.
Prove the Termination The Program does not have any loops and program terminates after reading 2 strings.
Prove the Correctness The Program reads two strings and concatenates and prints them.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 58
Task 2 Module 5 Strings
www.esnips.com/user/vapremaims Page 59
Task 3
Write a program to reverse a string S1 and stores the result in S2.
Test Cases Input Output
1 Varsham mahsrav
2 Katak katak
3 Program margorP
WBS – Table- Template Step 1: Identifying the problem
Input S String Output rev=Reverse of S Specification Map f(S)=Reverse of S Constraints None
Step 2: Constructing a solution
Input initialization l=length_of(S),s=”” Output initialization rev” “ Other variables initialization None State(Input, Output and other variables) State(S,l)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S=User input rev=” “ l=length_of(s)
www.esnips.com/user/vapremaims Page 60
Work Schedule
Worker Test Action Transfer control
W0 l!=0 rev=rev+s[l] W1
l==0 Display New line STOP
W1 l=l-1 W0
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(varsham)= mahsrav f(katak)= katak f(Program)=margorP The program terminates for all the inputs.
Test the Correctness f(varsham)= mahsrav f(katak)= katak f(Program)=margorP The program is correct for all the inputs.
Prove the Termination The Program has loop and it is terminated properly when l==0.
Prove the Correctness The Program reads a string, reverses and prints it. Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 61
Task 3 Module 5 Strings
www.esnips.com/user/vapremaims Page 62
Task 4
Write a program that takes a string S1 of letters as input and converts the letters into upper case.
Test Cases Input S1 Output
1 Abcdef ABCDEF
2 Hello how are you! HELLO HOW ARE YOU!
3 Lower LOWER
WBS –Templete Module Step 1: Identifying the problem
Input S1 String Output S2=Upper case of S1 Specification Map f(S2)=Upper case of S1 Constraints None
Step 2: Constructing a solution
Input initialization S1=” “, x=length_of(S1), i=1 Output initialization None Other variables initialization None State(Input, Output and other variables) State(S1,x,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S1=User input S2=” “ x=length_of(S1), i=1
www.esnips.com/user/vapremaims Page 63
Work Schedule
Worker Test Action Transfer control
W1 X=length_of(S1) W2
W2 i=1 W3
W3 i>x Print S1 “Stop”
i<=x W4
W4 S1[i]>=97&&S1[i]<=122 S1[i]=S1[i]-32 W5
S1[i]<97 && S1[i]>123 S1[i]=S1[i] W5
W5 i=i+1 W3
Number of workers = 5
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(abcdef)= ABCDEF f(hello how are you!)= HELLO HOW ARE YOU! f(lower)=LOWER The program terminates for all the inputs.
Test the Correctness f(abcdef)= ABCDEF f(hello how are you!)= HELLO HOW ARE YOU! f(lower)=LOWER The program is correct for all the inputs.
Prove the Termination The Program has loop and it is terminated properly when i reaches to x.
Prove the Correctness The Program reads a string, converts into Upper case letters.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 64
Task 4 Output Module 5 Strings
www.esnips.com/user/vapremaims Page 65
Task 5
Write a program that takes two string inputs S1, S2 and checks whether S2 is a substring of S1 or not.
WBS –Templete Module Step 1: Identifying the problem
Input Strings S1, S2 Output F(S1,S2)= YES if S2 is in S1
NO if S2 is not in S1 Specification Map f(S1,S2)= S2 in S1 Constraints None
Step 2: Constructing a solution
Input initialization S1=” “, S2=” “,x=length_of(S1), y=length_of(S2),i=1, j=1, c=0,count=0
Output initialization None Other variables initialization None State(Input, Output and other variables) State(S1,S2,x,y,I,j,c,count)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S1=User input S2=” “, x=length_of(S1), y=length_of(S2), i=1,j=1,c=0,count=0
Test Cases Input S1 Input S2 Output
1 ChanakyaPuri Puri Yes
2 Mahabharat bharata No
3 Radhakrishnan rish Yes
www.esnips.com/user/vapremaims Page 66
Work Schedule
Worker Test Action Transfer control
W1 X=length_of(S1) W2
W2 Y=length_of(S2) W3
W3 Count==y||i>x W4
Count!=y||i<x W5
W4 c==y Display “YES” “Stop”
c!=y Display “NO” “Stop”
W5 S1[i]==S2[j] W6
S1[i]!=S2[j] W7
W6 J=j+1,c=c+1,count=count+1 W8
W7 J=0,c=0,count=0 W8
W8 I=i+1 W3
Number of workers = 5
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(ChanakyaPuri,Puri)= YES f(Mahabharat, bharata)= NO f(Radhakrishnan,rish)=YES The program terminates for all the inputs.
Test the Correctness f(ChanakyaPuri,Puri)= YES f(Mahabharat, bharata)= NO f(Radhakrishnan,rish)=YES The program is correct for all the inputs.
Prove the Termination The Program has loop and it is terminated properly when i reaches to x.
Prove the Correctness The Program reads two strings, finds for the sub string in the main string and displays the result.
Mathematics vs Machine
www.esnips.com/user/vapremaims Page 67
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
Task 5 Output Module 5 Strings
www.esnips.com/user/vapremaims Page 68
Module 6 Functions Tasks
Task 1
Write a program that takes an integer A as input and calls the function
i. Large (), if A > 100. This function prints the string “Large”. ii. Medium (), if 50 <= A <= 100. This function prints the string
“Medium”. iii. Small (), otherwise and it prints string “Low”.
WBS-Templete Module Step 1: Identifying the problem
Input Integer A Output Large/Medium/Small Specification Map F(A)= Large if A>100
Medium if A>50 && A<100 Small if A<50
Constraints None
Step 2: Constructing a solution
Input initialization A=0 Output initialization None Other variables initialization None State(Input, Output and other variables) State(A,output)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A=0
www.esnips.com/user/vapremaims Page 69
Work Schedule
Worker Test Action Transfer control
W1 A>100 call large() W2
A>50 Call medium() W3
A<50 Call small() W4
W2 Print “Large” “Stop”
W3 Print “Medium” “Stop”
W4 Print “Small” “Stop”
Number of workers = 4
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(120)=Large F(90)=Medium F(30)=Small Program terminates for all the test inputs.
Test the Correctness F(120)=Large F(90)=Medium F(30)=Small Program gives correct output for the test input.
Prove the Termination Program does not have loops, it has function calls. The program terminates when all the sub functions and main function are completed.
Prove the Correctness In the program value is read and based on value control goes from main to large,medium,small functions and displays the result.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 70
Task 1Output Module 6 Functions
www.esnips.com/user/vapremaims Page 71
Task 2
Write a program that sends two integers A and B as parameters to different functions below
a. Sum () prints the sum of A and B. b. Product () prints product of A and B. c. Diff() prints the difference A - B.
WBS –Templete Module
Step 1: Identifying the problem
Input (A,B) Output C=A+B,
C=A*B, C=A-B
Specification Map F(A,B)= A+B, A*B, A-B Constraints None
Step 2: Constructing a solution
Input initialization A=0, B=0 Output initialization C=0 Other variables initialization None State(Input, Output and other variables) State(A,output)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
A=0,B=0,C=0
www.esnips.com/user/vapremaims Page 72
Work Schedule
Worker Test Action Transfer control
W1 Call Sum(A,B) W2
W2 C=A+B W3
W3 Display C W4
W4 Call Product(A,B) W5
W5 C=A*B W6
W6 Display C W7
W7 Call Ddifference(A,B) W8
W9 C=A-B W10
W10 Display C “STOP”
Number of workers = 5
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
www.esnips.com/user/vapremaims Page 73
Test the Termination F(20,10)= Sum=30 Product=200 Difference=10 Program terminates for all the test inputs.
Test the Correctness F(20,10)= Sum=30 Product=200 Difference=10 Program gives correct output for the test input.
Prove the Termination Program does not have loops, it has function calls. The program terminates when all the sub functions and main function are completed.
Prove the Correctness In the program values are read and control goes to different functions to perform different operations and prints results.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 74
Task 3
Write a program that sends two strings S1 and S2 as parameters to the following functions
a. Concatenate () prints concatenation of two strings. b. Equality () checks the equality of the two strings and prints “True”
if equal and “False” if unequal.
WBS-Templete Module
Step 1: Identifying the problem
Input (S1,S2) Output (S1,S2)=S1+S2
(S1,S2)= TRUE if S1==S2 FALSE if S1!=S2
Specification Map F(S1,S2)= S1+S2, S1==S2 Constraints None
Step 2: Constructing a solution
Input initialization S1=0, S2=0 Output initialization S3=0 Other variables initialization None State(Input, Output and other variables) State(S1,S2,S3,output)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S1=0,S2=0,S3=0
www.esnips.com/user/vapremaims Page 75
Work Schedule
Worker Test Action Transfer control
W1 Call Concatenate(S1,S2)
W2
W2 S3=S1+S2 W3
W3 Call Equality(S1,S2) W4
W4 If(S1==S2) Display “TRUE” stop
If(S1!=S2) Display “FALSE” stop
Number of workers = 6
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(vignan,vignan)= Concatenation=vignanvignan Equality= TRUE F(Santhosh,Kumar)= Concatenation= SanthoshKumar Equality= FALSE Program terminates for all the test inputs.
Test the Correctness F(vignan,vignan)= Concatenation=vignanvignan Equality= TRUE F(Santhosh,Kumar)= Concatenation= SanthoshKumar Equality= FALSE Program gives correct output for the test input.
Prove the Termination Program does not have loops, it has function calls.
www.esnips.com/user/vapremaims Page 76
The program terminates when all the sub functions and main function are completed.
Prove the Correctness In the program two strings are read and control goes to different functions to display the concatenation and equality results.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
Task 3 Output Module 6 Functions
www.esnips.com/user/vapremaims Page 77
Module 7 Recursions Tasks
Task 1
Given a positive integer N as user input, write a program to print the sum of the first N natural numbers using recursion.
Test Cases Input N Output
1 3 6
2 5 15
3 10 55
WBS-Templete Module Step 1: Identifying the problem
Input N Output Sum Specification Map F(n)=1+2+…….+n Constraints None
Step 2: Constructing a solution
Input initialization N=0 Output initialization sum Other variables initialization i=0 State(Input, Output and other variables) State(n,sum,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
www.esnips.com/user/vapremaims Page 78
Manager’s Initialization
N=user input, sum=0,i=0
Work Schedule
Worker Test Action Transfer control
W0 sum=sum+i W1
W1 Display Sum “Stop”
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(3)=6 F(5)=15 F(10)=55 The Program Terminatess for all the test inputs
Test the Correctness F(3)=6 F(5)=15 F(10)=55 The program gives correct output for all the test inputs.
www.esnips.com/user/vapremaims Page 79
Prove the Termination The control passes from main function to sub function and repeats till the condition is false. When the condition is false program terminates.
Prove the Correctness The program computes Sum from i = 1 to i = N. The Finally the result is displayed.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
Task 1 Output Module 7 Recursions
www.esnips.com/user/vapremaims Page 80
Task 2
Given a positive integer N as user input, write a program to print the sum of the cubes of the first N natural numbers using recursion.
Test Cases Input N Output
1 3 36
2 1 1
3 5 225
WBS-Templete Module Step 1: Identifying the problem
Input N Output Sum Specification Map F(n)=13+23+…….+n3
Constraints None
Step 2: Constructing a solution
Input initialization N=0 Output initialization sum=0 Other variables initialization i=0 State(Input, Output and other variables) (N,sum,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
N=user input, sum=0,i=0
www.esnips.com/user/vapremaims Page 81
Work Schedule
Worker Test Action Transfer control
W0 sum=sum+(i*i*i) W1
W1 Display sum Stop
Number of workers = 2
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination F(3)=36 F(1)=1 F(5)=225 The Program Terminatess for all the test inputs
Test the Correctness F(3)=36 F(1)=1 F(5)=225 The program gives correct output for all the test inputs.
Prove the Termination The control passes from main function to sub function and repeats till the condition is false. When the condition is false program terminates.
Prove the Correctness The program computes Sum of cubes from i = 1 to i = N. The Finally the result is displayed.
Mathematics vs Machine
www.esnips.com/user/vapremaims Page 82
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 83
Task 3
Write a program to find whether the given number P is a prime number or not using recursion.
Test Cases Input N Output
1 3 "Prime Number"
2 5 "Prime Number"
3 8 "Not A Prime Number"
WBS –Templete Module Step 1: Identifying the problem
Input Input Integers N Output “PRIME “ if N%2==0
“Not PRIME” if N%2!=0 Specification Map F(input) = output, F(N)= PRIME/NOT PRIME Constraints None
Step 2: Constructing a solution
Input initialization N = 0 Output initialization None Other variables initialization I = 1, count=0 State(Input, Output and other variables) State is (N,I,count)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
N = USER INPUT, I = 1, count=0
www.esnips.com/user/vapremaims Page 84
Work Schedule
Worker Test Action Transfer control
W0 I = 1 W1
W1 Count=0 W2
W2 I > N W6
I<= N Skip W3
W3 N%I==0 W4
I%2!=0 W5
W4 count=count+1 W5
W5 I = I + 1 W2
W6 count==2 Display “PRIME” “Stop”
count!=2 Display “NOT PRIME” “Stop”
Number of workers = 7
Program (Implementation in RAPTOR or Java or any other programming language)
www.esnips.com/user/vapremaims Page 85
Step 3: Verifying the Solution
Test the Termination f(3) = PRIME NUMBER. f(5) = PRIME NUMBER. f(8) = NOT PRIME NUMBER. For the test cases given the test terminates.
Test the Correctness f(3) = PRIME NUMBER. f(5) = PRIME NUMBER. f(8) = NOT PRIME NUMBER. For the test cases given the result is correct.
Prove the Termination The program consists of a loop when subfunction is called. The variable i increments from I = 1 to I = n and the program terminates when the value of I becomes larger than n and the result is printed by using recursion.
Prove the Correctness The program is correct because the I increments for n times and the factors are counted. The loop terminates after n iterations and displays the result by using recursion.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)
www.esnips.com/user/vapremaims Page 86
Task 4
Write a program that takes a string "Str1" as input and prints the reverse of it using recursion.
Test Cases Input Str1 Output
1 Arjuna anujrA
2 Ashoka akohsA
3 Reverse esreveR
WBS Step 1: Identifying the problem
Input S1 String Output S2=Reverse of S1 Specification Map f(S2)=Reverse of S1 Constraints None
Step 2: Constructing a solution
Input initialization S1=” “, x=length_of(S1), i=1 Output initialization S2=” “ Other variables initialization None State(Input, Output and other variables) State(S1,x,i)
Management Model
A manager M is assigned the given problem to solve. The manager M divides the given work among the workers W0, W1….. Wn . The role of the manager is to choose the program variable initialization and design the work schedule.
The manager and all the workers act on the state tuple.
Manager’s Initialization
S1=User input S2=” “ x=length_of(S1), i=1
www.esnips.com/user/vapremaims Page 87
Work Schedule
Worker Test Action Transfer control
W1 X=length_of(S1) W2
W2 i=1 W3
W3 S2=” “ W4
W4 x<1 Print S2 “Stop”
x>=1 S2[i]=S1[x] W5
W5 x=x-1 W6
W6 i=i+1 W4
Number of workers = 6
Program (Implementation in RAPTOR or Java or any other programming language)
Step 3: Verifying the Solution
Test the Termination f(Arjuna)= anujrA f(Ashoka)= akohsA f(Reverse)=esreveR The program terminates for all the inputs.
Test the Correctness f(Arjuna)= anujrA f(Ashoka)= akohsA
www.esnips.com/user/vapremaims Page 88
f(Reverse)=esreveR The program is correct for all the inputs.
Prove the Termination The Program has loop and it is terminated properly when x reaches to 1by using recursion.
Prove the Correctness The Program reads a string, reverses and prints it by using recursion.
Mathematics vs Machine
Step 4: Documentation
It is commenting in between the programming (or RAPTOR)