Algorithmdesignandproblemsolvingautosaved 230525024624 6a6fb3b2
Algorithmdesignandproblemsolvingautosaved 230525024624 6a6fb3b2
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
Pseudocode for
the checking-
for-the-alarm-
time algorithm
• REPEAT
• PRINT "How many tickets would you like to
buy?"
• INPUT NumberOfTickets
• UNTIL NumberOfTickets > 0 AND
Tickets are sold for a concert NumberOfTickets < 26
at $20 each. If 10 tickets are • IF NumberOfTickets < 10
bought then the discount is • THEN Discount ← 0
10%; if 20 tickets are bought • ELSE
the discount is 20%. No more
• IF NumberOfTickets < 20
than 25 tickets can be bought
in a single transaction. a Use • THEN Discount ← 0.1
pseudocode to write an • ELSE Discount ← 0.2
algorithm to calculate the cost • ENDIF
of buying a given number of • ENDIF
tickets.
• 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]
Total 0
For Counter 2 TO ClassSize
For Counter 1 TO ClassSize
IF StudentMarks[Counter] > MaxMArks Total Total +
THEN StudentMArks[Counter]
NEXT Counter
MaxMarks StudentMarks[Counter]
Average Total / ClassSize
End if
IF StudentMark[Counter] < MinMarks
THEN
MinMarks
StudentMark[Counter]
ENDIF
NEXT Counter
Pseudocode for linear search
OUTPUT: “Please enter name to find”
INPUT : Name THEN
Found FALSE OUTPUT Name “ Found at
Counter 1 position”, Counter,” in the
REPEAT list.”
IF Name = StudentName[Counter] ELSE
THEN OTPUT NAME ,’Not
Found TRUE found”
ELSE ENDIF
Counter Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize