Week 2

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

Programming Concept and Algorithms

Algorithm, Flowchart, Pseudocode


Dr. Karzan Hussein sharif
[email protected]
How to perform well

1. Revise your lecture in the same day.


2. Complete given task before next lecture.
3. Consult your teacher or your friend if you are unable
to understand a concept.

2
Three Most Important Things

• Practice

• Practice

• Practice

3
Outlines

• Introduction to Computers
• Programming Languages
• Program translators
• Algorithm
• Flowchart
• Pseudocode
• Sustainable programming: How algorithm efficiency contributes to energy conservation.

4
Introduction to Computers

• Computer: Is a machine
that stores data (numbers,
words, pictures), interacts
with devices (the monitor,
the sound system, the
printer), and executes
programs.

5
Computer programming

• Computer programming is the process of writing instructions that get


executed by computers. The instructions, also known as code, are written in a
programming language which the computer can understand and use to
perform a task or solve a problem

• Computers are processing the data under


the control of a set of instructions called
computer programs, which is designed by
HUMAN called a programmer.
6
Programing Languages

• There are different levels of Programming


languages based on how user friendly or
human friendly are they:

1. Machine Level Programming Language


(late 1940s)

2. Assembly Language (early 1950s)

3. High Level Language (mid 1950s to


present)

7
Programming languages

1. Machine Language: is a set of primitive instructions built into every computer.


The instructions are in the form of binary code (0,1).

• It is very difficult to read and understand.

8
Cont.
2. Assembly languages: are developed to make the programs easier for
human.

• a program called assembler is used to convert the assembly language into a


machine code.

• For example, to add two numbers, you might write

an instruction in assembly code:


ADD R1, R2
MOV A, B 9
Cont.

3. High-level languages allow you to write instructions that look almost like everyday
English and contain commonly used mathematical notations also it is easy to learn and
program.

• For example, the following is a high-level language

statement that computes the area of a circle with radius:


Input r;
Area = r * r * 3.1415
Output (Area)
10
Translator Types: Compiler and Interpreter

11
Popular languages
• FORTRAN (FORmula TRANslation)
• COBOL (COmmon Business Oriented Language)
• Pascal
• Ada => AADL (Architecture Analysis and Design Language)
• BASIC
• C
• C++
• Objective-C
• Visual Basic
• C#
• Java
• Python 12
PROGRAM TRANSLATORS

✓ Assembler
✓ Compiler
✓ Interpreter

13
PROGRAM TRANSLATORS
• A compiler is a program translator that translates a program written
in high-level language into machine language program.

• Interpreter: Translates high-level language into machine language.


• Unlike compilers, it translates a statement in a program and execute
immediately, i.e., before translating the next source language
statement.
• An assembler converts the assembly code into binary code.

14
15
Differences between Compiler and Interpreter

Basis Compiler Interpreter


Object Code A compiler provides a separate object An interpreter does not generate a permanent object
program. code file.
Translation Process Converts the entire program into machine code Translates the source code line-wise.
at one go.
Debugging Ease Removal of errors (debugging) is slow. Debugging becomes easier because the errors are
pointed out immediately.
Execution Time Compilers are faster as compared to Interpreters are slower as compared to compilers
interpreters because each statement is because each statement is translated every time it is
translated only once and saved in object file, executed from the source program.
which can be executed anytime without
translating again.

16
Program

• Sequence of instruction written in a programming language.


• Generally every program takes an input, manipulates it and
provides an output.

input Program code output

17
Just Writing Code Is Not Sufficient To Solve A Problem.

Program Must Be Planned Before Coding In Any Computer


Language Available.

18
Planning the computer program

• Problem solving is one of the most significant advantages of a


computer. Before writing programs, it is a good practice to
understand
✓the complete problem
✓Analyze the various solution
✓Arrive at the best solution. Identify
the Analyses the
Select the
best
problem solution solution

19
Stages of solving a problem

• There are many steps to be done before and after writing code:
1. Problem Analysis
2. Program Coding (Implementation)
3. Testing
4. Documentation

20
Problem solving steps (Problem Analysis)

1. Identify the problem


2. Understand the problem
3. Identify alternative ways to solve the problem
4. Select the best way to solve the problem from the list of alternative solution
5. List instructions that enable you to solve the problem using the selected
solution
6. Evaluate the solution

21
Different methods for solving problem

1. Algorithm
2. Flowchart
3. Pseudocode

22
Algorithm

• A procedure for solving a problem in term of


1. The action to execute
2. The order in which these action execute

Is called Algorithm

• It is an effective procedure for solving a problem in finite number


of steps.

23
Algorithm requirements
• Finiteness: Must terminate in a limited number of steps.
• Definiteness: Each step must be clearly defined.
• Input: Accepts one or more inputs.
• Output: Produces at least one output.
• Effectiveness: Simple, basic operations that can be performed
efficiently.
• Generality: Should solve a general class of problems, not just one
specific case.
24
Algorithm Example 1

Algorithm for sum of two numbers

Step 1: Input N1,N2

Step 2: Sum N1+N2

Step 3: Print “Sum”

25
Algorithm Example 2
• Write an Algorithm to determine a student’s final grade and indicate
whether it is passing or failing. The final grade is calculated as the
average of four marks.

Step 1: Input M1,M2,M3,M4


Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE < 50) then
Print “FAIL”
else
Print “PASS”
endif
26
Algorithm Example 3
An algorithm to find largest of three different numbers:
Step 1: input A, B, C
Step 2: if (A > B) then
if (A > C) Then
print “Biggest number is”, A
Else
print “Biggest number is”, C
Else
if (B > C) then
print “Biggest number is”, B
Else
print “Biggest number is”, C
Step 3: End

27
Algorithm Example 4

• Exchanging values of two variables


Step 1: Start
Step 2: Input A, B
Step 3: Temp A
A B
B Temp
Step 4: Write “ Exchanged values are” , A, B
Step 5: End

28
Algorithm Example 5
• Write an algorithm and draw its flowchart to convert temperature in
Fahrenheit to Celcius.

• Step 1: Input F

• Step 2: C  5/9*(F-32)

• Step 3: Print C
29
Flowchart

• Diagrammatic representation of algorithm


• Solving a problem using figures
• Different figures having different functions

30
Flowchart symbols

31
Rules for Drawing a Flowchart

• It should contain only one starter and one end symbol.


• The direction of arrows should be top to bottom and left to right.
• It should be simple and drawn clearly and neatly.
• The branches of decision box must be labeled.
Flowchart Example 1
• Flowchart sum of two numbers

33
Flowchart Example 2
• Flowchart to check whether the input number is even or odd

34
Flowchart Example 3

• Convert
temperature from
Fahrenheit to
Celsius

35
Flowchart Example 4

• Calculate the
factorial of a number

36
Pseudocode

• Pseudocode is a textual representation of an algorithm that


mimics the structure of real code but without adhering to the
strict syntax of programming languages.

• Purpose: It is a bridge between the logic of an algorithm (or


flowchart) and actual programming code. Pseudocode is
easy to understand but much closer to real code than a
flowchart.
37
Pseudocode Why Last?

• Pseudocode translates the logic from an algorithm or


flowchart into something that can be easily converted
into actual programming code.

• It is generally introduced last because students need to


understand the logic (algorithm) and flow (flowchart)
before learning how to express that logic in a more
code-like form.
38
Pseudocode Example 1
• pseudocode sum of two numbers

Declare number1, number2, sum as variables


Iutput: “enter the first number”
set number1 = ??
ask user: “enter the second number:”
set sum = number1 + number2
output: “the sum is:”
output: sum

39
Pseudocode Example 2
• Write an Pseudocode to determine a student’s final grade and
indicate whether it is passing or failing. The final grade is
calculated as the average of four marks.
▪ Input a set of 4 marks
▪ Calculate their average by summing and dividing by 4
▪ if average is below 50
Print “FAIL”
else
Print “PASS”
40
Pseudocode Example 3
• pseudocode to check whether the input number is even or odd

1. START
2. DISPLAY "Enter the Number - “
3. READ number
4. IF number MOD 2 = 0 THEN
DISPLAY "Number is Even"
ELSE
DISPLAY "Number is Odd"
END IF
5. STOP
41
Sustainable programming
• How algorithm efficiency contributes to energy conservation?
• Algorithm efficiency plays a crucial role in sustainable programming and energy
conservation in computing.

• Efficient algorithms help reduce the amount of computational power, memory,


and resources required to solve a problem, which in turn leads to lower energy
consumption.
✓ Reduced CPU Usage
✓ Optimized Memory Usage
✓ Faster Execution Time
✓ Efficient algorithms reduce unnecessary I/O operations, lowering energy use and extending hardware life.

42
Activity: Write an algorithm and Flowchart to
• Convert the length in feet to centimeter.
• Read the two sides of a rectangle and calculate its area.
• Read two values, determines the largest value and prints the largest value with an identifying
message.
• Read three numbers and prints the value of the largest number
• Determine if a given positive integer is even or odd.
• Read two positive integers, determine which has greater value and then print this value if it is even.
• Convert days into months and days
• Find the sum of first 50 natural numbers.
• Read a number N and print all its divisors.
• Find the sum of given N numbers.
• Computer the sum of squares of integers from 1 to 50

43
Any Questions

44

You might also like