Unit I Program Logic Development

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

Unit I

Program Logic Development


04 Hours
08 Marks
Industry identified competency addressed by this course:
- Develop C programs to solve broad-based computer related problems.

Course Outcome addressed by Unit:


- Develop flowchart and algorithm to solve problem logically.

Major Learning Outcomes (Cognitive Domain):


1a. Write pseudo program logic for the given problem.
1b. Identify given symbols of a flowchart.
1c. Explain guidelines for preparing flowchart with example.
1d. Create flowchart to logically solve the given problem.

Affective Domain Outcomes:


- Follow safety practices.
- Practice energy conservation.
- Follow ethical practices.

Topics and Subtopics:


1.1.Fundamentals of algorithm – Notion of an algorithm, Pseudo-code conventions like
assignment statements and basic control structures
1.2.Algorithmic Problems – Develop fundamental algorithms to solve simple problems
such as i) solve simple arithmetic expressions, ii) find greatest of three numbers, iii)
determine whether given number is even or odd, iv) determine whether given number
is prime or not
1.3.Flowchart – flowchart, symbols of flowchart, guidelines for preparing flowchart

Suggested specification table:


Distribution of Theory Marks
Remember Level Understand Level Apply and Above Level Total Marks
02 08 04 08
This specification table provides general guidelines to assist students for their learning
and to teachers to teach and assess students with respect to attainment of Learning
Outcomes (LOs). The actual distribution of marks at different taxonomy levels (R, U and
A) in the question paper may vary from above table.

1.0 Introduction

We have heard the words hardware and software related to computer


system or mobile phones. Hardware consists of various electronic parts and their
interfacing. In other words, it is physical part of computer system. On the
contrary, software consists of set of instructions/commands and programs.
Software makes hardware to operate properly. Software is a logical part of
computer system.

1 of 10
A program is a sequence of instructions for performing a specific task.
Process of developing a program involves finding a solution to a complex
problem. To do so, there is a need of tools which enable programmer to represent
logic required in the program. Before solving any given problem we have to first
think about logical steps for finding one of the solutions for that problem. These
logical steps can be represented using different ways as
- Algorithm
- Flowchart

1.1 Algorithm

The word ‘algorithm’ is derived from phonetic pronunciation of last name


of an Arabic mathematician named Abu Jafar Mohammed ibn Musa Al-
Khowarizimi.
This is a very popular technique used to obtain a solution for given
problem. Description of steps for solving a given problem is provided in
algorithm. Stress is given on text.
Definitions
- A sequential solution to any problem written in human language is
called as algorithm.
- A process that should be followed for solving a specific problem is called
algorithm.
- A step by step problem solving procedure for solving a problem in a
finite number of steps is called algorithm.
- The algorithm is defined as the finite set of steps, which provide a
chain of actions for solving a definite nature of problem.

An algorithm is said to be accurate and truthful only when it provides the


exact required output. We may use imperative statements, conditional
statements and iterative statements while designing algorithms.

Example 1: Making a milk tea.


Algorithm MAKING_TEA
1. START
2. WASH UTENSIL
3. WASH CUP
4. FILL THE CUP WITH WATER (HALF CUP)
5. TURN-ON THE STOVE/BURNER
6. ADJUST IT TO MEDIUM FLAME
7. PLACE UTENSIL ON THE BURNER
8. POUR THE WATER FROM THE CUP IN THE UTENSIL
9. OPEN SUGAR CONTAINER
10. PUT 2 SPOONS OF SUGAR IN THE BOILING WATER
11. TAKE CARDAMOM
12. GRIND IT IN A MIXER/GRINDER
13. TAKE A PINCH OF IT AND PUT IT IN THE VESSEL ALONG
WITH BOILING WATER AND SUGAR
14. LET THE WATER BOIL FOR A WHILE(~3 MINUTES)
15. MEANWHILE:
2 of 10
a. WASH THE SIEVE/DRAINER
b. TAKE OUT MILK FROM REFRIGERATOR
c. POUR MILK INTO THE CUP (3/4 TH CUP)
16. POUR THE MILK INTO THE BOILING WATER
17. LET THE MILK AND WATER BOIL (~4 MINUTES)
18. OPEN THE CONTAINER OF TEA POWDER.
19. ADD ONE SPOON OF TEA POWDER IN THE VESSEL OF
BOILING MILK AND WATER WITH DISSOLVED SUGAR &
CARDAMOM
20. LET IT BOIL FOR FEW MORE MINUTES
21. REDUCE THE BURNER TO LOW FLAME
22. CHANGE/MAKE IT TO HIGH FLAME
23. REPEAT STEPS 21-22, 2-3 TIMES
24. TURN THE BURNER OFF
25. TAKE THE SIEVE AND THE CUP
26. POUR THE TEA THROUGH THE SIEVE INTO THE CUP
27. TEA IS READY TO SERVE.
28. END

Algorithms of some realistic problems are discussed in section 1.2.

1.2 Algorithmic problems

Example 1: Finding multiplication (product) of two numbers. (Imperative


constructs)
Algorithm MULTIPLICATION
1. START
2. INPUT first number into variable A
3. INPUT second number into variable B
4. COMPUTE C = A * B
5. DISPLAY C
6. END

Example 2: Finding whether number is even or odd. (Conditional


constructs)
Algorithm EVEN_ODD
1. START
2. INPUT a number into variable A
3. IF A MOD 2 = 0
DISPLAY Number is Even
ELSE
DISPLAY Number is Odd
END IF
4. END

Example 3: Displaying first n natural numbers. (Iterative constructs)


Algorithm NATURAL
1. START
2. INPUT N
3 of 10
3. I = 1
4. REPEAT steps a and b WHILE I <= N
a) DISPLAY I
b) I = I + 1
END WHILE
5. END

Example 4: Finding greatest of three numbers


Algorithm GREATEST
1. START
2. INPUT first number into variable A
3. INPUT second number into variable B
4. INPUT third number into variable C
5. IF A > B AND A > C
DISPLAY A is greatest
END IF
6. IF B > A AND B > C
DISPLAY B is greatest
END IF
7. IF C > A AND C > B
DISPLAY C is greatest
END IF
8. END

Example 5: Determining whether given number is prime or not


Algorithm PRIME
1. START
2. INPUT N
3. I = 2
4. REPEAT steps a and b WHILE I < N
a) IF N MOD I = 0
DISPLAY N is not prime
GOTO step 6
END IF
b) I = I + 1
END WHILE
5. DISPLAY N is prime
6. END

1.3 Flowchart

Flowchart represents the solution of a given problem graphically. Pictorial


representation of the logical steps is a flowchart. It is a diagrammatic
representation that shows flow of execution of a program.
Each step in the process is represented by a different symbol and contains
a short description of the process step. Standard symbols used for drawing
flowcharts are shown here.

4 of 10
• Start / End (Terminal symbol)

• Input / Output box

• Process box

• Decision box

• Connector

• Refers to separate flowchart

• Manual Operation

• Arrows represent direction of flow of control

• Off page connector

5 of 10
Some sample flowcharts are given here.

Example 1: Finding multiplication (product) of two numbers. (Imperative


constructs)

START

INPUT A

INPUT B

C=A*B

DISPLAY C

END

Example 2: Finding whether number is even or odd. (Conditional


constructs)

START

INPUT A

IF
Yes A MOD 2 No
=0

DISPLAY Number is Even DISPLAY Number is Odd

END

6 of 10
Example 3: Displaying first n natural numbers. (Iterative constructs)

START

INPUT N

I=1

IF No
I <= N

Yes
DISPLAY I

I=I+1

END

7 of 10
Example 4: Finding greatest of three numbers

START

INPUT A

IF
A > B AND Yes
A>C

No
DISPLAY A is greatest

IF
B > A AND Yes
B>C

No
DISPLAY B is greatest

IF
C > A AND Yes
C>B

No
DISPLAY C is greatest

END

8 of 10
Example 5: Determining whether given number is prime or not

START

INPUT N

I=2

IF No
I<N

Yes

IF
Yes
N MOD I
=0
No

I=I+1

DISPLAY N is not prime DISPLAY N is prime

END

1.4 Basic General Concepts (Content beyond Curriculum)

As computer is an electronic machine, it understands language of 0’s and


1’s only (i.e. Binary machine code). This language is called as machine
language or low-level language. It is very difficult (or we can say highly
impossible task) to write a program in machine language.
Symbolic representation of machine code can be used for writing
programs. This kind of programming language is called as assembly language.
Program written in assembly language is not understandable by a computer. So,
it is needed to be translated to machine language program. Conversion of an
assembly language program into a machine language program is done by system
software called assembler. Original assembly language program is known as
source code and the generated machine language program is called as object
code. For loading the object code and starting its execution, loader (it is also a
system software) is used. There are different assembly languages for different
processors. i.e. The programs written in assembly language are machine
dependent (cannot be easily run on other machines).
9 of 10
High level languages are nearer to human languages. So, they are
easily understandable. Programs written in high level language are not machine
dependent. But these programs are also not understandable by a computer.
There are two ways to make it understandable by computer.
With some programming languages, approach of Compiler is used.
Compiler is system software which translates high level language program
(source code) into a machine language program (object code). Then loader is
responsible for loading the object code and initiating execution. C uses this
approach.
The other approach is Interpreter. Interpreter is system software which
checks a single line of high level language program, executes it by converting to
machine level instruction. The process is repeated line by line.

A program is a sequence of instructions for performing a specific task. A


person who develops program is called programmer and the process of
developing programs is called as programming.
Every programming language has its own syntax which is to be studied by
a learner. Syntax is the set of rules that defines combination of symbols that are
considered to be correctly structured fragment in that language. It can be
considered to be similar to grammar in the human languages.
Before converting a source program into object program, compiler checks
for syntax errors. If the program contains such syntax errors, those are shown
to the programmer. These are the errors in the syntax of the programming
language.
Sometimes there is another type of errors in the program. Those are called
as logical errors or bugs. These errors are not shown by compiler. When we
develop a program that works, but it does not do what was expected from it.
These errors can be removed with the help of debugging (i.e. removing bugs).

Sample Questions

1. Define algorithm. [2M]


2. Write an algorithm to determine whether given number is divisible by 5 or
not. [4M]
3. Write algorithm to determine the given number is even or odd. [4M]
4. State importance of flowchart. [4M]
5. Draw and label different symbols used in flowchart. [2M]
6. State use of following symbols used for flowchart drawing. [2M]
i) ii)
iii) iv)
7. Draw a flowchart for checking whether given number is prime or not. [4M]
8. Draw flowchart for checking whether given number is even or odd. [2M]
9. Draw flowchart for finding largest number among three numbers. [4M]
10. Draw flowchart for addition of two numbers. [2M]
11. Write algorithm and draw flowchart to print even numbers from 1 to 100.
[4M]

10 of 10

You might also like