Week 2
Week 2
Week 2
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
7
Programming languages
8
Cont.
2. Assembly languages: are developed to make the programs easier for
human.
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.
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.
14
15
Differences between Compiler and Interpreter
16
Program
17
Just Writing Code Is Not Sufficient To Solve A Problem.
18
Planning the computer program
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)
21
Different methods for solving problem
1. Algorithm
2. Flowchart
3. Pseudocode
22
Algorithm
Is called Algorithm
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
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.
27
Algorithm Example 4
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
30
Flowchart symbols
31
Rules for Drawing a Flowchart
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
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.
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