Algorithm - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Algorithm

Description:

Algorithm: Is a planned set of steps to solve certain problem Ex.: Assume for instance that the gross pay of an employee is to be calculated and then 10 percent of ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 14
Provided by: CDS86
Category:
Tags: algorithm

less

Transcript and Presenter's Notes

Title: Algorithm


1
Algorithm
2
Algorithm
  • Is a planned set of steps to solve certain
    problem
  • Ex.
  • Assume for instance that the gross pay of an
    employee is to be calculated and then 10 percent
    of the gross is to remove as tax. The yield being
    the net pay. Write down the algorithm for this
    problem.
  • Sol.
  • Begin
  • input name, hours_worked, and wage par hour
  • calculate gross_pay hours_worked wage par
    hour
  • calculate tax (10/100) gross_pay
  • calculate net_pay gross_pay tax
  • print name, net_pay
  • End

3
Algorithm
  • Ex.
  • Write an algorithm to read the name and the mark
    (one) for one student then add to his mark 5
    marks
  • Sol.
  • Begin
  • input name, mark
  • new_mark mark5
  • print name, new_mark
  • End

4
Algorithm
  • Ex.
  • Write an algorithm to read the name and mark of
    student and calculate the new mark of each by
    adding 5 to its mark. We have 10 students
  • Sol.
  • Begin
  • studentcount 1
  • if studentcount gt 10 then Stop
  • else
  • read name, mark
  • calculate new_mark mark 5
  • print name, new_mark
  • studentcount studentcount 1
  • goto step 3

5
Pseudo code algorithm
  • It is an algorithm written in a language that is
    very close to high level languages
  • while do loop
  • syntax
  • while ( condition) do
  • begin
  • end
  • ex.
  • start
  • student_count 1
  • while (student_count lt 10) do
  • begin
  • read name, mark
  • new_mark mark 5
  • print name, new_mark
  • student_count student_count 1
  • end
  • stop

6
Pseudo code algorithm
  • repeat until loop
  • syntax
  • repeat
  • until (condition)
  • ex.
  • start
  • student_count 1
  • repeat
  • read name, mark
  • new_mark mark 5
  • print name, new_mark
  • student_count student_count 1
  • until (student_count gt 10)
  • stop

7
Pseudo code algorithm
  • For loop
  • syntax
  • for variable_name initial_value
    to end_value do
  • begin
  • end
  • ex.
  • start
  • for student_count 1 to 10 do
  • begin
  • read name, mark
  • new_mark mark 5
  • print name, new_mark
  • end
  • stop

8
Pseudo code algorithm
  • ex.
  • write an algorithm to compute the sum of n
    numbers
  • sol.
  • start
  • read n
  • sum 0
  • for I 1 to n
  • begin
  • sum sum I
  • end
  • write sum
  • stop

9
Pseudo code algorithm
  • ex.
  • write an algorithm to compute y 1 22/2 33/3
    nn/n
  • sol.
  • start
  • read n
  • sum 0
  • for I 1 to n do
  • begin
  • sum sum (I I) / I
  • end
  • y sum
  • print y
  • stop

10
Pseudo code algorithm
  • or
  • if ( condition) then
  • begin
  • end
  • else
  • if ( condition) then
  • begin
  • end
  • or
  • if ( condition) then
  • if ( condition) then
  • if ( condition) then
  • if ( condition) then
  • else
  • else
  • else
  • else
  • if statement
  • syntax
  • if ( condition) then
  • begin
  • end
  • or
  • if ( condition) then
  • begin
  • end
  • else
  • begin
  • end

11
Pseudo code algorithm
  • Ex.
  • Write an algorithm to find the maximum of two
    values
  • Sol.
  • Start
  • Read a, b
  • if ( agtb) then
  • max a
  • else
  • if ( bgta) then
  • max b
  • else
  • print (a b)
  • print max
  • Stop

12
Pseudo code algorithm
  • Ex.
  • Write an algorithm to find the maximum of three
    values
  • Sol.
  • Start
  • Read a, b, c
  • if ( agtb) then
  • if ( agtc) then
  • max a
  • else
  • if ( bgtc) then
  • max b
  • else
  • max c
  • else
  • if ( bgtc) then
  • max b
  • else
  • max c
  • print max

13
Pseudo code algorithm
  • ex.
  • write an algorithm to compute y 1- 22/2 33/3 -
    nn/n
  • sol.
  • start
  • read n
  • sum 0
  • for I 1 to n do
  • begin
  • if ( I mod 2 0) then
  • sum sum - (I I) / I
  • else
  • sum sum (I I) / I
  • end
  • y sum
  • print y
  • stop
Write a Comment
User Comments (0)
About PowerShow.com