Top Banner
F1001 PROGRAMMING FUNDAMENTALS INTRODUCTION TO PROGRAMMING Hardware And Software Components Steps Of Program Execution The Evolution Of Programming Language Translator And The Types Of Translator 1 UNIT 1
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

INTRODUCTION TO PROGRAMMING

Hardware And Software Components Steps Of Program Execution The Evolution Of Programming

Language Translator And The Types Of

Translator Six Criteria Of A Good Program

1

UNIT

1

Page 2: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

INTRODUCTION TO COMPUTER SYSTEMS

Computer is an electronic device used to process data.

Computer system is a group of reference to all computer hardware related including processor, storage

devices, input devices, output devices and communication devices.

2

Page 3: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Environment of Computer System

The four parts of computer system are:

Component Hardware Category

Hardware is parts in computer that can be seen and touched.

The five categories are:

1. Input Device

Input device is an intermediary device that allows user to enter data and instruction

to computer’s memory.

Example: Mouse, microphone and keyboard

2. Output Device

It is used to generate information that has been executed by the computer for the

user.

Example: printer, monitor and speaker.

3. Storage Device

3

Software- set of computer’s instructions that directs devices to carry out tasks.

User – a person who uses the computer.

Data – facts arrangement that can be modified by computer into a beneficial form.

Hardware – parts of a computer that can be seen and touched.

Page 4: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

It is used to store and access data, instructions and information from memory.

Example: Floppy disc drive, CD-ROM drive and DVD-ROM Drive.

4. Communication Device

Allows user to communicate or exchange data/ information with other users.

Examples: Modem, Fibre Optic, and Coaxial Cable

5. System Unit

System unit is plastic and metal coats containing Centre Processing Unit (CPU),

memory and other electronic devices.

CPU, sometimes named as processor, is an electronic device that executes instruction

in computer.

Memory is a series of electronic element that holds data and instructions during the

period of processing by CPU.

Types of Software

Software is a set of computer instructions that directs devices to carry out tasks. The two types of

software are:

1. System Software

All programs related to modification and monitoring computer’s operations

including operating system (OS), translator and program services. Example: Disk

Operating System (DOS)

2. Application Software

4

Page 5: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Application Software is a program designed to perform task or functions. Example:

Word Processing (Microsoft Word) and Spreadsheet Software (Microsoft

Excel).

Software development refers to all activities to produce solution of information system regarding

to organisation problem.

The activities are system analysis, system design, programming, testing, conversion and

production and also maintenance.

DEFINITION OF PROGRAM, PROGRAMMING AND PROGRAMMER

To understand program design in application software, user should know the definitions of:

Program:

A set of step-by-step instructions that directs a computer to perform a specific task and to

produce the required results.

Programming:

Programming is a process of designing/ creating a program.

Programmer:

Programmer is a person who writes the program.

STEPS OF PROGRAM EXECUTION

Input – Refers to the process of entering data, program and instructions into the computer system

using input devices.

5

INPUT (data) OUTPUT (information)PROCESS (processor)

Page 6: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Process – Computer processes raw data into usable information to be used by user. Data processing is

done by the CPU (Central Processing Unit).

Output – Output is raw data that has been processed by the computer (result). Output will be

converted to an understandable form before being displayed or printed.

Example: Flow of ATM program

Assume that our transaction is money withdrawal. The instructions are:

a. Get the card number from the user (Input)

b. Get pin number from the user (Input)

c. Process the input data (Process)

d. Get the transaction chosen by the user (Input)

e. Get the account type from the user (Input)

f. Process the transaction as wanted by the user (Process)

g. Withdraw amount of money required by the user (Output)

h. Print receipt for the user (Output)

Input: Example: Card number, ATM pin number, type of transaction, type of account,

amount of money to withdraw.

Process: Example: Process to identify card number, valid pin number, type of transaction,

type of account and deducts the withdrawal from the user’s account.

Output: Example: Receipt will show balance in user’s account and money withdrawn.

EXECUTING A PROGRAM

To execute a program, CPU will examine each program instruction in memory and send out the

required command signals to carry out the instruction.

During execution, data can be entered into memory and manipulated in some specific way (delete and

modify) as required.

6

INPUTPROCESS

OUTPUT

Page 7: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Program instructions are used to copy a program's data (program input) into memory.

After the input data were processed, instructions for displaying or printing will be executed.

Result displayed by a program is called “program output”.

Example: Cash withdrawal from ATM machine.

First step:

Data entered by user are stored in memory (input).

Second step:

CPU will instruct program to process the card number and ATM pin number with the

data in memory concurrently. Program will execute the transaction chosen by the user

and store the result in memory.

Third step:

The outputs are money withdrawal and receipt (output).

PROGRAMMING LANGUAGE

Definition:

A set of rules and reserved words (keywords) that can be used to tell a computer what are the

operations to be done.

Artificial language composed by a fixed vocabulary and set of rules is used to create instructions

for the computer to follow.

Now, there are over 200 programming languages in the world. For example: Cobol, Pascal, C, C+

+, and Fortran.

How Human Interact:

Example: People communicate using human languages, such as Bahasa Melayu, English and

Japanese.

7

Machine language program for processing card number and PIN number.

Data entered during execution.

Computed results.

Central Processing

Unit.

Output results:Receipt and money

Input data: Card Number, PIN Number, transaction.

Program Output

Step 1

Step 2

Step 3

Interaction

HUMAN LANGUAGE (BAHASA MELAYU)

Page 8: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

How Human and Computer Interact:

People communicate with computers using programming languages such as C, C++ and Cobol.

8

#include <stdio.h>#include <conio.h>

main(){int a,b,c; /* hold variables value*/int sum; /* variable – to hold total (a+b+c) */float avg; /* variable output*/clrscr();

printf (“Input three numbers:”); /* ask user to enter 3 numbers*/scanf (“%d %d %d”, &a, &b, &c);sum = a+b+c; /* calculate total of a, b and c*/avg = sum / 3; /* calculate average*/

printf (“Average of the numbers: %.2f”, avg); /* Display the average*/}

Program (Aturcara) Mengira Hasil Purata Tiga Nombor Dalam Bahasa C

PROGRAMMING LANGUAGE (C, C++, COBOL)

Interaction

Example program in C language to calculate average of 3 numbers

Page 9: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

EVOLUTION OF PROGRAMMING LANGUAGES

This figure shows the generations of language based on chronological time factor.

The darker shade indicates the period of greater use by application programmers.

The lighter shade indicates the time which generation faded eventually from its primary use.

1. First Generation Language (Machine language (1950-1960))

The lowest level of language.

It represented data and program instructions as 1s and Os - binary digits corresponding to the on

and off electrical states in the computer. Examples: 0 represents “off” and 1 represents “on”.

The only language that the computer could understand (does not require translator).

Machine language is machine-dependent. Meaning, a written program for one computer generally

cannot be executed on another. For example, a program written in Apple PC cannot be run in IBM

PC.

Advantages:

o The most efficient in terms of storage area and speed.

o Allows the program to fully utilize the computer's potential of data processing.

Disadvantages:

9

Year

First Generation

Second Generation

High Level Language – 3rd Generation

Very High Level Language – 4th Generation

Human Language – 5th Generation

1

2

3

4

5

1960 1970 1980 1990

Level

Page 10: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

o The instruction and syntax in a program is not standardised. It depends on the type of

computer used.

o Program instruction executes only on the specific machine for which it was written.

Program Example:

2. Second Generation (Assembly Language (1950s)

Known as symbolic language.

It was developed to replace "0" and "1" used in Machine Language.

It uses mnemonic codes - a symbol chosen to help user to remember the significance of the

symbol. For example, A represented ADD and S represented SUM

Computer does not understand the language so it needs an assembler to translate the program to

machine language.

Advantages:

o Program can run faster.

o An efficient use of the memory.

Disadvantages:

o Errors can be made easily - difficult to identify & correct errors.

o It is difficult to understand.

o It takes a long duration of time to write the program.

10

Instruction to compute 2 * 4 using machine language

Page 11: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Example of Mnemonic code:

3. High Level Language ((3GL, 4GL, 5GL) 1960s till now)

Instruction is written as a series of English-like words.

Translator (Compiler/Interpreter) is needed to translate high level language to machine language.

It is Machine-Independent. The program can be written and executed on any computer.

It can be classified into two categories:

i. Non-procedural language

A language that states task to be accomplished but does not state the steps needed to

accomplish it.

ii. Procedural language.

A language that represents a step-by-step process (procedure) for solving problem.

Advantage:

o Easy to be developed and maintained

11

Program to compute 2 * 4 using assembly language

3rd Generation Language (1960s)

It is called a procedural language.

Translation is done by compiler/ interpreter.

Example: FORTRAN, COBOL and BASIC.

Page 12: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

4th Generation Language (1980s)

Program is written in English-like statements.

It is a short programming language where the operation needs a few lines instead of hundreds of lines.

It is a non-procedural language.

Known as query language, which can be used to retrieve data from database.

Example: PROLOG, FOCUS, LISP and SQL.

Advantages:

o Result-oriented where they emphasize on what instead of how.

o It is simpler and easier, so programmers and non-programmers can use this language with

minimum training.

12

Create table order

( OWNERID INTEGER NOT NULL,

ITEM CHAR (40) NOT NULL);

SQL Language

‘BASIC PROGRAM

‘AVERAGING THREE INTEGERS

PRINT “Mengira Purata”

PRINT “---------------------“

PRINT “Nilai A = “; 20

PRINT “Nilai B = “; 30

PRINT “Nilai C = “; 40

PRINT “Purata tiga nombor ialah “; (20+30+40)/3

END

Program in Basic to compute the average of three numbers

Page 13: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

5th Generation Language ((Natural Language) 1990s)

A programming language that resembles human language.

Known as knowledge-based language.

Translates human instructions into code (machine language) where a computer can understand.

The use of a natural language to access knowledge base is called knowledge-based system.

Example: Expert system that stores knowledge of an expert. For example, expertise of a doctor or

engineer.

LANGUAGE TRANSLATOR

A translator is a program that:

Checks syntax of a program to ensure the programming language is used correctly, by giving you

the syntax-error messages, which is known as diagnostics.

Translates a program into language that the computer can understand.

Produces descriptive error messages known as syntax-error to programmer if there are errors in

the programs.

13

Syntax Error – error in the use of a programming

language.

Example in C:

Statement: clrscr()

If semicolon (;) is not placed after statement, a

message will display:

“Error DIT103.CPP: Declaration syntax-

error”

The correct syntax is:

clrscr();

Syntax – set of rules to be followed when

writing a program, these rules are similar

to the grammatical rules in English.

Example:

Message displayed on the screen is

“please enter a number”.

C Language:

printf(“please enter a number”);

Natural Language

Page 14: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

Types of Translator

1. Assembler

Translates the whole program at one time.

Also called assembler program.

A program, which is used to convert assembly language to machine language.

2. Compiler

Translate the whole program at one time.

Translates the source code, for example COBOL language to machine language.

Used to translate C, COBOL and other language.

14

sseg segment stackdb 256 dup (?)

sseg endsdseg segmentdata db “2 x 4 = ”dseg endscseg segmentassumecs:cseg:ds:sdeg:ss:sseg.es:nothingStart proc far

11101 100000 100000111101011100011101000111111001011100010001110 110110001011100010001110 11000000

Assembler

Assembly LanguageAssembly Language

Machine LanguageMachine Language

# include <stdio.h>

main( ){ int x, y, z, sum; double avg;

printf(" Enter 3 numbers:"); scanf("%d %d %d",&x, &y, &z); sum = x + y + z; avg = sum / 3; printf("x = %d y = %d z = %d ", x, y, z); printf("the average is = %f",avg);

return 0; }

11101 100000 100000111101011100011101000111111001011100010001110 110110001011100010001110 11000000

Compiler

C LanguageC Language

Machine LangeageMachine Langeage

Page 15: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

3. Interpreter

Program that translates a high level language to machine language.

It translates one line at a time and executes each line after it is translated.

Displays feedback as the error occurred.

Used to translate BASIC and SQL language.

CRITERIA OF A GOOD PROGRAM

A good program is a program that has fulfils the following criteria’s:

1. Accuracy

Program that fulfils all the specifications, requirements and objectives.

For example: ATM machine. When a user withdraws RM200, the user will get RM200 in

cash.

To ensure accuracy, programmer needs to define a control to prevent invalid value.

2. Reliability

No errors. Produces output as required.

Information is reliable if the user can depends on its accuracy and completeness.

Example: the balance in Touch N Go card.

3. Usability

Easy to use and learn. Programmer or non-programmer can use the interaction.

Example, LRT-STAR machine (ticket) can be used by anyone.

15

10001110 11000000101110101011101110001101 1101101011111110110000 00000001010110011 00000100011100011 11111100000001110 110000101000001011100110100100

Interperter

Basic LanguageBasic Language

Machine LanguageMachine Language

‘BASIC PROGRAM‘AVERAGING THREE INTEGERSPRINT “Mengira Purata”PRINT “---------------------“PRINT “Nilai A = “; 20PRINT “Nilai B = “; 30PRINT “Nilai C = “; 40PRINT “Purata tiga nombor ialah “; (20+30+40)/3END

Page 16: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

4. Maintainability

A program that is easily understood, tested and modified without having to change the

whole program. The program may be changed by any programmers at anytime to fulfil the

current requirement.

Example: Total digit of telephone number can be changed from 7 to 8 digits without

changing the existing application program.

5. Efficiency

Fast program execution and quick access to data to generate output.

Example: Tickets System in LRT-STAR System

6. Readability

A readability program is:

A clear and understandable program.

Using meaningful name for variables.

Have explanations on the program’s function.

Instruction is written by using spacing in between words.

Example of a readable program:

16

/* compute total of 2 numbers*/

printf (“Enter first number\n”);

scanf (“%d”, &number1);

printf (“Enter second number\n”);

scanf (“%d”, &number2);

total = number1 + number2;

printf (“Total is %d\n”, total);

Page 17: Unit 1

F1001 PROGRAMMING FUNDAMENTALS

17