Comp App PDF
Comp App PDF
PROGRAMMING
ENGR. BENEDICT S. MARZAN
COMPUTER PROGRAMMING
Object Code
Source Code Compiler
Execute Program
Using Interpreter:
Execute a line
Source Code Interpreter
of Program
CHARACTERISTICS OF A PROGRAM
• Well designed programs must be:
• Correct and accurate
• Easy to understand
• Easy to maintain and update
• Efficient
• Reliable
• flexible
PROGRAMMING PROCESS
1. Problem definition
• What must the program do?
• What outputs are required and in what form?
• What inputs are available and in what form?
Example: Find a maximum of two numbers
• Input two numbers, compare them and print the
maximum value
• Inputs and outputs are decimal numbers
• Inputs are entered from the keyboard
• Result is shown on the monitor
PROGRAMMING PROCESS
Top-down design
• The main problem is split into subtasks
• Then each subtask is divided into simpler
subtasks, etc. unless it is clear how to solve all
such subtasks
PROGRAMMING PROCESS
#include <stdio.h>
return 0;
}
PROGRAMMING PROCESS
• Program Compilation – translation of a program written in a
high-level programming language into machine language
instructions
• Compilation step converts a source program into an
intermediate form, called object code
• Linking step is necessary to combine this object code with
other code to produce an executable program
• The advantage of this two-step approach:
• Source of the large program may be split into more than
one file
• These files are worked on and compiled separately
• Object code may be stored in libraries and used for many
programs
• Then they will be combined into one executable code
PROGRAMMING PROCESS
4. Program Testing & Debugging
• Initially, almost all programs may contain
a few errors, or bugs
• Testing is necessary to find out if the
program produces a correct result.
Usually it is performed with sample data
• Debugging is the process of locating and
removing errors
TYPES OF ERRORS
• Syntax Errors: Violation of syntactic rules in a Programming Language
generates syntax errors.
• Effect? Interpreters and Compilers can not notice them, but on execution,
they causes unexpected results.
• Effect? It occurs on run time and may crash the program execution
PROGRAMMING
A Flowchart
• shows logic of an algorithm
• emphasizes individual steps and
their interconnections
• e.g. control flow from one action to
the next
FLOWCHART SYMBOLS
Name Symbol Use in Flowchart
Input
Patm
Algorithm
• Step 1: Input Patm
Phg Patm x 30
• Step 2: Phg → Patm x 29.92
• Step 3: Print Phg Print
Phg
STOP
EXAMPLE 3
• Write pseudocode, algorithm and draw a
flowchart that will calculate the roots of a
quadratic equation
START
• Algorithm: Input
a, b, c
• Step 1: Input a, b, c
d sqrt(b x b – 4 x a x c)
• Step 2: d sqrt ( ( b b) 4 a c )
• Step 3: x1 (–b + d) / (2 x a) x1 (–b + d) / (2 x a)
STOP
DECISION STRUCTURES
DECISION STRUCTURES
• The expression A>B is a logical expression
• it describes a condition we want to test
• if A>B is true (if A is greater than B) we take the
action on left
• print the value of A
• if A>B is false (if A is not greater than B) we take
the action on right
• print the value of B
IF-THEN-ELSE STRUCTURE
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
>= Greater than or equal to
<= Less than or equal to
=! Not equal to
EXAMPLE 4