Algorithm Design and
Problem Solving
Kaavya gaur
Introduction
In order to build a computer system that performs a
specific task or solves a given problem, the task or
problem has to be clearly defined, showing what is
going to be computed and how it is going to be
computed.
This chapter introduces the tools and techniques that
are used to design a software solution that together
with the associated computer hardware will form a
computer system.
What is a computer system?
• A COMPUTER SYSTEM is made up of software, data, hardware, communications
and people.
• each computer system can be divided up into a set of sub-systems. Each subsystem
can be further divided into sub-systems and so on until each sub-system just
performs a single action.
For example, when I wake up in the morning I use an app on my smart phone for my alarm, I then check the
weather forecast on my computer before I drive to work.
The alarm program is a very small computer system; when I check the weather forecast I
obtain information from one of the largest computer systems in the world
Activity
• Identify at least five computer systems you frequently use in your daily life.
See if you can decide the size of each system.
Tools and techniques
• In order to understand how a computer system is built up and how it works,
it is often divided up into sub-systems.
• This division can be shown using :
 Top-down design
 Sub-system
 Sub-routine
 Flowcharts or pseudocode
Top-down design
• TOP-DOWN DESIGN is the breaking down
of a computer system into a set of
subsystems, then breaking each sub-system
down into a set of smaller sub-systems, until
each sub-system just performs a single action.
• The process of breaking down into smaller
sub-systems is called ‘stepwise refinement’.
Structure diagrams
• The STRUCTURE DIAGRAM shows the design of a computer system in a
hierarchical way, with each level giving a more detailed breakdown of the
system into sub-systems.
• For eg : Alarm app for a smart phone
Structure diagram for alarm app
Flowcharts
Flowchart for checking-for-the-alarm-time sub-system
This symbol
means this
process is defined
elsewhere.
flowchart for the algorithm
to calculate the cost of
buying a given number of
tickets.
Library routines and Sub-routines
• A LIBRARY ROUTINE is a set of programming instructions for a given
task that is already available for use. It is pre-tested and usually performs a
task that is frequently required. For example, the task ‘get time’ in the
checking-for-the-alarm time algorithm would probably be readily available as
a library routine.
• A SUB-ROUTINE is a set of programming instructions for a given task
that forms a sub system, not the whole system. Sub-routines written in high-
level programming languages are called ‘procedures’ or ‘functions’ depending
on how they are used.
HOW TO WRITE PSEUDOCODE
• Always capitalize the initial word (often one of the main six constructs). (READ, WRITE, IF,
WHILE, UNTIL).
• Make only one statement per line.
• Indent to show hierarchy, improve readability, and show nested constructs.
• Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.).
• Keep your statements programming language independent.
• Use the naming domain of the problem, not that of the implementation. For instance: “Append
the last name to the first name” instead of “name = first+ last.”
• Keep it simple, concise and readable.
THE MAIN CONSTRUCTS OF
PSEUDOCODE
• SEQUENCE represents linear tasks sequentially performed one after the
other.
• WHILE a loop with a condition at its beginning.
• REPEAT-UNTIL a loop with a condition at the bottom.
• FOR another way of looping.
• IF-THEN-ELSE a conditional statement changing the flow of the
algorithm.
Conditional statements
• A condition that can be true or false:
IF … THEN … ELSE … ENDIF,
• for example
IF Age < 18
THEN PRINT "Child"
ELSE PRINT "Adult"
ENDIF
• A choice between several different values:
• CASE … OF … OTHERWISE … ENDCASE,
• for example:
CASE Grade OF
"A" : PRINT "Excellent"
"B" : PRINT "Good"
"C" : PRINT "Average”
OTHERWISE PRINT "Improvement is needed"
ENDCASE
Comparison operators
The algorithm below checks if a percentage mark is valid and a pass or a fail.
This makes use of two IF statements. The second IF statement is part of the
ELSE path of the first IF statement. This is called a nested IF.
Loop structures
FOR … TO … NEXT
REPEAT … UNTIL
• This loop structure is used
when the number of
repetitions/iterations is not
known and the actions are
repeated UNTIL a given
condition becomes true.
The actions in this loop are
always completed at least
once
WHILE … DO … ENDWHILE
• This loop structure is used when
the number of
repetitions/iterations is not
known and the actions are only
repeated WHILE a given
condition is true. If the WHILE
condition is untrue when the loop
is first entered then the actions in
the loop are never performed.
Pseudocode
• PSEUDOCODE is a simple method of showing an algorithm, using
English-like words and mathematical operators that are set out to look like a
program
Pseudocode for the
checking-for-the-
alarm-time
algorithm
Tickets are sold for a concert at $20
each. If 10 tickets are bought then the
discount is 10%; if 20 tickets are
bought the discount is 20%. No more
than 25 tickets can be bought in a single
transaction. a Use pseudocode to write
an algorithm to calculate the cost of
buying a given number of tickets.
• REPEAT
• PRINT "How many tickets would you like to buy?"
• INPUT NumberOfTickets
• UNTIL NumberOfTickets > 0 AND NumberOfTickets < 26
• IF NumberOfTickets < 10
• THEN Discount ← 0
• ELSE
• IF NumberOfTickets < 20
• THEN Discount ← 0.1
• ELSE Discount ← 0.2
• ENDIF
• ENDIF
• Cost ← NumberOfTickets * 20 * (1 – Discount)
• PRINT "Your tickets cost", Cost
Standard Method's of solution
• Totalling
• Counting
• Finding maximum , minimum and average values
• Searching using a linear search
Standard Method's of solution
• Variable Assignments
When devising an algorithm the programmer will need to use variables and assign values to them. This is indicated
in pseudocode using an arrow symbol (←). The arrow points from the value being assigned towards the variable it
is being assigned to. The following line of pseudocode should be read as 'a becomes equal to 34'.
a ← 34
• Totaling and Counting : The assignment operator can also be used when finding totals, as shown in the
following example where x becomes equal to a plus b.
x ← a + b
Similarly we can also use the assignment operator for counting, by assigning a variable to become equal to the value
of itself plus 1.
x ← x + 1
• Input and Output : Output is the act or returning some data to the user of
the program. This could be in the form of a written message, a numerical
value, an image, video or sound. This is shown in pseudocode by writing
OUTPUT followed by what the output will be. The example below shows
how we would output the message 'Hello World':
OUTPUT 'Hello World'
Read Number n and print the integers
counting upto n
BEGIN
READ n
INITIALIZE i to 1
FOR i <= n, then
DISPLAY i
INCREMENT i
END FOR
END
Psuedo code for finding max,min and average
marks from the class.
MaxMarks  StudentMarks[1]
MinMarks StudentMarks[1]
For Counter 2 TO ClassSize
IF StudentMarks[Counter] > MaxMArks
THEN
MaxMarks  StudentMarks[Counter]
End if
IF StudentMark[Counter] < MinMarks
THEN
MinMarks StudentMark[Counter]
ENDIF
NEXT Counter
Total  0
For Counter  1 TO ClassSize
Total  Total +
StudentMArks[Counter]
NEXT Counter
Average  Total / ClassSize
Pseudocode for linear search
OUTPUT: “Please enter name to find”
INPUT : Name
Found  FALSE
Counter  1
REPEAT
IF Name = StudentName[Counter]
THEN
Found  TRUE
ELSE
Counter  Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
THEN
OUTPUT Name “ Found at
position”, Counter,” in the list.”
ELSE
OTPUT NAME ,’Not found”
ENDIF

Algorithm Design and Problem Solving [Autosaved].pptx

  • 1.
    Algorithm Design and ProblemSolving Kaavya gaur
  • 2.
    Introduction In order tobuild a computer system that performs a specific task or solves a given problem, the task or problem has to be clearly defined, showing what is going to be computed and how it is going to be computed. This chapter introduces the tools and techniques that are used to design a software solution that together with the associated computer hardware will form a computer system.
  • 3.
    What is acomputer system? • A COMPUTER SYSTEM is made up of software, data, hardware, communications and people. • each computer system can be divided up into a set of sub-systems. Each subsystem can be further divided into sub-systems and so on until each sub-system just performs a single action.
  • 4.
    For example, whenI wake up in the morning I use an app on my smart phone for my alarm, I then check the weather forecast on my computer before I drive to work. The alarm program is a very small computer system; when I check the weather forecast I obtain information from one of the largest computer systems in the world
  • 5.
    Activity • Identify atleast five computer systems you frequently use in your daily life. See if you can decide the size of each system.
  • 6.
    Tools and techniques •In order to understand how a computer system is built up and how it works, it is often divided up into sub-systems. • This division can be shown using :  Top-down design  Sub-system  Sub-routine  Flowcharts or pseudocode
  • 7.
    Top-down design • TOP-DOWNDESIGN is the breaking down of a computer system into a set of subsystems, then breaking each sub-system down into a set of smaller sub-systems, until each sub-system just performs a single action. • The process of breaking down into smaller sub-systems is called ‘stepwise refinement’.
  • 8.
    Structure diagrams • TheSTRUCTURE DIAGRAM shows the design of a computer system in a hierarchical way, with each level giving a more detailed breakdown of the system into sub-systems. • For eg : Alarm app for a smart phone
  • 9.
  • 10.
  • 12.
    Flowchart for checking-for-the-alarm-timesub-system This symbol means this process is defined elsewhere.
  • 13.
    flowchart for thealgorithm to calculate the cost of buying a given number of tickets.
  • 14.
    Library routines andSub-routines • A LIBRARY ROUTINE is a set of programming instructions for a given task that is already available for use. It is pre-tested and usually performs a task that is frequently required. For example, the task ‘get time’ in the checking-for-the-alarm time algorithm would probably be readily available as a library routine. • A SUB-ROUTINE is a set of programming instructions for a given task that forms a sub system, not the whole system. Sub-routines written in high- level programming languages are called ‘procedures’ or ‘functions’ depending on how they are used.
  • 15.
    HOW TO WRITEPSEUDOCODE • Always capitalize the initial word (often one of the main six constructs). (READ, WRITE, IF, WHILE, UNTIL). • Make only one statement per line. • Indent to show hierarchy, improve readability, and show nested constructs. • Always end multi-line sections using any of the END keywords (ENDIF, ENDWHILE, etc.). • Keep your statements programming language independent. • Use the naming domain of the problem, not that of the implementation. For instance: “Append the last name to the first name” instead of “name = first+ last.” • Keep it simple, concise and readable.
  • 16.
    THE MAIN CONSTRUCTSOF PSEUDOCODE • SEQUENCE represents linear tasks sequentially performed one after the other. • WHILE a loop with a condition at its beginning. • REPEAT-UNTIL a loop with a condition at the bottom. • FOR another way of looping. • IF-THEN-ELSE a conditional statement changing the flow of the algorithm.
  • 17.
    Conditional statements • Acondition that can be true or false: IF … THEN … ELSE … ENDIF, • for example IF Age < 18 THEN PRINT "Child" ELSE PRINT "Adult" ENDIF
  • 18.
    • A choicebetween several different values: • CASE … OF … OTHERWISE … ENDCASE, • for example: CASE Grade OF "A" : PRINT "Excellent" "B" : PRINT "Good" "C" : PRINT "Average” OTHERWISE PRINT "Improvement is needed" ENDCASE
  • 19.
  • 20.
    The algorithm belowchecks if a percentage mark is valid and a pass or a fail. This makes use of two IF statements. The second IF statement is part of the ELSE path of the first IF statement. This is called a nested IF.
  • 21.
  • 22.
    FOR … TO… NEXT
  • 23.
    REPEAT … UNTIL •This loop structure is used when the number of repetitions/iterations is not known and the actions are repeated UNTIL a given condition becomes true. The actions in this loop are always completed at least once
  • 24.
    WHILE … DO… ENDWHILE • This loop structure is used when the number of repetitions/iterations is not known and the actions are only repeated WHILE a given condition is true. If the WHILE condition is untrue when the loop is first entered then the actions in the loop are never performed.
  • 25.
    Pseudocode • PSEUDOCODE isa simple method of showing an algorithm, using English-like words and mathematical operators that are set out to look like a program Pseudocode for the checking-for-the- alarm-time algorithm
  • 26.
    Tickets are soldfor a concert at $20 each. If 10 tickets are bought then the discount is 10%; if 20 tickets are bought the discount is 20%. No more than 25 tickets can be bought in a single transaction. a Use pseudocode to write an algorithm to calculate the cost of buying a given number of tickets. • REPEAT • PRINT "How many tickets would you like to buy?" • INPUT NumberOfTickets • UNTIL NumberOfTickets > 0 AND NumberOfTickets < 26 • IF NumberOfTickets < 10 • THEN Discount ← 0 • ELSE • IF NumberOfTickets < 20 • THEN Discount ← 0.1 • ELSE Discount ← 0.2 • ENDIF • ENDIF • Cost ← NumberOfTickets * 20 * (1 – Discount) • PRINT "Your tickets cost", Cost
  • 27.
    Standard Method's ofsolution • Totalling • Counting • Finding maximum , minimum and average values • Searching using a linear search
  • 28.
    Standard Method's ofsolution • Variable Assignments When devising an algorithm the programmer will need to use variables and assign values to them. This is indicated in pseudocode using an arrow symbol (←). The arrow points from the value being assigned towards the variable it is being assigned to. The following line of pseudocode should be read as 'a becomes equal to 34'. a ← 34 • Totaling and Counting : The assignment operator can also be used when finding totals, as shown in the following example where x becomes equal to a plus b. x ← a + b Similarly we can also use the assignment operator for counting, by assigning a variable to become equal to the value of itself plus 1. x ← x + 1
  • 29.
    • Input andOutput : Output is the act or returning some data to the user of the program. This could be in the form of a written message, a numerical value, an image, video or sound. This is shown in pseudocode by writing OUTPUT followed by what the output will be. The example below shows how we would output the message 'Hello World': OUTPUT 'Hello World'
  • 30.
    Read Number nand print the integers counting upto n BEGIN READ n INITIALIZE i to 1 FOR i <= n, then DISPLAY i INCREMENT i END FOR END
  • 31.
    Psuedo code forfinding max,min and average marks from the class. MaxMarks  StudentMarks[1] MinMarks StudentMarks[1] For Counter 2 TO ClassSize IF StudentMarks[Counter] > MaxMArks THEN MaxMarks  StudentMarks[Counter] End if IF StudentMark[Counter] < MinMarks THEN MinMarks StudentMark[Counter] ENDIF NEXT Counter Total  0 For Counter  1 TO ClassSize Total  Total + StudentMArks[Counter] NEXT Counter Average  Total / ClassSize
  • 32.
    Pseudocode for linearsearch OUTPUT: “Please enter name to find” INPUT : Name Found  FALSE Counter  1 REPEAT IF Name = StudentName[Counter] THEN Found  TRUE ELSE Counter  Counter + 1 ENDIF UNTIL Found OR Counter > ClassSize THEN OUTPUT Name “ Found at position”, Counter,” in the list.” ELSE OTPUT NAME ,’Not found” ENDIF