Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Chapter 1

Collaborate

From the Experts Desk


You have learned in the previous lessons about algorithms and pseudocode. This section
will provide the guidelines for writing efficient algorithms and pseudocode.

Guidelines for Writing Efficient Algorithms


The following are the 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.

Guidelines for Writing Efficient Pseudocode


The following are the guidelines for writing efficient pseudocode:

Combine related statements to avoid writing lengthy pseudocode. For example,


consider the following statements in the pseudocode sample:
display Enter first number
accept number1
display Enter second number
accept number2
display Enter third number
accept number3

These statements make the pseudocode lengthy because they are written on separate
lines. Therefore, the statements can be combined, as given below:
display Enter three numbers
accept number1, number2, number3

The pseudocode should be correctly aligned for a clear understanding of the steps.
For example, consider the following pseudocode snippet:
begin
:
:
:
display Enter your name
accept name
display Enter your age

NIIT

Introduction to Programming Logic and Techniques 1.3

accept age
if age is greater than 18
display Candidate can have a driving license
else
display Candidate cannot have a driving license
:
:
:
end

The statements in the preceding pseudocode are not aligned correctly. To ensure
readability, the statements must be aligned, as shown below:
begin
:
:
:
display Enter your name
accept name
display Enter your age
accept age
if age is greater than 18
display Candidate can have a driving license
else
display Candidate cannot have a driving license
:
:
:
end

1.4 Introduction to Programming Logic and Techniques

NIIT

Challenge
Problem Statement 1
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.
Represent the algorithm of the given problem statement, using pseudocode.

Problem Statement 2
As part of a publicity campaign, Arizona Institute of Geology needs to publish its
students performance results along with the average percentage for the top five students
in an educational magazine. The top five engineering students of the institute scored 99,
97, 96, 95, and 94 percent in their semester examinations. The following pseudocode
partially represents the algorithm for this problem. Complete the pseudocode.
begin
display The average is
average = (nScore1+nScore2+nScore3+nScore4+nScore5)/5
display nAverage
end

Problem Statement 3
A teacher at the RiverDale school conducts a mathematics test for three students: Marc,
Alex, and John. She needs to compare the score of the three students and display the name
of the student scoring the highest score. The following pseudocode partially represents the
logic to solve the problem. Complete this pseudocode.
begin
numeric
display
accept
display
accept
display
NIIT

nScore1, nScore2, nScore3


Enter the score for Marc
nScore1
Enter the score for Alex
nScore2
Enter the score for John
Introduction to Programming Logic and Techniques 1.5

accept
end

nScore3

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 following algorithm for the problem:

Step 1: Accept the employees basic salary

Step 2: If the employees basic salary is greater than $10,000

Step 3: Calculate the HRA as 30% of the employees basic salary

Step 4: Display the HRA amount

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 more than 50 percent, the result is displayed as Passed, along with the marks.
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.
begin
display Enter your roll number
accept StudentRollNumber
display StudentRollNumber and Passed
if student score is less than 50%
display StudentRollNumber and Failed
end

1.6 Introduction to Programming Logic and Techniques

NIIT

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

Introduction to Programming Logic and Techniques 1.7

Home Assignment
1.

In the instruction MULTIPLY A AND B, which of the following option specifies the
opcode?
a. MULTIPLY
b. A
c. AND
d. B

2.

Which of the following construct has a testing statement at the bottom of the iterative
set of statements?
a. ifelseendif
b. dowhile
c. whiledo
d. fornext

3.

Which of the following statement is used to exit from the case after the condition is
met and the case statements are executed?
a. Switch statement
b. Break statement
c. Default statement
d. Execute statement

4.

Which of the following sequence of steps will help you display the sum of two
numbers?
a. 1. Get the First Number
2. Display the Result
3. Stop
4. Add the Two Numbers
5. Get the Second Number
6. Start
b. 1. Get the First Number
2. Display the Result
3. Add the Two Numbers
4. Get the Second Number
5. Stop
6. Start

1.8 Introduction to Programming Logic and Techniques

NIIT

c. 1.
2.
3.
4.
5.
6.
d. 1.
2.
3.
4.
5.
6.

Start
Get the First Number
Get the Second Number
Add the Two Numbers
Display the Result
Stop
Get the First Number
Display the Result
Add the Two Numbers
Stop
Get the Second Number
Start

5.

Which of the following operator is used to check if two numbers are not equal?
a. !=
b.
c. <>
d. NOT

6.

Jim has started writing a pseudocode for finding the factorial of a number. Which of
the following keyword will he use to give additional information about each step in
the pseudocode?
a. //
b. display
c. accept
d. compute

7.

Which of the following conditional execution construct is used for executing the
statements if the condition is true?
a. if construct
b. ifelse construct
c. nested ifelse construct
d. switchcase construct

8.

Which of the following statement is optional in a switchcase construct?


a. switch statement
b. case statement
c. break statement
d. default statement

NIIT

Introduction to Programming Logic and Techniques 1.9

9.

Consider the following statements:


Statement A: Variables can be assigned value after their declaration.
Statements B: Variables can be assigned value anywhere in the program after
declaration.
Which of the following options is true with respect to the above statements?
a. Both, Statement A and Statement B, are False.
b. Both, Statement A and Statement B, are True.
c. Statement A is True and Statement B is False
d. Statement A is False and Statement B is True

10. Which of the following is the INVALID variable name?


a. My.Name
b. My_Name
c. MyName
d. My1Name

1.10 Introduction to Programming Logic and Techniques

NIIT

You might also like