45
BNR 20803 Computer Programming Lecture Note Dr. Mohd Shamian Bin Zainal Dr. Faiz Asraf Bin Saparudin Hj. Hazli Bin Roslan Sem 1, sesi 20212022 FTK 1

BNR 20803 Computer Programming

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

BNR 20803 Computer Programming

Lecture Note Dr. Mohd Shamian Bin ZainalDr. Faiz Asraf Bin SaparudinHj. Hazli Bin Roslan Sem 1, sesi 20212022 FTK

1

Chapter 1 Introduction to computer and programming

No. Title Page

1. Electronic computer 3

2. Introduction to programming 13

3. Program development cycle 25

4. Algorithm design 32

2

1. Electronic Computer

3

What is the computer?

• Computer is a machine that manipulates databased on a list of instructions call program

• Computer consists of hardware and software

• Device capable of performing computations andmaking logical decisions

• Computers process data under the control ofsets of instructions called computer program

• Its physical part of the computer

• Examples: tiny computers, SBCs, PCs, Super computers

Computer

Super computer

4

Task: List out the latest Top 10 Computer Brands in the world

VDSL8
Highlight

Electronic Computer

Totalled 9 billion of microprocessor manufactured for computers, only 2% used for PC’s, the remaining 8.8 billion is for Embedded system

5 Task: Lists out the Examples of embedded systems

VDSL8
Highlight

COMPUTER HARDWARE

• COMPUTER HARDWARE is a physicalpart of a computer.

• Examples of computers : tiny computer single board computer PCs Workstations super computers.

• Typical Computer hardware consist Central processing unit (CPU)

Control unit (CU) Arithmetic and Logic unit (ALU)

Main memory and external memory Input devices Output devices

6 Q: How computer hardware communicate each other?

VDSL8
Highlight

COMPUTER HARDWARE

7

3 1

2

4

1. CPU

8 Task: Sketch and explain the diagram for ALU, Register and CU

2. MemoryMain Memory/Primary memory

• Used to install both program and data

• 2 principles of main memory;

RAM (Random access memory)Can be read/write anytime

Erased when no power

ROM (read-only memory)Preloaded with program and data that never change

Typically used to stored computer initial start-up

In computer, ROM

9

Task: What is DRAM and SRAM in computer?

Double Data Rate Synchronous Dynamic Random-Access Memory is a double data rate synchronous dynamic random-access memory class of memory integrated circuits used in computers.Release date: DDR: 1998; DDR2: 2003; DDR3: 2007; DDR4: 2014; DDR5: 2020

VDSL8
Highlight

External Memory

• Called secondary memory

• Used to store program or data permanently

• Cheaper than main memory and can store more information

• Examples: HDD, USB drives, floppy disk and tape drives

10

2. Memory

3 & 4 . I/O Devices Input and Output Devices

means for a computer to communicates with anoutside world

Input Devices Output Devices

11

12

3 & 4 . I/O Devices

2. Introduction to Programming

13

Human vs machines

14

INTRODUCTION TO PROGRAMMING

• What distinguishes a computer from other machines is its programmability

• Computer software is a general term used to describe a collection of computer program that can be loaded into main memory and executed in the CPU of a computer.

• Software system is essential part of computer

simplest way divide two categories:

OS

application software

Alternatively, divide into

system software

application software

15

Interaction of users and computer hardware through software system 16

OPERATING SYSTEM (OS)

17

OPERATING SYSTEM (OS) • OS is Software program that responsible for management and coordination

of activities and sharing of computer resources

• An interface between hardware and user

• Provides easy and efficient use of system resource

• OS Evolution

Single_user batch processing: only do one task at a time

Early OSs: manage transition between job (minimize transition time between jobs)

Multiprogramming: computer resources are shared by many jobs or task (user still need to wait for their output)

Timesharing (access computer via terminals): computer runs a small portion of one job then move on to service next user

• Examples of OS: MS Windows, Linux OS such Ubuntu, Solaris etc.

• In Embedded system, usually used special purpose dedicated OS where OS and application program are combined to reduce the computer resource requirement

18

OS inside PC • Convert human language to

digital language Input • Human language

Output • Show result/language

human can understand

An interface between hardware and user

Human

19

APPLICATION SOFTWARE

APPLICATION SOFTWARE • Refers to programs developed to assist

user completing specific task.

• Eg. Web browsers such IE can be used to access website on internet. Word processing such MS word for writing docs and CAD/CAM software for design and manufacturing and games for entertainment.

20

PROGRAMMING SOFTWARE

PROGRAMMING SOFTWARE

• Compilers, assemblers and interpreters allow users to develop their application programs.

21

COMPUTER PROGRAMMING Computer programming can be classified into two:

a. High-level language i. High level compiled language ii. High level scripting language

b. Low-level language

22

a. High-level language

High-level language a) High level language are easier to use and most software are written in

high level language. b) High-level language is more portable and easier to use. i. High level compiled language

Is readable English like language Easier to read and write and can be understand by normal user Examples; C, C++ programs Compiler language

ii. High level scripting language

Scripting language can also be called interpreted language Examples: matlab, mathematica The use of code similar to C/C++

23

b. Low-level language

i. Machine language

• Is lowest level of language

• Called machine code

• Contains instruction and data directly executed by CPU

• Each instruction contains 0’s and 1’s.

ii. Assembly language

• Use more meaningful language that machine code

• Higher level than machine language

• Example of code “add”

24

3. Program development cycle

25

A. Analysis and specification of the

task(Problem definition)

B. Design of the software (object and algorithm design)

C. Implementation (coding)

D. Maintenance and evolution of the system

E. Obsolescence (usang)

PROGRAM DEVELOPMENT CYCLE

26

Procedure required to properly develop programming software

A. PROBLEM DEFINITION

a) Analyze and specify about the problem

b) What is the input?

c) What is the output?

d) What are the process involve?

e) How is the output organized?

27

• Reason: to clearly defines and understand the flows of the program that will be design

• Defined as a step-by-step sequence of instruction that must be terminate

• Describes how the data to be processed to produce desired output

• Experience show that this steps will save a lot of times getting the program to run

B. ALGORITHM DESIGN

28

• Translate the Algorithm into a programming language

• This steps usually easier as gain experience with the language

• Compile the written source code Compiler will locates errors in using the programming language

• Run the program on sample data Verify the correctness of results

C. IMPLEMENTATION (CODING)

29

• Usually these step are so called programmed update where some modification of algorithm and coding are required

• Usually, programmer update the feature in this step

• Example : window or utility update

D. MAINTENANCE AND EVOLUTION OF THE SYSTEM

30

• As the written program become ages, it becomes practically unusable or unsustainable to revise it, both economically and practical ability.

E. OBSOLESCENE

31

4. Algorithm design

32

ALGORITHM

• Algorithm originally referred to any computation perform via set of rules applied to numbers written in decimal form.

• Algorithm = step by step procedure to solve a given problem.

• An algorithm is a representation of a solution to a problem.

• Meaning that, if a problem can be define as a difference between a desired situation and the current situation, then a problem solution is a procedure, or method for transforming the current situation to the desired one.

• Preparing solution to current problem will requires little intellectual effort and usually involves in understandable solution or procedure that others can understand.

33

Algorithm Design

• Skip problem define step as the step is considered basic knowledge for engineering student.

• two important steps in algorithm: a) Pseudocode

b) Flowchart

• important! ability to do conversion between both steps.

34

Pseudocode What is pseudocode?

• One of the tools that can be used to write a preliminary plan that can be developed into computer program.

• Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax.

• As its name, it cannot be executed on real computer, but it models and resembles real programming code, and written at roughly the same level of detail.

35

Why use pseudocode?

• In algorithm design, the steps of the algorithm are written in free English text and although brevity is desired, they may be as long as needed to describe the particular operation.

• The steps of an algorithm are said to be written in pseudocode

• So, many languages such Pascal, have syntax that is almost identical to pseudocode and hence make the transition from design to coding extremely easy.

36

Pseudocode form

• Step number is optional

• Sub-procedure is indented to show it is under one of the main procedure

• Use understandable language

Pseudocode general rules!

• Should present step-by-step solution that can be understand by other people.

• Sub-process should be indent from the main procedure/ solution.

• Can include step numbers in the pseudo code to shows the number of main procedure taken in the solution

37

Flowchart

WHAT IS FLOWCHART?

• It is a tool developed in the computer industry, for showing the steps involved in a process.

• A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows.

• Each shape represents a step in the process and the arrows show the order in which they occur.

• In simple words, flowchart is the steps involves to solve the problem

38

Flowchart symbols (most used symbols)

1. Start/End (1) terminal symbol indicates the starting or ending of the program, process or interrupt program

2. Process box (2) in rectangular shape indicates any type of internal operation inside the processor or memory

3. Input / Output (3) box used for any input or output operation. Its indicates that the computer is to obtain data or output results.

4. Decision box (4) use to ask a question that can be answered in a binary format (Yes/No, True/False)

39

General rules of flowchart

• Flowchart should have start/begin and end.

• Every shapes represent specific and must be label accordingly.

• Every shape should be connected using arrows and should have in and out except for start and end.

40

Summary of Chapter 1

• General understanding of computer hardware and software?

• Idea of basic programming language and levels of language available

• Full knowledge in program, development cycle?

• Able to differentiate between pseudocode and flowchart

• Able to solve problem using algorithm method.

41

Practice: Pseudocode/Algorithm

42

Flowchart:

Pseudocode:

Nama saya Ali. Saya tinggal di Parit Raja. Saya ada dua orang kawan namanya Lee dan Muthu. Lee ada tiga batang pensil dan Muthu ada lima batang pensil. Saya meminjam kesemua pensil daripada Lee dan Muthu. Kemudian saya berikan kepada adik saya Aminah separuh daripada pensil yang saya pinjam daripada Lee dan Muthu.

Practice: Pseudocode/Algorithm

43

Flowchart:

Pseudocode: 1. Start 2. Nama saya Ali 3. Saya tinggal di Parit Raja 4. 2 kawan Lee & Muthu 5. Input Lee = 3 6. Input Muthu = 5 7. JUMLAH = Pensil Lee + Pensil Muthu 8. Display “JUMLAH” 9. AMINAH = JUMLAH / 2 10. Display “AMINAH” 11. End

Nama saya Ali. Saya tinggal di Parit Raja. Saya ada dua orang kawan namanya Lee dan Muthu. Lee ada tiga batang pensil dan Muthu ada lima batang pensil. Saya meminjam kesemua pensil daripada Lee dan Muthu. Kemudian saya berikan kepada adik saya Aminah separuh daripada pensil yang saya pinjam daripada Lee dan Muthu.

Start

End

• Nama saya Ali • Saya tinggal di Parit Raja • Ada 2 kawan Lee & Muthu

Input Lee = 3 Input Muthu = 5

JUMLAH = Pensil Lee + Pensil Muthu

AMINAH = JUMLAH / 2

JUMLAH

AMINAH

44

Exercise

1. Create Pseudocode and flowchart that read 2 integer number, then display total of input number

2. Create Pseudocode and flowchart that read 3 integer number, then display the average of those numbers and maximum number

45

Start Read a , b Total = a + b Display Total End

Start

End

Read a & b

Total = a + b

Display Total

Flowchart:

Pseudocode:

Exercise 1