Professional Documents
Culture Documents
Algorithmic WayPart 1
Table of Contents
Problem Solving The Algorithmic WayPart 1 ................................... 1
Session Objectives
Identify the need for problem solving in programming
Use algorithms for problem solving
Identify the structure of an algorithm
Identify the forms of algorithm
Session Breakup
S. No. Duration
Section
(Minutes)
1 Introduction to Problem Solving for 05
Programming
2 Need for Problem Solving in Programming 08
3 Algorithms for Problem Solving 05
4 Using Algorithms 02
5 What is an Algorithm? 03
6 Characteristics of an Algorithm 05
7 Steps for Creating an Algorithm 05
8 Structure of an Algorithm 10
9 Forms of Algorithms 05
10 Types of Algorithms 05
11 Check Your Understanding 05
12 Summary 02
Total 60 minutes
The following figure shows the correct way of arriving at a solution for a problem:
Problem solving and critical thinking help you write programs faster. When thinking in a group, you
tend to get innovative ideas for efficiently solving or developing a problem or program, respectively.
For example, a large program can be broken down into simpler smaller modules, which could be later
integrated. In this manner, the same program is shared by many developers, thereby saving time.
Moreover, smaller modules are easy to maintain and reuse.
In the preceding scenario, where you need to book a hotel for your team, the problem-solving
approach before booking the hotel should be:
1. Identify the problem:
Too many people
Rush due to festive season
Less time left for booking
2. Arrive at the solution:
The number of people going
Whether to book rooms on twin-sharing basis
Asking peers if someone has done such a task before
Asking your supervisor if the organisation has a corporate travel agent
The allotted budget
3. Develop the solution into an algorithm: Based on the responses to the preceding
questions, you arrive at the solution as:
Total number of persons going is 20.
The rooms can be booked on twin-sharing basis. Moreover, as the weather is cold,
non-AC rooms can be booked.
Contact the travel agent suggested by your supervisor to share the details of booking.
Inform the travel agent about the budget given by your supervisor.
4. Check the algorithm for results: On confirmation of the booking, verify that:
The number of rooms booked is correct.
Dates of arrival and departure are correct.
Location is appropriate.
The cost is within budget.
Using Algorithms
An algorithm enables the developer to choose the right thought process to arrive at the best solution
for a problem.
What is an algorithm?
An algorithm is a finite set of clearly defined steps that describe how you want to solve a problem. It
converts input, information relating to the problem, into output which is the solution by following a
defined set of steps, as shown in the following figure:
A computer program is itself an algorithm. However, because it is written according to the syntax of a
specific programming language, you tend to focus on syntactic issues and lose the focus on the
algorithm. The problem-solving approach to programming requires you to design an algorithm that
adheres to the good characteristics of a program and then write the code from the algorithm.
Characteristics of an Algorithm
An algorithm allows you to think, analyse and design a solution before developing a program. An
algorithm is written in a spoken language and not in any programming language, such that even a
person who does not know programming can easily understand it. Writing an algorithm in the initial
stages of programming helps eliminate errors of problem-solving logic so that when a developer
starts programming, he/she needs to consider only the language-specific issues.
Hence, the main characteristics of an algorithm are:
It should have a definite starting point.
The steps should be crisp and clear.
It should be accurate.
It should arrive at the correct solution.
It should not get stuck in an infinite loop.
It should end at a definite point.
The following figure represents the characteristics of an algorithm with the help of the previously
discussed example of booking a hotel:
The steps involved for creating an algorithm are shown in the following figure:
Consider the example of reaching college by 9 a.m., which was discussed previously. You can take
one of the two modes of transportthe city bus or the metro. The steps for writing an algorithm for this
scenario would be as follows:
1. Identify the problem.
You need to reach the college by 9 a.m.
2. Analyse the problem.
The bus stop is near your house, but the bus may take more time depending on the
traffic. If you take the bus, you may reach the college in 25 minutes or you might get
stuck in traffic and take more time to reach the college.
On the other hand, the metro station is little far from your house. But, once you reach
the metro station, you will reach the college in 15 minutes.
3. Identify the solutions.
Taking the bus is an indefinite solution, and you may or may not reach the college on
time.
Taking the metro will definitely make you reach college on time.
4. Evaluate the solutions.
If you leave before 8:30, then you can take the bus, as the bus stand is closer to your
home.
If you leave at 8:30, you can take either the bus or the metro.
If you leave after 8:30, then you should take the metro, as the metro would take lesser
travelling time than the bus.
5. Select the best solution.
Since its 8:25, you should take the bus, as the bus stop is closer to your home.
Structure of an Algorithm
Every algorithm should have clearly defined sections written in an order, as shown in the following
figure:
Self-Read
An algorithm for the preceding scenario, where you need to book a hotel for the team members of
your organisation, would be as follows:
Example 2: Write an algorithm to calculate the tax amount for the employees of your organisation.
Solution
Step 1: Start.
Step 2: Declare the variables: salary as double and tax as float.
Step 3: Read the value of salary.
Step 4: If salary < 1,00,000, then
tax = 0
otherwise
if salary > 1,00,000 and salary < 1,50,000 then
tax = salary * 0.05
otherwise
tax = salary * 0.30
Step 5: Display tax.
Step 6: Stop.
Example 3: Your supervisor approaches you to determine the bonus for every employee in your
department. An employee is entitled for a bonus if he/she has taken less than two leaves in a quarter.
You need to design an algorithm to calculate the bonus.
Solution
Step 1: Start.
Step 2: Declare the variables: employee_name as an array of characters, leaves_in_quarter as float
and BONUS as double.
Step 3: Read the values of employee_name and leaves_in_quarter.
Step 4: If (leaves_in_quarter < 2), then
BONUS = 10% of salary
otherwise
BONUS = 0
Step 5: Display BONUS for, employee_name is Rs. BONUS.
Step 6: Stop.
Typically, while writing algorithms, developers tend to make mistakes.
Consider the example in which you need to add two numbers that would be entered by the user. The
following example depicts some mistakes while writing an algorithm:
Step 1: Start.
Step 2: Find sum.
Step 3: Add num1 and num2 and assign the result to sum.
Step 4: Read values num1 and num2.
Step 5: Display sum.
Step 6: Stop.
To correct the preceding errors, the correct steps for writing the algorithm would be as follows:
Step 1: Start.
Step 2: Declare the variables: num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum.
sum = num1 + num2
Step 5: Display the sum.
Step 6: Stop.
Forms of Algorithms
There are two forms of AlgorithmPseudocode and Flow chart. The following figure depicts the
characteristics of the two forms of algorithms:
Types of Algorithms
There are many different ways of classifying algorithms. It enables you to understand how an
algorithm works. It also helps in choosing the right technique to analyse the algorithm to solve a
problem. It is important for a developer to appreciate the right kind of algorithm based on the scope of
the problem statement.
An algorithm may be classified as:
Deterministic vs. Randomized
Deterministic algorithms give the same result every time for the same input and same
steps. Randomised algorithms, on the other hand, give a different result each time for the
same input.
Offline vs. Online
Online algorithms are computed based on the input given online (by the user), whereas
input is known beforehand in Offline algorithms.
Exact vs. Approximate
Exact algorithms are used to arrive at the exact solution, whereas approximate algorithms
aim at arriving at a solution that is proximity of the exact goal.
Simple recursive algorithm
Recursive algorithm breaks a problem into smaller parts until you arrive at a simpler
sub-problem that can be easily solved.
Backtracking algorithm
In case of backtracking algorithm, each time a solution is generated, it is tested if all
conditions are satisfied to continue further, else you need to back track and go on a
different path.
Divide-and-conquer algorithm
Self-Read
To read more about types of algorithms, please visit the following website:
http://www.mi.fu-berlin.de/wiki/pub/ABI/DiscretMathWS10/runtime.pdf
Summary
In this session, you learned:
Program design includes the steps a programmer should take before he/she starts coding
the program in a specific language.
There are various disadvantages of coding straightaway for a problem.
Your customers requirement is the problem statement, and your responsibility as a
developer is to give a solution to the problem by writing a program.
Problem solving in programming is done through algorithms.
An algorithm is a procedure for solving a problem using a sequence of instructions.
Writing an algorithm in the initial stages of programming helps eliminate errors of
problem-solving logic so that when a developer starts programming, he/she needs to
consider only the language-specific issues.
The various steps for writing an algorithm are:
Identify the problem.
Analyse the problem.
Identify solutions.
Evaluate solutions.
Select the best solution.
Develop an action plan.
Implement the solution.
There are two forms of AlgorithmPseudocode and Flow chart.
Pseudocode literally means fake code as it is not a programming code. It only specifies the
steps to perform a task.
Flow charts represent the steps of problem solving using symbols and texts.
Question 2:
The two forms of Algorithm are Pseudocode and Flow chart.
Question 3:
The correct sequence of steps of writing an algorithm is:
c. Analyse the problem Select the solution Develop the plan Implement the solution
Question 4:
True. Every algorithm needs to have a header and a terminator.
Question 5:
The correct algorithm to find the average of any three numbers is:
Step 1: Start.
Step 2: Declare the variables: num1 as int, num2 as int, num3 as int, sum as int and average as
float.
Step 3: Read the values num1, num2 and num3.
Step 4: Add num1, num2 and num3 and assign to sum.
sum = num1 + num2 + num3
Step 5: Divide the sum by 3 and assign to average.
average = sum / 3
Step 6: Display the average.
Step 7: Stop.
Question 6: A good program should be easy to maintain.
Question 7:
The algorithm for checking the eligibility of the member for promotion and displaying his/her result is
as follows:
Step 1: Start.
Step 2: Declare the variables: employee_name, no_of_years and test_percentage.
Step 3: Read employee_name, no_of_years, and test_percentage.
Step 4: If (no_of_years < 2), then
Display You are not eligible for the test
otherwise
if (test_percentage >= 70)
display You are promoted!
otherwise
display You are not promoted. You need to work hard!
Step 5: Stop.
Question 8:
The algorithm to calculate the increment is as follows:
Step 1: Start.
Step 2: Declare the variables: employee_name, no_of_tests_cleared and increment.
Step 3: Read salary, employee_name and no_of_tests_cleared.
Step 4: If (no_of_tests_cleared = 3), then
increment = 10% of salary
otherwise
if (no_of_tests_cleared = 2)
increment = 5% of salary
otherwise
increment = 0
Step 5: Stop.
Question 9:
The algorithm to calculate the salary of the employees is as follows:
Step 1: Start.
Step 2: Declare the variables: rate, hours and salary.
Step 3: Read the values, hours and rate.
Step 4: Multiply hours with rate to calculate salary.
salary = hours * rate
Step 5: Display the salary.
Step 6: Stop.
Question 10: The missing step is to develop the solution into an algorithm.