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

EE1005 L01 Computers & Programming

This document provides an overview of computers and programming. It discusses what a computer is, its main hardware components like the CPU, memory, and storage devices. It also explains system software like operating systems and applications. Programming is introduced as providing instructions to computers using programming languages. The key steps in programming are problem solving, coding, testing and maintenance. Algorithms are presented as a set of steps to solve a problem, often written in pseudocode initially.

Uploaded by

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

EE1005 L01 Computers & Programming

This document provides an overview of computers and programming. It discusses what a computer is, its main hardware components like the CPU, memory, and storage devices. It also explains system software like operating systems and applications. Programming is introduced as providing instructions to computers using programming languages. The key steps in programming are problem solving, coding, testing and maintenance. Algorithms are presented as a set of steps to solve a problem, often written in pseudocode initially.

Uploaded by

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

EE1005 From Computational

Thinking to Programming

Lesson 1
Computers & Programming
(Reference: Harry H. Cheng, Chapter 1)

Dr. Tan Chee Wah, Wesley (CoPaCE/School of EEE)


Email: [email protected]
Office: S1-B1b-54
Phone: 6790 6009
1-1
“Be Prepared, Give Feedback”
What is a Computer?

Instructions &
Data Storage
in Memory

Input Processing Output

Keyword: programmable

1-2
Computer Systems

Two main components:

1. Hardware: monitor,
keyboard, etc.

2. Software: Collections of
instructions for the
Personal Computer (PC)
computer to execute
(programs).

1-3
Hardware Components
• Main Memory (Primary Storage)
- on Main Board

• The Central Processing Unit (CPU)


- on Main Board

• Secondary Storage (hard disks, thumb drives)


- outside of Main Board

• Input Devices

• Output Devices

http://www.youtube.com/watch?v=S6-5FCv7-_g 1-4
Main Memory (1)
To store
• instructions to be executed,
• data to be manipulated,
• processing results.

Information is stored in bits--short for


binary digits (0 or 1, off or on).
These bits are organized into groups of 8 bits
(1 byte) called memory cells.

1-5
Main Memory (2)
Think of main memory as a collection of boxes
(cells). Each cell can hold 1 byte of data and
has a unique address (an integer value).
Address Contents

100000
‘Logical’ Cell
100001 3.14159 Size is
100002 greater than
100003 1 byte
100004 H
Cell Size of
100005 I 1 byte each
1-6
Main Memory (3)
As each memory cell can be accessed directly,
main memory is also known as Random
Access Memory (RAM).

The size is measured in kilobytes (KB)


megabytes (MB), or gigabytes (GB):

1 KB = 210 bytes = 1024 bytes


1 MB = 220 bytes = 1024 KB
1 GB = 230 bytes = 1024 MB

* Note: the above conversion only applies


when referring to memory size 1-7
Main Memory (4)
RAM is high-speed memory for storing
instructions and data temporarily.

However, the most common type of RAM has


two disadvantages:
1. It is volatile (i.e. it loses all data when
power is cut off).
2. It is expensive.

We need cheap and permanent storage:


disks, tapes, etc.

1-8
Read-Only Memory
ROM (Read-Only Memory) stores data
permanently. Usually, they can only be read from
but not written to. The data are physically
encoded in the circuit, so they cannot be modified
easily, if at all.

Usually used to store boot-up (start-up)


instructions, i.e., the initial instructions that run
when the computer is powered on.
It is also used for storing other
critical system instructions.

1-9
CPU Components

Registers M

Data, e
Control Address, m
Unit Control
Buses o
r
ALU
y

1-10
ALU
The ALU (Arithmetic Logic Unit) is a
fundamental component of the CPU, and it
can perform:

• Arithmetic operations (+, -, x, ÷)

• Logical operations (COMPARE, AND, OR,


NOT, etc.)

1-11
Registers & Control Unit

Control Unit:
It controls flow of instructions and data from
and to memory, and inside the CPU. It tells
the ALU what operation to perform on the
data.

Registers:
These are memory cells in the CPU that allow
very rapid access of instructions and data by
the ALU and the Control Unit.

1-12
Secondary Storage & I/O Devices
For cheap and permanent storage of data.
Examples:
• Hard disks
• Flash drives
• CD-R, CD-RW, DVD, DVD-RW
• Magnetic tapes

Input Devices: keyboard, mouse, microphone

Output Devices: monitor, printer, speaker

1-13
Flash Memory

Flash memory is a type of non-volatile


memory that can be erased and
reprogrammed.

It is used in memory cards (e.g. for digital


cameras and cell phones) and USB flash
drives (thumb drive, pen drive, USB stick, …)
for storage and transfer of data between
digital devices.

1-14
Interaction Between CPU,
RAM and Hard Disk
RAM
2a.One instruction and associated
3a.Final results are
data are fetched from RAM to
either shown on
CPU at any one time
screen or saved
CPU B to Hard Disk
3b.Program and data
U released to Hard
2b.ALU computes and stores
intermediate results in S Disk when
terminated
registers in CPU
2c.Final results are
transferred back to 1. Program and data
RAM transferred to RAM

Example: Executing a
Hard Disk
program on a data file 1-15
System Software
Two major types of software: (i) System
Software; (ii) Application Software.

System Software sub-categories:


1. Operating Systems (OS)
Manage resources of a computer. Most
important software.
2. Utility Programs
e.g. programs to format disks, compress
data, etc.
3. Software Development Tools
e.g. compilers, linkers, etc.
1-16
Operating System (OS)
• Loaded into RAM when computer is started (a
process known as booting the computer).
• Controls access to computer
• Enforces security and privacy of files
• Allocates/manages memory, CPU resources, etc.
• An OS shields the user from the complexity of
computer hardware.
• Examples: Windows 2000/2003/XP/Vista/7/8/10,
UNIX (Linux), Mac OS X

1-17
Application Software
Software that are very useful to most users.
Examples are:

• Word Processors (Microsoft Word)


• Spreadsheets (Excel)
• Graphics (Photoshop)
• World Wide Web (WWW) Browsers
(Internet Explorer, Mozilla Firefox, Opera,
Safari, Google Chrome)
• Computer-Aided Design (CAD) (AutoCAD)

1-18
What is Programming?

The computer needs detailed and exact


instructions to carry out the steps needed to
solve a problem.

These instructions must be coded (i.e. written)


using a programming language and they
form a computer program.

1-19
Programming Languages
A programming language is an artificial
language with a set of special words
(keywords) and a set of rules (syntax or
grammar).

Two main categories:


(1) Low-level languages
Machine language, Assembly language
(2) High-level languages
C, C++, Java

1-20
Machine Language
Each CPU has a set of instructions designed
and manufactured into it.
These machine instructions are different for
CPUs from different chip design companies.
They consist of sequences of 0s and 1s.

The only language "understood" by the


computer. Machine language statements look
like:
100100 0000 010001
100110 0000 010010
100010 0000 010011
1-21
Difficult to use for humans!
High-Level Languages
Most popular because

• They use English words (such as PRINT,


READ, WRITE, etc.) Need a translator
(compiler) to convert the statements into
machine language.
• The programs are portable. High-level
languages are not tied to any particular CPU
in any machine.
• Programs are much easier to read,
understand and modify for humans!

1-22
High-Level Languages (Examples)
There are numerous high-level languages.
Common ones are:

• FORTRAN (Formula Translation)


• C, C++
• Java
• C# (C Sharp)
• Perl
• Python

1-23
Language Standard
Each major high-level language has a
language standard that describes its syntax
(grammar).

The syntax rules are very strict. Deviations


will result in syntax errors.

1-24
The Programming Process
1. Problem Solving:
(a) Problem Specification
(b) Problem Analysis
(c) Algorithmic Design (Solution Procedures)

2. Implementation
(a) Coding
(b) Testing
(c) Maintenance

1-25
Algorithm
An algorithm is a set of precisely stated, finite
sequence of executable steps for solving a
problem.

Finding a suitable algorithm is often the most


difficult part of the problem solving process.

An algorithm is usually written using informal


English-like statements known as pseudocode*.
Another representation is using a graphical
flowchart (not covered here).

*
1-26
You will learn more in EE2008 DATA STRUCTURES
& ALGORITHMS
Pseudocode Example 1

To calculate the area of a circle, given its radius:

INPUT radius
circle_area=3.14159*radius*radius
OUTPUT circle_area

Note: We can use words like GET or READ, etc.


instead of INPUT; similarly, we can also use
WRITE, PRINT, etc. for OUTPUT.

1-27
Pseudocode Example 2a

To find the larger of two user-input numbers,


number1 and number2:

INPUT number1, number2


If number1 is greater,
IF number1 > number2 THEN max assumes value of
SET max = number1 number1
ELSE
SET max = number2 Otherwise, max
assumes value
END IF of number2
OUTPUT max

1-28
Pseudocode Example 2b

The following is an alternative (and better)


form for finding the larger of two user-input
numbers.

INPUT number1, number2 Set number1 as the


SET max = number1 maximum value first

IF number2 > max THEN


If number2 is greater,
SET max = number2 then replace number1
END IF as the maximum value

OUTPUT max

1-29
Pseudocode Example 3
To calculate and display the distances travelled by a
particle falling under gravity from an initial time 0 to a
given point in time. Assume zero initial velocity.

Formula is s=1/2*a*t*t, where a = acceleration, t = time


s = distance travelled

The results are to be displayed in a table form:

Time Elapsed Distance Travelled


Table Title
----------------------------------
0.00 0.00
0.50 1.23
1.00 4.90
1.50 11.03
2.00 19.60 1-30
max_time = 2.00, interval = 0.50, initial time = 0
Pseudocode Example 3
INPUT max_time, interval
SET accel = 9.8, time = 0
PRINT Table Title Compute and
display as long as
WHILE time <= max_time time ≤ max_time

Compute and
distance = 0.5*accel*time*time
display time and
associated PRINT time, distance
distance travelled

SET time = time + interval

Increment time, for computation and display at other time values


1-31
END WHILE
Edit, Compile & Link Cycle
1. Create the source program (simple.c) by
typing program statements using a
program editor.
2. Use a compiler to translate the source
program into machine language. This
produces an object file (simple.o) if there
are no syntax errors. If there are errors, we
need to go back to the editor to fix these
errors.
3. Use a linker to link this object file with
other necessary components (i.e. libraries)
to get a stand-alone executable file
(simple.exe). 1-32
Edit, Compile & Link Cycle
These steps are usually carried out within an
IDE (Integrated Development Environment).

An IDE typically contains an editor, a compiler,


a linker, a debugger (for helping to find and
correct programming errors), and other
components.

We will use CodeBlocks in this course.

1-33
Errors (Bugs) in Programs
A bug is an error in software/hardware.
Correcting a bug is known as debugging. The
following types of errors may appear in our
programs:

• Syntax Errors: errors in grammar


• Logical Errors: errors due to the use of
wrong algorithm, wrong formula, etc.
• Runtime Errors: errors that occur when a
program is being executed. An example is
division by a variable value which turns out to
be zero.
* Compiler can only help you catch
1-34

Syntax Errors
Summary
1. Computer hardware components
• Main memory, CPU, secondary storage

2. Computer software
• System and Application software
• Programming Language: Low Level and
High Level
• Algorithm and Pseudocode
• Edit, Compile and Link Cycle
• Errors (bugs)

1-35

You might also like