0% found this document useful (0 votes)
11 views

COMP1117_01 Introduction

The document provides an overview of computer programming, emphasizing its role in problem-solving and the creation of applications. It discusses various programming languages, the hardware components of computers, and the process of writing and executing programs. Additionally, it introduces Python as a programming language and highlights the importance of Integrated Development Environments (IDEs) for coding and debugging.

Uploaded by

eksecondac
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

COMP1117_01 Introduction

The document provides an overview of computer programming, emphasizing its role in problem-solving and the creation of applications. It discusses various programming languages, the hardware components of computers, and the process of writing and executing programs. Additionally, it introduces Python as a programming language and highlights the importance of Integrated Development Environments (IDEs) for coding and debugging.

Uploaded by

eksecondac
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Introduction

COMP1117B Computer Programming

1
Programming in Daily Life

A lift contains thousands


of components / devices.
For the lift to function
normally and safely, a
control program is Lift system simulator
needed. for program testing
purpose

2
Programming in Daily Life
Some MTR lines in Hong
Kong rely on auto-
driving. To avoid
collision or accidents, an
excellent control
program is required.

3
What is Computer Programming?
Teaching the computer to do something for you by providing a set
of instructions.

Computer Programming is NOT just about writing codes…

It is about problem solving


• the study of using computer technologies to solve real-life problems.

4
What is an Application?
Application software, also known as application or app, is computer
software which is designed to help the user to perform specific tasks.

Online booking application

Smartcard payment application Electronic wallet 5


What is an Application?
Application software, also known as application or app, is computer
software which is designed to help the user to perform specific tasks.

Estimated Time of Arrival (ETA) 6


Applications Everywhere
Internet and WWW applications
Electronic commerce applications
Cybersecurity applications
Wireless and mobile applications
Database applications
Computer games
Multimedia applications
Computer Vision, Computer Graphics
Artificial intelligence applications

and many more …


http://www.cs.hku.hk/programme/courses-offered.jsp
7
Evolving and Emerging
Technologies
Artificial learning / machine learning
IoT, mobile and wearable devices
Self driving car / driverless car
Cloud technologies
3D printing technologies
Blockchains
Virtual/Augmented/Mixed/Extended Realities

8
Programming Empowerment

Programming skills give you the ability


to create and innovate.

9
How Computers Work

10
Pre-Class Videos
Covers the very basic concepts on
– binary & data
- circuits & logic
- CPU, memory, input & output
- hardware & software
https://youtube.com/playlist?list=PLzdnOPI1iJNcsRwJhvksEo1tJqjIqWbN-&si=_PBBzfIVS_ox5WoB

11
Basic Concepts
Note that “computer” is not only referring to your personal
computer (PC), it can be…
Computer == Any device
• The computer inside your mobile phone. with computing capabilities
• The computer inside your camera.
• The computer in your video game console.

Question: What are the


common components of
these “computers”?
12
Hardware
The actual physical parts that make up a computer.

1. Central Processing Unit (CPU)


• The “brain” of the computer.
• Follows instructions in a program. A quad-core i7 CPU

• Performs simple tasks like


Question: Do you know how is the CPU
• Addition, subtraction, multiplication and division. speed measured?
• Move data from one memory location to another.
Answer : Clock-rate is the fundamental rate in cycles
per second at which a computer performs its most
basic operations such as adding two numbers.
100MHz = 100 Million cycles per second, 1GHz = 1
Billion cycles per second. 13
Hardware
2. Main memory
• Long list of memory cells.
• Each memory cell has an address (a number)
A DDR3 RAM
that gives its position in the memory. Main Memory
Address 1

• Each memory cell consists of 8 digits (1 byte).


Address 2
Address 3


• Volatile – information will be lost when the
computer is switched off. 0 0 0 0 0 0 0 1

……
Information is represented as
“0”s and “1”s in main memory.
14
Hardware
3. Secondary memory
• Non-volatile – information will not be lost even
after the computer is switched off.
• For permanent storage of data in units of files.
• E.g., hard disks, DVD/CD ROMs, thumb drives

An internal Hard Disk

Flash hard drive External Hard Disk DVD/CD ROMs USB flash drive

15
Hardware
4. Input devices
• Any device that allows a user to communicate
information with the computer.

5. Output devices
• Any device that allows a computer to communicate
information to the user.

16
Computer Program
A computer program is a sequence
Processer CPU Input of instructions written to perform a
device(s) specified task with a computer.
Fast

Output
Main memory device(s)

Slow

Secondary Compute 2+3=?


memory
17
Computer Program
When a Program
is being executed
Processer CPU Input • The program is loaded into the main
device(s)
memory.
Fast

Output
Main memory device(s)
Compute 2+3=?

Slow

Secondary Compute 2+3=?


memory
18
Computer Program
When a Program
is being executed
Processer CPU Input • The program is loaded into the main
device(s)
Compute 2+3=5
memory.
Fast • The CPU reads the program instructions
and process the program data.
Output
Main memory device(s)
Compute 2+3=?

Slow

Secondary
memory
19
Computer Program
When a Program
is being executed
Processer CPU Input • The program is loaded into the main
device(s)
Compute 2+3=5
memory.
Fast • The CPU reads the program instructions
and process the program data.
Output
Main memory device(s) • The output of the program can be
Compute 2+3=?
written to the main memory, secondary
memory, or displayed through the output
Slow device(s).

Secondary
memory
Input Processing Output
20
Computer Program
A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast

Main memory

Compute 2+3=?

Slow

Secondary
memory
21
Computer Program
Registers
A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
• Registers - Store the instructions / data loaded
from main memory for very fast processing.
Main memory

Compute 2+3=?

Slow

Secondary
memory
22
Computer Program
Registers
ALU A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
• Registers - Store the instructions / data loaded
from main memory for very fast processing.
Main memory
• Arithmetic logic unit (ALU) - Performs simple
Compute 2+3=?
arithmetic and logical operations, e.g. AND /OR.

Question: A CPU can only process data and codes represented as “0”s and “1”s, so
Slow how can it execute a program? A program is NOT written using “0”s and “1”s!

Secondary
memory
23
Computer Program
Registers
ALU A closer look into CPU…
• Control unit (CU) - Extracts instructions from
CU
memory and decodes and executes them.
Fast
0011001001001
• Registers - Store the instructions / data loaded
0010101011010 from main memory for very fast processing.
Main memory
• Arithmetic logic unit (ALU) - Performs simple
Compute 2+3=?
arithmetic and logical operations, e.g. AND /OR.

Question: A CPU can only process data and codes represented as “0”s and “1”s, so
Slow how can it execute a program? A program is NOT written using “0”s and “1”s!

Secondary Answer: We need a translation tool to help to translate a program written by


memory you, to the “0”s and “1”s machine language that the CPU understand.
24
Programming Languages
Machine language
• CPU can only understand machine language (machine code).
• Machine codes are well defined instructions and data executed directly
by computer's central processing unit (CPU).

?
Machine Human
language language
0111000100001111 Write a program that computes
1001110110110001 the addition of two integers.
1110000100111110
25
Programming Languages
Assembly language
• A low-level programming language.
• Defined by the hardware manufacturer, so every kind of computer has
its own unique assembly language.
• Need to translate to machine code for CPU to execute. The translation
program is called an assembler.

Machine Assembly Human


language language language
0111000100001111 LOAD A Write a program that computes
1001110110110001 ADD B the addition of two integers.
1110000100111110 STORE C
26
Programming Languages C
Visual
Basic Pascal

C+ + COBOL
High-level programming languages
• Resemble human languages. Python
• Use more complicated instructions than the CPU can follow.
• Need to translate to machine code for CPU to execute. The translation
program is called an interpreter or a compiler.

Machine Assembly Programming Human


language language language language
0111000100001111 LOAD A # Python program… Write a program that computes
1001110110110001 ADD B C = A+B the addition of two integers.
1110000100111110 STORE C
27
Interpreter vs. Compiler
Interpreter Compiler

Scans the entire program and translates it as a whole


Translates program one statement at a time.
into machine code.

It takes large amount of time to analyze the source


It takes less amount of time to analyze the source
code but the overall execution time is comparatively
code but the overall execution time is slower.
faster.

No intermediate object code is generated, hence it is Generates intermediate object code which further
memory efficient. requires linking, hence requires more memory.

Continues translating the program until the first It generates the error message only after scanning
error is met, in which case it stops. the whole program.
Hence debugging is easy. Hence debugging is comparatively hard.

Programming language like Python, Ruby use Programming language like C, C++, Java use
interpreters. compilers. 28
Program Design
Algorithms vs. Actual Coding

29
Let’s start with an example
If you run a 10 kilometer race in 42 minutes 42 seconds, what is your
average pace (i.e., time per km in mins & secs)?

What do you know?


Average pace = 10km race time / 10

Output Input

For this question, the input is a fixed number (42 mins 42 seconds)
If we are going to write a program to display the result on screen, the
output is some output on screen (i.e., standard output, stdout). 30
Solving the Problem
If you run a 10 kilometer race in 42 minutes 42 seconds, what is
your average pace (i.e., time per km in mins & secs)?

Solution 1
1. Convert race time to seconds (42 mins 42 secs --> 42*60 + 42 = 2562 secs)
2. Calculate pace in secs/km (= 2562/10 = 256.2 secs/km)
3. Calculate pace in mins&secs/km (convert 256.2 secs to mins&secs. How?)
4. Display the result on screen

31
Solving the Problem
If you run a 10 kilometer race in 42 minutes 42 seconds, what is
your average pace (i.e., time per km in mins & secs)?

Solution 2
1. Convert race time to minutes (42 mins 42 secs --> 42 + 42/60 = 42.7 mins)
2. Calculate pace in mins/km (= 42.7/10 = 4.27 mins/km)
3. Calculate pace in mins&secs/km (convert 4.27 mins to mins&secs. How?)
4. Display the result on screen

32
Problem Solving Phase
You have just gone through the problem solving phase,
which entails:
1. Understand the problem specification.
2. Identify the input and output of the program.
3. Come up with an algorithm (strategy) that solves the problem.

Note that the solution is not specific to any programming language.


Also, there can be more than one solutions to a problem.
Question: What if you are asked to obtain the race time from the user?
33
Let’s write a program to solve the problem
Suppose we choose to implement
solution 1 with a Python program.
Solution 1 Python implementation
1. Convert race time to seconds pace = 42*60 + 4
(42 mins 42 secs --> 42*60 + 42 = 2562 secs)

2. Calculate pace in secs/km pace = pace / 10


(= 2562/10 = 256.2 secs/km)

3. Calculate pace in mins&secs/km (take


quotient of 256.2 / 60 = 4 mins) pace_min = pace // 60
(take remainder of 256.2 / 60 = 16.2 secs) pace_sec = pace % 60
(answer = 4 mins 16 secs) print("pace = ", pace_min, " min ", pace_sec, " sec")
34
Implementation Phase
You have just completed the program through the
implementation phase:
1. Choose a programming language (e.g., Python) and implement the
algorithm / strategy.
2. Execute the program on sample input.
3. Identify runtime and logic error.
4. Debug until don

You should test your program on different possible inputs (including


boundary cases) to identify potential errors.

35
Summary - Programming Strategy
Problem solving phase
1. Understand the problem specification.
2. Identify the input and output of the program.
3. Come up with an algorithm (strategy) that solves the problem.

Implementation phase
1. Choose a programming language (e.g., Python) and implement the algorithm / strategy.
2. Execute the program on sample input
3. Identify runtime and logic error
4. Debug until done

36
Python Programming

37
Python - A Programming Language
Created by the Dutch programmer Guido van Rossum and first
released in 1991.
You may get Python and related tools via
https://www.python.org/downloads/

Python has two different major versions Python2 and Python3.


And you should use Python3, e.g., Python 3.7.8
Warning: Python 2 and Python 3 have significant differences.

38
IDE
An Integrated Development Environment (IDE) is a
software that integrates the steps of developing a
program, e.g., input program, “run” program and
debug program.
When you download Python, you will also get an
IDE called IDLE (Integrated Development and
Learning Environment):
• IDLE is an integrated development environment
(IDE) for editing and running Python programs.
• It has a number of features to help you develop
your Python programs including powerful Python shell (Interactive mode): this is what
syntax highlighting. you will see if you open IDLE without any
filename.
Icon for the Python program Q4.py:
39
A Python Program
Below is a simple Python program hello.py

40
A Python Program
Below is a simple Python program hello.py

All Python programs should


have the extension “.py”.

41
A Python Program
Below is a simple Python program hello.py

A comment line.
For any line, all characters following the
character # will be ignored by the Python
interpreter.

42
A Python Program
Below is a simple Python program hello.py

Display the string (i.e., the sequence of characters)


between the two quotes on the output window.

43
A Python Program
Below is a simple Python program hello.py

input( ) instructs the computer to read


a string, and we then store it in the
variable “name” so we can refer to this
input string later using this variable.

44
How to run a Python Program?
Two modes: interactive mode vs script mode

Interactive mode
• Input the Python statements in the Python shell directly (without using its
editor)
• After each statement is input, the Python shell will execute the statement and
display its result immediately.
Google Colab, Jupyter Notebook, …
Script mode
• Edit the whole Python program (.py) using the editor of IDLE, and then call the
“Python shell” to execute the program by clicking the “Run” button at the top of
the window.
Visual Studio Code, Atom, …
45
Arithmetic Operators
Python supports the following arithmetic operations:
• addition: +
• subtraction: –
• multiplication: *
• division: /
• floor division: //
• modulus: %
• exponent: **
Under the interactive mode, together with these arithmetic
operators, you have a super-calculator.
46
Arithmetic Operators

Challenge: Find out what "floor division", "modulus", "exponent"


operators mean.
47
More Mathematics
The program is in a .py file and is run by the
Python in script mode.

48
Testing & Debugging

49
Bug & Debug
A mistake in a program is called a bug, and the process of
eliminating bugs is called debugging.

50
3 Kinds of Program Errors
1. Syntax errors
2. Run-time errors
3. Logic errors

51
Syntax Errors
Errors resulting from violation of the syntax (i.e., the grammar
rules) of the programming language

52
Run-time Errors
Errors occur during the execution of a program
Many run-time errors have to do with numeric calculations
E.g., division by zero
The computer cannot
compute (12 /0) in run-time

53
Logic Errors
Errors due to incorrect logic flow
Logic errors are most difficult to locate as they are not reported by
the compiler, but incorrect results may be produced
E.g., using + mistakenly for multiplication
A wrong area is
computed

54
Reading Assignment
Think Python, Chapters 1 & 2

55

You might also like