You are on page 1of 27

TOPIC 1:

INTRODUCTION TO COMPUTER
PROGRAMMING
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++ program is
processed
Ø Learn what an algorithm is and explore problem-solving
techniques
BASIC CONCEPT

Computer Program

Programming Language

Programmer
BASIC CONCEPT

¢ Computer Program:
Ø Is a set of detailed, step-by-step instructions that
directs the computer to do what you want it to do.
Ø Another name for software
Ø Programs are written in a programming language
BASIC CONCEPT (CONT.)

¢ Computer Program:
Ø Turn data (input) into useful information (output).

Input Program Output

Storage
BASIC CONCEPT (CONT.)

¢ 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
BASIC CONCEPT (CONT.)

¢ Programmer:
Ø A person who writes programs by using programming
language.
THE LANGUAGE OF COMPUTER

¢ Generation of Programming Language:

High
Machine Assembly
Level
Language Language
Language
THE LANGUAGE OF COMPUTER (CONT.)

¢ Machine Language:
Ø Binary number codes understood by a specific CPU
Ø The computers only executes machine language
instructions
Ø Binary digit (bit): The digit 0 or I
Ø Binary code: A sequence of 0s and 1s
Ø Byte: A sequence of eight bit
THE LANGUAGE OF COMPUTER (CONT.)

¢ Assembly Language:
Ø Very low-level
Ø Use mnemonic codes, abbreviations to replace 0s and 1s
Ø Easier to understand
Ø Assembler: translates a program written in assembly
language into machine language
THE LANGUAGE OF COMPUTER (CONT.)

¢ Using the assembly language instructions, the equation


wages = rates x hours can be written as follows:

LOAD rates
MULT hours
STOR wages
THE LANGUAGE OF COMPUTER (CONT.)
¢ 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.
Ø The equation wages = rates x hours can be written
as follows:
wages = rate * hours;
PROCESSING A PROGRAM

¢ 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:
• Check that the program obeys the rules
• Translate into machine language (object program)
PROCESSING A PROGRAM (CONT.)

¢ Linker: Combines the object program


with the programs from libraries to
create executable code
¢ Loader: Loads executable program into
main memory
¢ The last step is to execute the program
A C++ PROGRAM

Output:
PROBLEM SOLVING

¢ Programming is a process of problem solving


¢ Problem solving techniques:
Ø Analyze the problem
Ø Outline the problem requirements
Ø Design steps (algorithm) to solve the problem
¢ Algorithm: A step-by-step action to perform overall task of
the program
PROGRAM DEVELOPMENT LIFECYCLE

ØOutline the problem and its requirements


Step 1: Problem analysis

ØDesign steps (algorithm) to solve the


Step 2: Program design problem

Step 3: Coding ØImplement the algorithm in code

Step 4: Testing ØVerify that the algorithm works correctly


and fulfil its requirements.

ØUse and modify the program if the problem


Step 5: Documentation domain changes
ANALYZE THE PROBLEM

¢ Thoroughly understand the problem


¢ Understand problem requirements
¢ Problem Analysis: A process of identifying the output,
processes and input 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)
DESIGN AN ALGORITHM

¢ Tools used for designing an algorithm:


Ø Pseudo-code: An English-like phrases which are used to describe the
algorithm.
Ø Flowchart: A set of symbols to describe the algorithm.

¢ If problem was broken into subproblems


Ø Design algorithms for each subproblem

¢ Check the correctness of algorithm


Ø Can test using sample data
Ø Some mathematical analysis might be required
DESIGN AN ALGORITHM (CONT.)
¢ Flowchart symbol:
WRITE THE CODE

¢ Once the algorithm is designed and correctness verified


Ø Write the equivalent code in high-level language
¢ Enter the program using text editor
ERROR CORRECTION

¢ Syntax error : During compilation and caused by incorrect usage


of the programming language

¢ Run-time error: During execution and caused by the incorrect


usage of programming logic. Occurs while the program is running
and might cause the memory leak (infinity loop) or program crash
(quits while running).

¢ Logic Error: Errors found by the programmer looking for the


causes of incorrect results. A logic error produces unintended or
undesired output or other behaviour, although it may not
immediately be recognized as such.
EXAMPLE ALGORITHM

¢ Design an algorithm (pseudocode and flowchart) to find


the perimeter and area of a rectangle
¢ The perimeter and area of the rectangle are given by
the following formulas:

perimeter = 2 * (length + width)


area = length * width
ALGORITHM (PSEUDOCODE)
Begin
1.Read length of the rectangle
2.Read width of the rectangle
3.Calculate the perimeter using the following equation:
perimeter = 2 * (length + width)
4.Calculate the area using the following equation:
area = length * width
5. Display the perimeter and area of rectangle
End
ALGORITHM (FLOWCHART)
SUMMARY

¢ A computer program is a collection of instructions that can be executed by


a computer to perform a specific task

¢ 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.

¢ 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.

¢ Assembly language uses easy-to-remember instructions called


mnemonics.
SUMMARY (CONT.)

¢ Compilers are programs that translate a program written in a high-level


language into machine code, called object code.

¢ Algorithm: step-by-step problem-solving process; solution in finite amount


of time

¢ The problem-solving process has five steps:


Ø 1. Analyze problem
Ø 2. Design an algorithm
Ø 3. Implement the algorithm in code
Ø 4. Test the program
Ø 5. Documentation

You might also like