Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
81 views

Chapter No 1 Program Logic Development Algorithms:: Algorithm

The document discusses algorithms and pseudocode. It defines an algorithm as a sequence of instructions that can be performed by a machine to solve a problem. Pseudocode is a notation for writing algorithms in a human-friendly way to avoid ambiguity. The document then outlines the basic notation and structures used in pseudocode, including variables, arrays, comments, assignment statements, input/output, and control constructs like if/else statements and loops.

Uploaded by

Shyam Kolge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Chapter No 1 Program Logic Development Algorithms:: Algorithm

The document discusses algorithms and pseudocode. It defines an algorithm as a sequence of instructions that can be performed by a machine to solve a problem. Pseudocode is a notation for writing algorithms in a human-friendly way to avoid ambiguity. The document then outlines the basic notation and structures used in pseudocode, including variables, arrays, comments, assignment statements, input/output, and control constructs like if/else statements and loops.

Uploaded by

Shyam Kolge
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Chapter No 1

Program Logic development


Algorithms:The word Algorithm means “a process or set of rules to be followed in
calculations or other problem-solving operations”.

Therefore Algorithm refers to a set of rules/instructions that step-by-step define


how a work is to be executed upon in order to get the expected results.

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?

• Clear and Unambiguous: Algorithm should be clear and unambiguous.


Each of its steps should be clear in all aspects and must lead to only one
meaning.
• Well-Defined Inputs: If an algorithm says to take inputs, it should be
well-defined inputs.
• Well-Defined Outputs: The algorithm must clearly define what output
will be yielded and it should be well-defined as well.
• Finite-ness: The algorithm must be finite, i.e. it should not end up in an
infinite loops or similar.
• Feasible: The algorithm must be simple, generic and practical, such that
it can be executed upon will the available resources. It must not contain
some future technology, or anything.
• Language Independent: The Algorithm designed must be language-
independent, i.e. it must be just plain instructions that can be
implemented in any language, and yet the output will be same, as
expected.

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

Write an algorithm to find number is positive or negative


1.start
2.declare number num
3.read num
4.if num>0
Num is positive
Else
Num is negative
5.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

Algorithm for 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

Algorithm for even odd number

Step 1-> START


Step 2 → Take integer variable A
Step 3 → Assign value to the variable
Step 4 → Perform A modulo 2 and check result if output is 0(A%2)
Step 5 → If true print A is even
Step 6 → If false print A is odd
Step 7->7 STOP
Flowchart

A flowchart can be diagrammatic representation of algorithm.


Flowchart is a graphical representation of an algorithm. Programmers often use it as a
program-planning tool to solve a problem. It makes use of symbols which are connected
among them to indicate the flow of information and processing.

Symbol Purpose Description

Flow line Indicates the flow of


logic by connecting
symbols.

Terminal(Stop/Start) Represents the start


and the end of a
flowchart.

Input/Output Used for input and


output operation.

Processing Used for arithmetic


operations and data-
manipulations.
Decision Used for decision
making between two
or more alternatives.

On-page Connector Used to join different


flowline

Off-page Connector Used to connect the


flowchart portion on a
different page.

Predefined Represents a group of


Process/Function statements performing
one processing task.

1. Add two numbers entered by the user.


2. Find the largest among three different numbers entered by the user.
a=100,b=170,c=89
for(k=2,k<n,k++)
If n%k==0
Not
Pseudocode for Algorithm Specification

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.

What notation will we use ?


We will write our algorithms as a sequence of pseudocode statements. Let us start with our
notation to represent data objects.

• 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 >.

• A scalar variable is a variable containing just one data item.

• 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]

result in the variable b holding the value 22.

• 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.

We now consider simple statements:

• 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

results in the value 2 stored in the variable i.

• Mathematical expressions are allowed on the right of an assignment. For example:

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.

• We may write a value to an output device. For example, the statement:

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.

We now consider something called control constructs. To express an algorithm, we must


be able to:

• group statements into a block,

• conditionally perform (or not perform) a statement or a block of statements depending


on a logical expression.

• and repeat a statement or block of statements.

We now introduce the details of each of these three control constructs.

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.

• Repeating a Statement (or a Block of Statements)


We can conditionally repeat a statement (or a block of statements) using the while
construct. For example:

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

Input: An integer n, and an array A containing n integers.

Output: The largest integer in the array.

Method:

t = A[0] // A[0] is our first candidate for the largest.


i=1 // i is our variable index.
while ( i < n ) { // Start a loop.
if ( t < A[i] ) {
t = A[i]
}
i=i+1
} // end while loop
write t // Output the answer

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

You might also like