Chapter No 1 Program Logic Development Algorithms:: Algorithm
Chapter No 1 Program Logic Development Algorithms:: Algorithm
What is an Algorithm ?
An algorithm is a sequence of instructions that can be performed by a machine.
An algorithm also has clearly defined inputs, and clearly defined outputs.
The Algorithm designed are language-independent, i.e. they are just plain
instructions that can be implemented in any language, and yet the output will be
the same, as expected
What are the Characteristics of an Algorithm?
What is Pseudocode ?
Pseudocode is a notation system for writing algorithms. The pseudocode
notation specifies operations that a machine can perform in as human-
friendly (e.g., easy to read) way as possible, while avoiding ambiguity.
1.Write an algorithm to exchange values of two numbers without using
third variable
Input a=10
b=20
output
a=20
b=10
Logic
a=a+b a=30,b=20
b=a-b, b=30-20 ,b=10,a=30
a=a-b a=30-10,a=20
1.start
2. Declare two numbers num1,num2
3.read num1,num2
4.num1=num1+num2
5.num2=num1-num2
6.num1=num1-num2
7.write num1,num2
8.stop
57/2=28 57%2=1
89/3=29 89%3=2
Prime number
11%(2-10)=0 prime
7%(2 or 3 or 4or 5or 6)
7%1=0 7%7=0
num=10
for(i=2 ,i<10, i++)
If 10%2=0
Not prime number
Step 1->START
Step 2 → Take integer variable A
Step 3 → Divide the variable A with (A-1 to 2)
Step 4 → If A is divisible by any value (A-1 to 2) it is not prime
Step 5 → Else it is prime
Step-> STOP
Even odd
2,4,,6,8,10
3,5,7,9,
Num =88
num%2=0 even 88%2=0
num%2=1 odd
What is an Algorithm ?
An algorithm is a sequence of instructions that can be performed by a machine. An algorithm
also has clearly defined inputs, and clearly defined outputs.
What is Pseudocode ?
Pseudocode is a notation system for writing algorithms. The pseudocode notation specifies
operations that a machine can perform in as human-friendly (e.g., easy to read) way as
possible, while avoiding ambiguity.
• A variable is a name for one or more contiguous memory locations. A variable name
must begin with a letter, but make contain digits or an underscore. Names are chosen
and capitalization may used to visually help convey the intention of the algorithm
writer, and to clarify the type of information stored.
Examples: i, n, Employee_Pay_Rate
In the examples above, you don’t get too many clues about what i or n might represent.
Any idea what the identifier Employee_Pay_Rate might represent ? < LOL >.
• An array variable is a name for many data items of the same kind. For example,
we might have an array of integers that represent Tiddlywink scores in a campus
tournament as illustrated below:
26 11 31 17 43 22 15 18
0 1 2 3 4 5 6 7
Suppose the name of the array is A. We specify a particular element of an array (say
the data in position three) with the notation: A[3]. In keeping with computer science
convention, we start numbering the positions in the array with zero. In the array
pictured above A[0] is the number 26 and A[3] is the number 17.
The operation specified by the square brackets [ and ] is called array indexing. The
object between the square brackets may also be another variable. For example, the
following two statements:
i = 5
b = A[i]
• Comments begin with // and go to the end of the line. For example:
1
// The quick red fox jumped over the lazy brown dog.
Comment statements are not executed by the computer. They are only reminders to
ourselves.
• The equals sign = is used to assign a value to a variable. For example: i = 1. Such a
statement is usually called an assignment statement. You should think of assignment
as the movement of data into the storage location named by the variable. Unlike
mathematical notation, the statement is not symmetric. I.e., we never write 1 = i.
Values stored in a variable may be replaced at any time. For example:
i = 1
i = 2
z = x * y + 1
• We may read a value from an input device. For example, the statement:
read x
will get a data value from the input device and store it in the variable x.
write x
will copy the value of x to the output device. The value stored in the memory location
named by x is not changed.
2
• Statement Blocks
We will group a set of statements using the curly brace characters “{” and “}”. For
example:
{
read celsius_temperature
fahrenheit = 5.0/9.0 * celsius_temperature + 32
}
• Conditional Execution
We can conditionally execute a statement (or a block of statements) using the if ...
else ... control construct. For example:
if ( x < 0 ) {
x = 0 - x
}
The example above has the effect of taking the absolute value of x. I.e., if x is initially
-5, subtracting from zero gives us positive 5.
The else clause is optional and will be executed whenever the logical condition is false.
t = 1
while ( t <= 5 ) {
write t
t = t + 1
}
The example above would output the numbers: 1 2 3 4 5 Notice we use indenting
to convey the intended meaning, i.e., that the write statement and the statement
incrementing t is under the control of the while construct. A repeated set of statements
(including the control construct) is called a loop.
3
Example Algorithm
Method:
In class we will trace this algorithm with n = 5 and with the following array:
22 37 11 17 30
0 1 2 3 4