You are on page 1of 38

INTRODUCTION TO

PROGRAMMING
ENGR. BENEDICT S. MARZAN
COMPUTER PROGRAMMING

• Computers do what we tell them to


do, NOT what we want them to do.
• Computer Programming involves
writing instructions and giving them to
the computer to complete a task.
• But what is a computer program?
TYPES OF SOFTWARE AVAILABLE

• Operating System Software


• Compilers and Interpreters
• Application Software
• Embedded System Software
• Utilities and Tools for productivity
A PROGRAMMER

• A programmer is a person who writes the


required computer programs.

• Programmers translate the expected tasks


given in human understandable form into
machine understandable form by using
compilers and interpreters.
COMPILER AND INTERPRETER

• Compiler is a specific software that gets the


whole Source Code and translates it into Object
Code all at a time.

• Interpreter is translating and executing one


statement of Source Code into Object Code at a
time.
EXECUTION OF A PROGRAM
Using Compiler:

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

2. Program Design involves creating an algorithm


– sequence of steps, by which a computer can
produce the required outputs from the available
inputs

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>

3. Program Coding int main()


means expressing the {
algorithm developed int number1, number2;
int maximum;
for solving a problem,
in a programming printf("Please, enter two numbers: ");
language scanf("%d %d", &number1, &number2);

if (number1 >= number2)


maximum = number1;
• Example of source else
code is on the right maximum = number2;

printf(“%d is maximum\n“, maximum);

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? Interpreter or Compiler finds it in Syntax Check Phase.

• Semantic Errors: Doing logical mistakes causes semantic errors in Source


code.

• Effect? Interpreters and Compilers can not notice them, but on execution,
they causes unexpected results.

• Run-time Errors: Occur on program execution. Mostly caused by invalid


data entry or tries to use not existing resources.

• Effect? It occurs on run time and may crash the program execution
PROGRAMMING

• Programming task can be made easier


• by breaking large and complex programs into smaller and less
complex subprograms
• Programming task can be separated into 2 phases
1. problem solving phase
• produce an ordered sequence of steps that describe solution of
problem
• this sequence of steps is called an algorithm
2. implementation phase
• implement the program in some programming language
(Pascal, Basic, C, C++, Java….etc)
STEPS IN PROBLEM SOLVING
• First produce a general algorithm (one can use
pseudocode)
• Pseudocode is an artificial and informal language
that helps programmers develop algorithms.
Pseudocode is very similar to everyday English.
• Refine the algorithm successively to get step by
step detailed algorithm that is very close to a
computer language.
PSEUDOCODE & ALGORITHM

Example 1: Write a pseudocode and an


algorithm to determine the type of
flow(Laminar and Turbulent only) of a
certain fluid.
THE FLOWCHART
• (Dictionary) A schematic representation of a
sequence of operations, as in a manufacturing
process or computer program.
• (Technical) A graphical representation of the
sequence of operations in an information system or
program. Information system flowcharts show how
data flows from source documents through the
computer to final distribution to users. Program
flowcharts show the sequence of instructions in a
single program or subroutine. Different symbols are
used to draw each type of flowchart.
THE FLOWCHART

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

Oval Denotes the beginning or end of the program

Parallelogram Denotes an input operation

Rectangle Denotes a process to be carried out


e.g. addition, subtraction, division etc.

Diamond Denotes a decision (or branch) to be made.


The program should continue along one of
two routes. (e.g. IF/THEN/ELSE)

Hybrid Denotes an output operation

Flow line Denotes the direction of logic flow in the program


LOGICAL CONTROL STRUCTURES

• The order in which process steps are done


can be broken down into three
fundamental control patterns:
✓Sequence
✓Selection
✓Repetition
LOGICAL CONTROL STRUCTURES

• With a sequential Pattern, one step is completed.


Then a next step is completed and so on.
LOGICAL CONTROL STRUCTURES

• With a selection pattern, the answer to a decision


causes a branch in the process. Thus, some steps
are skipped under certain circumstances
LOGICAL CONTROL STRUCTURES

• The repetition ( also known as loop )


control structure, allows one or more
actions to be repeated.
GUIDELINE FOR FLOWCHARTING
EXAMPLE 2

• Write an algorithm and draw a flowchart to


convert pressure in atm to in. Hg at 32 oF.
Pseudocode:
• Input the pressure in atm (Patm)
• Calculate pressure in in. Hg (Phg) by multiplying
Patm with 29.92
• Print pressure in in. Hg (Phg)
EXAMPLE 2 Flowchart
START

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

• Hint: d = sqrt ( b2  4ac ), and the roots are:


x1 = (–b + d)/2a and x2 = (–b – d)/2a
EXAMPLE 3
Pseudocode:
• Input the coefficients (a, b, c) of the
quadratic equation
• Calculate d
• Calculate x1
• Calculate x2
• Print x1 and x2
SEATWORK

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)

• Step 4: x2  (–b – d) / (2 x a) X2  (–b – d) / (2 x a)

• Step 5: Print x1, x2 Print


x1 ,x2

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

• The structure is as follows


If condition then
true alternative
else
false alternative
IF-THEN-ELSE STRUCTURE

• The algorithm for the flowchart is as follows:


If A>B then
print A
else
print B
endif
RELATIONAL OPERATORS

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

• Write an algorithm and flowchart that reads two values,


determines the maximum number and prints the largest value
with an identifying message.
PSEUDOCODE
~Input the first value and the second value
~if the first value is greater than the second value then
print the first value as the maximum number
~else if the second value is greater than the first value then
print the second value as the maximum number
NESTED IFS

• One of the alternatives within an IF–THEN–


ELSE statement
• may involve further IF–THEN–ELSE
statement
EXAMPLE 5
Write an algorithm and flowchart that reads three values
which determines the maximum number and prints the
largest value with an identifying message.
PSEUDOCODE
~Input the first value, second value, and the third value
~if the first value is greater than the second value and the
third value then
print the first value as the maximum number
~else if the second value is greater than the first value and
the third value then
print the second value as the maximum number
~else if the third value is greater than the first value and the
second value then
print the third value as the maximum number
EXAMPLE 5
Step 1: Input N1, N2, N3
Step 2: if (N1>N2) then
if (N1>N3) then
MAX  N1 [N1>N2, N1>N3]
else
MAX  N3 [N3>N1>N2]
endif
else
if (N2>N3) then
MAX  N2 [N2>N1, N2>N3]
else
MAX  N3 [N3>N2>N1]
endif
endif
Step 3: Print “The largest number is”, MAX

You might also like