PREPARED BY: DR NUR IDA ANIZA RUSLI
TOPIC 1:
INTRODUCTION TO
COMPUTER PROGRAMMING
PREPARED BY: DR NUR IDA ANIZA RUSLI
OBJECTIVES
In this chapter you will:
✓ Learn about the basic concept of a computer program
✓ Learn about the evolution of programming languages
✓ Discover what a compiler is and what it does
✓ Examine a C++ program and explore how a C++ is processed
✓ Learn what an algorithm is and explore problem-solving
CEO OF TIMMERMAN INDUSTRIES
techniques
PREPARED BY: DR NUR IDA ANIZA RUSLI
BASIC CONCEPT
Computer Program
Programming Language
ProgrammerCEO OF TIMMERMAN INDUSTRIES
PREPARED BY: DR NUR IDA ANIZA RUSLI
BASIC CONCEPT (CONT.)
1. Computer Program:
✓ A set of detailed, step-by-step instructions that directs a computer to
do what you want it to do
✓ Another name for software
✓ Programs are written in a programming language
CEO OF TIMMERMAN INDUSTRIES
PREPARED BY: DR NUR IDA ANIZA RUSLI
BASIC CONCEPT (CONT.)
1. Computer Program (cont.):
✓ Turn data (input) into useful information (output)
Input Program Output
CEO OF TIMMERMAN INDUSTRIES
Storage
PREPARED BY: DR NUR IDA ANIZA RUSLI
BASIC CONCEPT (CONT.)
2. Programming Language
✓ A set of rules, words, and symbols are used to write a computer program
✓ Provides a way of telling the computer what operations to perform
✓ Syntax: Rules that specify which statements (instructions) are legal
PREPARED BY: DR NUR IDA ANIZA RUSLI
BASIC CONCEPT (CONT.)
3. Programmer
✓ A person who writes programs using a programming language
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER
Generation of programming language:
High
Machine Assembly
Level
Language Language
Language
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER (cont.)
1. Machine Language
✓ A low-level language
✓ Binary number codes understood by a specific central processing unit
(CPU)
✓ The computers only execute machine language instructions
✓ Binary digit (bit): The digit 0 or 1
✓ Binary code: A sequence of 0s and 1s
✓ Byte: A sequence of eight-bit
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER (cont.)
2. Early computers were programmed in machine language
3. To calculate wages = rates x hours in machine language:
100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER (cont.)
2. Assembly language
✓ A low-level language
✓ Use mnemonic codes, abbreviations to replace 0s and 1s
✓ Easier to understand
✓ Assembler: translates a program written in assembly language into
machine language
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER (cont.)
Assembly Language Machine Language
2. Assembly language (cont.)
LOAD 100100
STOR 100010
MULT 100110
ADD 100101
SUB 100011
✓ Using the assembly language instructions, the equation
wages = rates x hours
can be written as follows:
LOAD rates
MULT hours
STOR wages
PREPARED BY: DR NUR IDA ANIZA RUSLI
THE LANGUAGE OF COMPUTER (cont.)
3. High-level language
✓ It is an English-like programming language. It is closer to human language
✓ High-level languages include Basic, FORTRAN, COBOL, Pascal, C++, C,
and Java
✓ Interpreter: translates each statement in a high-level language program
individually into a machine language
✓ Compiler: translates all statements in a high-level language program as a
complete unit into a machine language
wages = rates x hours wages = rates * hours;
PREPARED BY: DR NUR IDA ANIZA RUSLI
PROCESSING A PROGRAM
1. To execute a program written in a high-level language such as C++:
✓ Use an editor to create a source program in C++
✓ In a C++ program, statements that begin with the symbol # are called
preprocessor directives. These statements are processed by a program
called preprocessor
✓ Use the compiler to:
▪ Translate the program into a machine language (object program)
PREPARED BY: DR NUR IDA ANIZA RUSLI
PROCESSING A PROGRAM (cont.)
2. Linker: combines the object program with the
programs from libraries
3. Loader: loads the executable program into the
main memory
4. The last step is to execute the program
PREPARED BY: DR NUR IDA ANIZA RUSLI
A C++ Program
Input stream
Output stream
PREPARED BY: DR NUR IDA ANIZA RUSLI
PROBLEM SOLVING
1. Programming is a process of problem-solving
2. Problem-solving techniques:
✓ Analyze the problem
✓ Outline the problem requirements
✓ Design steps (algorithm) to solve the problem
3. Algorithm: A step-by-step action to perform the overall task of the program
PREPARED BY: DR NUR IDA ANIZA RUSLI
PROGRAM DEVELOPMENT LIFECYCLE (PDLC)
✓ Outline the problem and its requirements
Step 1: Problem analysis
✓ Design steps (algorithm) to solve the problem
Step 2: Algorithm design
✓ Implement the algorithm in code
Step 3: Algorithm implementation (coding)
✓ Verify that the algorithm works correctly
✓ Program must be free from error and fulfil its requirements
Step 4: Testing and debugging
✓ Use a set of data to validate the output
✓ Use and modify the program if the problem domain changes
Step 5: Maintenance and documentation ✓ User manual (program description: capability, limitation, user guide)
PREPARED BY: DR NUR IDA ANIZA RUSLI
ANALYZE THE PROBLEM
1. Thoroughly understand the problem
2. Understand problem requirements
3. Problem analysis: A process of identifying the input, processes, and output of a
program
✓ How to identify input?: Nouns and adjectives keywords such as read, get, accept,
enter
✓ How to identify output?: Nouns and adjectives keywords such as print, display,
produce
✓ Define the storage (variable) and data type to hold data
✓ Outline the process (arithmetic or logic operation)
PREPARED BY: DR NUR IDA ANIZA RUSLI
DESIGN AN ALGORITHM
1. Tools used for designing an algorithm:
✓ Pseudocode: An English-like phrases which are used to describe the algorithm
✓ Flowchart: A set of symbols to describe the algorithm
2. If the problem was broken into subproblems:
✓ Design algorithms for each subproblem
3. Check the correctness of algorithm
✓ Can test using sample data
✓ Some mathematical analysis might be required
PREPARED BY: DR NUR IDA ANIZA RUSLI
DESIGN AN ALGORITHM
4. Flowchart symbol:
Symbol Name Function
Start/End An oval represents a start or end point
Input/Output A parallelogram represents input or output
Arrows A line is a connector that shows relationships
between the representative shapes
Process A rectangle represent a process
Decision A diamond indicates a decision
PREPARED BY: DR NUR IDA ANIZA RUSLI
EXAMPLE ALGORITHM
1. Design an algorithm (pseudocode and flowchart) to find the perimeter
and area of a rectangle
2. The perimeter and area of a rectangle are given by the following
formulas:
perimeter = 2 * (length + width)
area = length * width
PREPARED BY: DR NUR IDA ANIZA RUSLI
ALGORITHM: PSEUDOCODE
Start
1. Read the length of a rectangle
2. Read the width of a rectangle
3. Calculate the perimeter of the rectangle using the formula:
perimeter = 2 * (length + width)
4. Calculate the area of the rectangle using the formula:
area = length * width
5. Display the perimeter of the rectangle
6. Display the area of the rectangle
End
PREPARED BY: DR NUR IDA ANIZA RUSLI
Start
ALGORITHM: Read the length of a rectangle
FLOWCHART Read the width of a rectangle
Calculate the perimeter of the rectangle using the formula:
perimeter = 2 * (length + width)
Calculate the area of the rectangle using the formula:
area = length * width
Display the perimeter of the rectangle
Display the area of the rectangle
End
PREPARED BY: DR NUR IDA ANIZA RUSLI
WRITE THE CODE
1. Once the algorithm is designed and correctness verified
✓ Write the equivalent code in a high-level language
2. Enter the program using text editor
Basic control structures
PREPARED BY: DR NUR IDA ANIZA RUSLI
EXERCISE
1. Design an algorithm to find the perimeter and area of a parallelogram
2. The perimeter and area of a parallelogram are given by the following
formulas:
perimeter = 2 * (side + base)
area = base * vertical_height
PREPARED BY: DR NUR IDA ANIZA RUSLI
SUMMARY
1. A computer program is a collection of instructions that can be executed by a computer to
perform a specific task
2. Input to the computer is done via an input device. Two common input devices are the
keyboard and the mouse. The computer sends its output to an output device, such as the
computer screen.
3. The most basic language of a computer is a sequence of 0s and 1s called machine
language. Every computer directly understands its own machine language.
4. Assembly language uses easy-to-remember instructions called mnemonics.
PREPARED BY: DR NUR IDA ANIZA RUSLI
SUMMARY (cont.)
5. Compilers are programs that translate a program written in a high-level language into
machine code, called object code.
6. Algorithm: step-by-step problem-solving process
7. The problem-solving process has five steps:
✓ 1. Analyze problem
✓ 2. Design an algorithm
✓ 3. Implement the algorithm in coding
✓ 4. Testing and debugging
✓ 5. Maintenance and documentation
PREPARED BY: DR NUR IDA ANIZA RUSLI
THANK YOU
FOR YOUR ATTENTION