You are on page 1of 19

Collaborate

From the Expert’s Desk


• You have learned in the previous lessons about algorithms and pseudocode. This
section will provide the guidelines for the following:
• Writing efficient algorithms
• Writing efficient pseudocode

©NIIT Collaborate Lesson 1C/ Slide 1 of 19


Collaborate

Guidelines for Writing Efficient


Algorithms
• The programmer should decide on the expected output before writing an
algorithm.
• The steps in an algorithm should be written clearly, stating each task to be
performed.
• An algorithm should be checked using sample values. This helps a programmer
to verify that the algorithm produces the expected output.

©NIIT Collaborate Lesson 1C/ Slide 2 of 19


Collaborate

Guidelines for Writing Efficient


Pseudocode
• Combine related statements to avoid writing lengthy pseudocode.
• The pseudocode should be correctly aligned for a clear understanding of the
depicted steps.

©NIIT Collaborate Lesson 1C/ Slide 3 of 19


Collaborate

Challenge
Problem Statement 1
• Detect the error in the flowchart
that represents the process of answering an
incoming telephone call either directly or by
using voicemail. In addition, draw the
correct flowchart.

©NIIT Collaborate Lesson 1C/ Slide 4 of 19


Collaborate

Solution
• A flowchart can have only one Stop symbol representing the end of a process. If a
step leads to the end in the middle of a process, it can be connected to the end of
the process using flow lines.
 

©NIIT Collaborate Lesson 1C/ Slide 5 of 19


Collaborate

Solution (Contd.)

The correct flowchart is as follows:

©NIIT Collaborate Lesson 1C/ Slide 6 of 19


Collaborate

Problem Statement 2
• Vaccination camps need to be organized to provide free vaccination to children
below five years of age. To organize these vaccination camps, a population survey
needs to be conducted. This will help the hospital staff to determine the
approximate number of vaccines that should be supplied to the camps. Children
who are below the age of five years, do not have any disease, and are born in
families below the poverty line will be given free vaccinations. Families that have
an annual income of less than $4500 will be assumed to be below the poverty line.

Which technique will you use to represent the algorithm for this problem? Give a
rationale for your choice, and represent the algorithm using the selected technique.

©NIIT Collaborate Lesson 1C/ Slide 7 of 19


Collaborate

Solution
• The algorithm should be represented using a decision table. This is because
decision tables are preferred for cases when the problem consists of multiple
conditions based on which a decision is made.

• If you create a flowchart for this problem, it will have a too many flow lines which
will make the flowchart difficult to understand.

©NIIT Collaborate Lesson 1C/ Slide 8 of 19


Collaborate

Solution (Contd.)

©NIIT Collaborate Lesson 1C/ Slide 9 of 19


Collaborate

Problem Statement 3
• Global Manufacturers Company services and repairs cars and has many workshops
worldwide. When a car is brought for service to a workshop of Global Manufacturers
Company, it is sent to the servicing department. However, if it is damaged and is
brought for repairs, it is sent to the repairing department. After completing the
servicing or repairing of the car, a supervisor inspects it and the car is returned to
the customer.

©NIIT Collaborate Lesson 1C/ Slide 10 of 19


Collaborate

Problem Statement 3 (Contd.)


Complete the given flowchart to represent
the algorithm for the given problem.

©NIIT Collaborate Lesson 1C/ Slide 11 of 19


Collaborate

Solution

©NIIT Collaborate Lesson 1C/ Slide 12 of 19


Collaborate

Problem Statement 4
• The management of Heritage Company decides to provide House Rent Allowance
(HRA) to its employees. The criteria for calculating the HRA amount is:
• If the basic salary of an employee is greater than $10,000, the HRA given to
the employee will be 30 percent of the basic salary.
• If the basic salary of an employee is equal to or less than $10,000, the HRA
given to the employee will be 20 percent of the basic salary.

Complete the algorithm given below for the problem.


• Step 1: Accept the employee’s basic salary
• Step 2: If the employee’s basic salary is greater than $10,000,
Calculate the HRA as 30% of the employee’s basic salary
• Step 3: Display the HRA amount

©NIIT Collaborate Lesson 1C/ Slide 13 of 19


Collaborate

Solution
• The complete algorithm is:
• Step 1: Start
• Step 2: Accept the employee’s basic salary
• Step 3: If the employee’s basic salary is greater than $10000,
calculate the HRA as 30% of the employee’s basic salary
• Step 4: Otherwise, calculate the HRA as 20% of the employee’s basic salary
• Step 5: Display the HRA amount
• Step 6: Stop

©NIIT Collaborate Lesson 1C/ Slide 14 of 19


Collaborate

Problem Statement 5
• Brilliants University displays the final results of the examinations on their Web site.
To view the results, students need to enter their roll numbers at the Web site. If a
student scores above 50 percent, the result is displayed as Passed, along with the
scores. Otherwise, the result is displayed as Failed. The following pseudocode is
created to represent the algorithm. However, the pseudocode has errors in it.
Correct the pseudocode, and create a flowchart for the algorithm.

begin
display “Enter your roll number”
accept StudentRollNumber
display StudentRollNumber and “Passed”
if student score is less than 50%
display StudentRollNumber and “Failed”
end

©NIIT Collaborate Lesson 1C/ Slide 15 of 19


Collaborate

Solution
The correct pseudocode is:
begin
display “Enter your roll number”
accept StudentRollNumber
read Score from database
if student Score is greater than 50%
begin
display StudentRollNumber, “Passed”
display Score
end
else
begin
display StudentRollNumber, “Failed”
display Score
end
end

©NIIT Collaborate Lesson 1C/ Slide 16 of 19


Collaborate

Solution (Contd.)
• The flowchart is:

©NIIT Collaborate Lesson 1C/ Slide 17 of 19


Collaborate

Problem Statement 6
• A bookstore is offering a discount on books. The books are divided into two
categories, A and B. The discount is only for the books in category A. If a customer
buys a book from category A, a discount of 10 percent is provided.

Complete the pseudocode given below to represent this algorithm:


begin
display “Enter the category of book purchased”
accept the category of book
end

©NIIT Collaborate Lesson 1C/ Slide 18 of 19


Collaborate

Solution
• The complete pseudocode is:

begin
  display “Enter the category of book purchased”
accept the category of book
if the category of book is A
display “You get a discount of 10%”
if the category of book is B
display “There is no discount on this book”
end

©NIIT Collaborate Lesson 1C/ Slide 19 of 19

You might also like