Part a CHP 4 Problem Solving 1
Part a CHP 4 Problem Solving 1
▪ Developing an Algorithm
▪ Coding
▪ Read and analyze the problem statement carefully in order to list the
principal components of the problem and decide the core
functionalities that our solution should have
Pseudocode is as follows:
• INPUT Age
• IF Age < 13 THEN
• PRINT "Child"
• ELSE IF Age < 20 THEN
• PRINT "Teenager"
• ELSE
• PRINT "Adult"
Flowchart
Repetition
• Write pseudocode and draw a flowchart to
• accept 5 numbers and find their average.
• The flowchart representation is shown in Figure 4.10.
• Pseudocode will be as follows:
• Step 1: SET count = 0, sum = 0
• Step 2: WHILE count < 5 , REPEAT steps 3 to 5
• Step 3: INPUT a number to num
• Step 4: sum = sum + num
• Step 5: count = count + 1
• Step 6: COMPUTE average = sum/5
• Step 7: PRINT average
Flowchart
Write pseudocode and draw flowchart to accept numbers till the
user enters 0 and then find their average.
• Pseudocode is as follows:
• Step 1: SET count = 0, sum = 0
• Step 2: INPUT num
• Step 3: WHILE num is not equal to 0, REPEAT Steps 4 to 6
• Step 4: sum = sum + num
• Step 5: count = count + 1
• Step 6: INPUT num
• Step 7: COMPUTE average = sum/count
• Step 8: PRINT average
Verifying Algorithms
• when an algorithm is written, to verify that it is working as expected.
• To verify, we have to take different input values and go through all the
steps of the algorithm to yield the desired output for each input value and
may modify or improve as per the need.
• The method of taking an input and running through the steps of the
algorithm is sometimes called dry run.
• Such a dry run will help us to:
1. Identify any incorrect steps in the algorithm
2. Figure out missing details or specifics in the algorithm
• It is important to decide the type of input value to be used for the
simulation. In case all possible input values are not tested, then the
program will fail.
Comparison of Algorithm
(i) Algorithm requires large number of calculations (means more processing time) as it checks for all the
numbers as long as the divisor is less than the number. If the given number is large, this method will take more
time to give the output.
(ii)Algorithm is more efficient than
(ii) Algorithm as it checks for divisibility till half the number, and thus it reduces the time for computation of
the prime number.
(iii) is even more efficient as it checks for divisibility till square root of the number, thereby further reducing the
time taken. As algorithm
(iv) uses only the prime numbers smaller than the given number for divisibility, it further reduces the
calculations.
But in this method we require to store the list of prime numbers first. Thus it takes additional memory even
though it requires lesser calculations.
Algorithms can be compared and analysed on the basis of the amount of processing time they need to run and
the amount of memory that is needed to execute the algorithm. These are termed as time complexity and space
complexity, respectively. The choice of an algorithm over another is done depending on how efficient they are in
terms of proecssing time required (time complexity) and the memory they utilise (space complexity)
Coding
• An algorithm is finalised, it should be coded in a high-level programming language as selected by the programmer.
• The ordered set of instructions are written in that programming language by following its syntax.
• Syntax is the set of rules or grammar that governs the formulation of the statements in the language, such as spellings, order of
words, punctuation, etc.
• An advantage of using high-level languages is that they are portable, i.e., they can run on different types of computers with little
or no modifications.
• Low-level programs can run on only one kind of computer and have to be rewritten in order to run on another type of system.
• A wide variety of high-level languages, such as FORTRAN, C, C++, Java, Python, etc., exist.
• A program written in a high-level language is called source code.
• We need to translate the source code into machine language using a compiler or an interpreter, so that it can be understood by
the computer.
• There are multiple programming languages available and choosing the one suitable for our requirements requires us to consider
many factors.
• It depends on the platform (OS) where the program will run. To decide whether the application would be a desktop application,
a mobile application or a web application.
• Desktop and mobile applications are generally developed for a particular operating system and for certain hardware whereas the
web applications are accessed in different devices using web browsers and may use resources available over cloud. Besides,
programs are developed not only to work on a computer, mobile or a web browser, but it may also be written for embedded
systems like digital watch, mp3 players, traffic signals or vehicles, medical equipment and other smart devices. In such cases, we
have to look for other specialised programming tools or sometimes write programs in assembly languages.
Decomposition
• The basic idea of solving a complex problem by decomposition is to
'decompose' or break down a complex problem into smaller sub problems
• These sub problems are relatively easier to solve than the original
problem. Finally, the sub problems are combined in a logical way to obtain
the solution for the bigger, main problem.
• Breaking down a complex problem into sub problems also means that
each sub problem can be examined in detail. Each sub problem can be
solved independently and by different persons (or teams).
• Having different teams working on different sub problems can also be
advantageous because specific sub problems can be assigned to teams who
are experts in solving such problems.
• There are many real life problems which can be solved using
decomposition. Examples include solving problems in mathematics and
science, events management in school, weather forecasting, delivery
management system, etc.