Introduction To Ict.: (Algorithms.) Lecture # 15-16 By: M.Nadeem Akhtar. Lecturer. Department of CS & IT
Introduction To Ict.: (Algorithms.) Lecture # 15-16 By: M.Nadeem Akhtar. Lecturer. Department of CS & IT
(ALGORITHMS. )
Lecture # 15-16
By:
M.Nadeem Akhtar.
Lecturer.
Department of CS & IT.
1
URL: https://sites.google.com/site/nadeemcsuoliict/home/lectures
DEFINING THE PROBLEM
• A well-defined problem has five components:
8
EXAMPLE
• 1+1=2
• 2+1=3
• 3+1=4
• 4+1=5
= 6, 7, 8…
in first 2, 3 iteration the child discover a pattern i.e. the
answer is always one more than the previous answer.
Despite added two numbers he discovered the trend or
pattern.
That’s how generally people work. 1st they solve the
problem for a particular case, then another case and so
on. And discover trends and patterns in the problem.
And from those trends and pattern they make a
general purpose solution. Which may be efficient.
• If you want to solve a problem, it helps if you have
experienced that problem or similar ones before.
STEPS
Sequence
• Steps:
When we divide a task into smaller task, we
may call them steps.
Example: Walking
• Algorithm:
Sequence of steps, these steps must be in
precise sequence.
Example: making an egg.
EXAMPLES
1) Addition (ADDING TWO NUMBERS)
2) Conversion from decimal to binary.
3) The process of boiling an egg.
4) The process of mailing a letter.
5) Sorting (Tallest person)
6) Searching (google follow an algorithm to
search your appropriate page)
Let us write down the algorithm for a
problem that is familiar to us
14
Algorithm for a problem
• Converting a decimal number into binary.
2 125 Remainder Algorithm in Structured English
2 62 1
1. Write the decimal number.
2 31 0
2 15 1 2. Divide by 2; write quotient and
remainder.
2 7 1
2 3 1 3. Repeat step 2 on the quotient;
keep on repeating until the
2 1 1 quotient becomes zero.
0 1
4. Write all the remainder digits
in the reverse order (last
remainder first) to form the final
result.
Points to Note:
1) The process consists of repeated application
of simple steps.
2) All steps are unambiguous (clearly defined).
3) We are capable of doing all steps.(all steps are
executable)
4) Only finite number of steps is need to be
taken.
5) The required result will be found.
6) Moreover, the process will stop at that point.
Algorithm (Better Definition)
• 1st Definition:
Sequence of steps that can be taken to solve a
problem.
• Better Definition:
A precise sequence of a limited number of
unambiguous, executable steps that terminates
in the form of a solution.
THREE REQUIREMENT
1. Sequence is:
a. Precise
b. Consists of limited number of steps
– Greedy Algorithm
– Deterministic Algorithm
– Randomized Algorithm
TRAVELING SALESMAN PROBLEM
• A salesman needs to visit each of the n cities.
120 C 70
B F
60
100 40 30
80 D 50
A G
90
70 110
E
– Pseudo Code
– Flowcharts
– Actual Code
– Structured English
FLOWCHART
• A graphical representation of a process (e.g.
an algorithm), in which graphic objects are
used to indicate the steps & decisions that are
taken as the process moves along from start
to finish.
Start or stop
Process
Input or output
Flowchart
Elements Decision
Flow line
Connector
Off-page connector
PSEUDO CODE
• Language that is typically used for writing
algorithms.
Calculate Percentage
Calculate Percentage
Sum ← Num1 + Num2
Average ← Sum/2 Sum ← Num1 + Num2
Average ← Sum/2
CONDITIONALS
• Select between alternate courses of action
depending upon the evaluation of condition:
Pseudo Code Flowchart
True False
If (condition = true) condition
statement block 1
Else
statement statement
statement block 2 block 1 block 2
End If
LOOPs
• Loop through a set of statements as long as a
condition is true:
Pseudo Code Flowchart
Loop while (condition=true)
Do statement block
End Loop
statement
True
block condition
False
EXAMPLE OF CONDITIONS
• Toss a coin, if the head will occur, then I will
not do my homework assignment. If the tail
will occur, then I will do my homework
assignment.
• Pseudo code
– Input Number1
– Input Number2
– Sum ← Number1 + Number2
– Average ← Sum/2
– Output Sum, Average
38
Flow Chart
START
Input
Num1, Num2
Sum ← Number1 +
Number2
Average ← Sum/2
Output Sum,
Average
STOP 39
ACTUAL CODE IN PASCAL
Program FindSumAverage;
Var
Number1, Number2, Sum, Average : Real;
Begin
write (‘First number? ’); Readln(Number1);
write (‘Second number? ’); Readln(Number2);
• Pseudo code
– Input coefficients (a, b, c) of the quadratic equation
– Calculate d = sqrt ( b 4ac )
2
Read Input
a, b, c
d = sqrt(b x b – 4 x a x c)
x1 = (–b + d) / (2 x a)
X2 = (–b – d) / (2 x a)
Write Output
X1, X2
STOP
42
PROBLEM STATEMENT
• Converting a decimal number into binary.
2 125 Remainder
2 62 1
2 31 0
2 15 1
2 7 1
2 3 1
2 1 1
0 1
12510 = 11111012
SOLUTION IN PSEUDO CODE
1) Let the decimal number be an integer x, x > 0
(whenever you write an algorithm you take
assumptions, so in this case we take assumption that
number is integer and it must be greater than 0.)
2) Let the binary equivalent be an empty string y
3) Repeat while x > 0 {
Determine the quotient & remainder of x ÷ 2
y = CONCATENATE(remainder, y)
x = quotient
}
4) Output y
5) Stop
Start Flowchart of Decimal
to Binary Conversion
Read x
Output y
x is the decimal number
Stop y is the binary equivalent 45
DECIMAL TO BINARY CONVERSION IN JAVASCRIPT
<SCRIPT type=“text/javascript”>
x = 125; // x is the decimal number
y = “”; // y is the binary equivalent
while ( x > 0) {
remainder = x % 2;
quotient = Math.floor( x / 2 );
y = remainder + y; NOTE: Don’t
x = quotient; worry if you
} don’t
understand this
document.write(“y = ” + y);
code for now;
</SCRIPT> you will - later!
46
EXAMPLES
• Students appears in an examination, which
consists of total 10 subjects. Each subject having
maximum marks of 100.
– The roll number of the student, his/her name, and the
marks obtained by him/her in various subjects, are
supplied as input data.
– We want to make a list of only those students who
have passed (obtained 50% or more marks) in the
examination.
– In the end, we also want to print out the total number
of students who have passed.
– The input data of all students is terminated by a trailer
record, which has sentinel value of 9999999 for Roll
No.
Start Flowchart for the Exam Process
Count = 0
Read
Input
Add 1 to Count
Write
Is RollNo = No Add marks of all Output
999999? Subjects (Total) Yes
Yes No
Percentage = Is percentage
Write Count
Total / 10 = >50?
48
Stop
SOLUTION IN PSEUDO CODE
1) Set Count to zero
2) Read first student record
3) Repeat while RollNo is not equal to 9999999 {
Calculate Total Marks
Calculate Percentage = Total / 10
IF Percentage => 50
Write Output
Add 1 to Count
ENDIF
}
4) Write Count
5) Stop
ACTUAL CODE OF EXAMPLE IN JAVASCRIPT
<SCRIPT type=“text/javascript”>
var marks = new Array(5);
var student, roll;
roll = window.prompt("Enter Student Roll No","");
if(roll=="999999"){
document.write(“Program Terminate”);
exit();
}else{
student = window.prompt("Enter Student Name","");
for(k=0; k<marks.length; k++){
marks[k] = parseInt(window.prompt("Enter Student subject marks",""));
}
var total = marks[0] + marks[1] + marks[2] + marks[3] + marks[4];
var percentage = total/5;
Read A, B, C
Yes If
No If No
A is larger B>A && B>C
A>B && A>C
Print A
Yes
B is larger C is larger
Print B Print C
STOP
ACTUAL CODE OF LARGEST NO. EXAMPLE IN JAVASCRIPT
<SCRIPT type=“text/javascript”>
– Those earning less than Rs. 30,000 are to be paid Rs. 1000
57
Pros and Cons of Pseudo Code (1)
• Quite suitable for SW development as it is closer in
form to real code
58
Pros and Cons of Pseudo Code (2)
• Pseudo code can be constructed quite quickly as
compared with a flowchart
59