Session1 - 3 - Introduction To Algorithm I - Final
Session1 - 3 - Introduction To Algorithm I - Final
Outline Materials
• Definition of an Algorithm
• Good Algorithm Practice
• Example of Algorithms
• Phase of Algorithm Development
• Definition of Problem
• Definition of Flow Chart
• Structured Theorem
• Algorithm vs Program
• Advantage of Algorithm
• Programming Language
Outline Materials
• Introduction to C
• Data Type
• Identifier
• Keyword
• Variable
• Definition of Pseudo Code
• Definition of Selection
What’s an Algorithm?
Sand Water
Blok 4
Block 3
Block 2
Block 1
PROBLEM
BOUNDARY
MODEL DESIGN
15
DOCUMENTATION FINISH
Algorithm Representation
• Description
– Describe in sentences with normal language
• Diagram or symbol
– Flow Chart
• Pseudo code
– Outline of computer programs
Definition of Problem
• To define a problem, we need to read repeatedly the
problem until the problem can be fully understood.
PRINT PRINT
“PASS” “FAILED”
STOP
Flow Chart Example
Start
Input a,b,c
d = b^2 – 4ac
Y
d<0
T
Print: x1, x2
Stop
Structured Theorem
• There are 3 main structure to write a computer program:
1. Sequence
2. Selection
3. Repetition
Sequence
• Description
Sequence of command is from the 1st line to the end of code. If
newStudent input is 2 then total that later on printed out is 51
Algorithm vs. Program
• Program is a set of computer statements, while methods and
systematic instructions is described as algorithm.
• Program is written using programming language.
• Program is implementation of algorithm into programming
language.
• Procedural
• Structured
• Object-Oriented
Structure Programming Language
• Special type of procedural programming, that provides
additional tools to manage the problems.
• Structure programming
-> break program structure into small pieces of code
• Example:
– C
– Ada
– Pascal
Introduction
• Preparation to COMP6047 - Algoritma dan Pemrograman
course at 1st semester.
• Portability
Used form micro computer to super computer
main() main()
1. { 3. {
statements; statements;
} return(0);
}
#include is a directive
#include<stdio.h>
int main() command to tell the
{ computer to search for
printf (“Welcome to BINUS\n”);
return(0);
printf function prototype
} at header file stdio.h as
well
C Structure
• Directive #include generally written at the beginning
of a program
• Coding Style (depends to the programmer)
#include<stdio.h>
int main()
{
printf (“Welcome to BINUS\n”);
return (0);
}
#include<stdio.h>
int main(){
printf (“Welcome to BINUS\n”);
return (0);
}
Comments in C
• Used for readability of the program
• Not accounted as a command/statement by the compiler
• Using /* and */
• Using // at the beginning of line for one line comment
• Example:
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (“Hello, BINUSIAN\n”);
}
// This program will simply print out a message
Escape Sequences
• \a bell, alert, system beep
• \b back space
• \t horizontal tab
• \n new line, line feed
• \v vertical tab
• \r carriage return
• \’ single quote
• \” double quote
• \\ backslash
• \xdd hexadecimal notation
• \ddd octal notation
Data Type
• In C, there are 5 data types and 4 modifiers
Data types:
– Character char
– Integer int
– Floating point float
– Double floating point double
– Void void
Modifiers:
- signed
- unsigned
- long
- short
Data Type
-128 -128 32 8 4 2 1
64 16
• ASCII
American Standards Committee for Information Interchange
http://www.asciitable.com/
Identifier
• The naming mechanism for various element in a program
such as: variable, function, constant, etc.
• Started with a letter or underscore_
• It is case sensitive
• Maximum length is vary for every compiler
Example: Turbo 2.0 (DOS), max 32 characters
• Never use reserved word/keyword
(such as: for, while, if, main)
• Example:
name, x1, _total, cubic()
wrong: 1time, int
Keywords
• Keywords/reserved words are words that have special meaning to the C
compiler.
• Example:
Keywords
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
Keywords in Visual
C++ use blue color
Variable
• Identifier for storing data/information
• Each variable has its name, address (L-value), type,
size and data (R-value)
• Data or variable value can be modified at run time
• Declaration format:
<data type> <variable name>;
<data type> <variable name> = <initial value>;
• Example:
int a, b, c, total;
float salary, bonus;
int num_students = 20;
Variable
• Example:
ch 65 123456
Range of value:
name -128 – 127
value
Variable
• Variable Declaration:
– Variable can be declared at every statement block
– Block statement or compound statement is statement
exists between { and } sign
– Example:
int x;
int y;
int z;
or:
int x, y, z;
or:
• Pseudo code
– Outline of computer program
Definition of Pseudo Code
• Example:
Read bilangan
Get tax_code
Baca students_name
2. Output
• Example:
Print “Bina Nusantara University”
Write “Algorithm and Programming”
Output Total
3. Compute
• To do arithmetic calculation the following operators are used:
+ (add)
- (subtract)
* (multiply)
/ (divide)
() (scope)
• Example:
Add number to total
Total = Total + number
4. Storing Value to An Identifier (Store)
• Example:
Set Counter to 0
Total = Price * Qty
Simple Pseudo Code Example
Class Average
Start
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input the next grade
Add the grade into the total
Add one to the grade counter
Set the class average to the total divided by ten
Print the class average.
End
Example: Rate of exchange
• A program to convert rate of exchange from Rupiah to US
Dollar. (input: Rupiah)
• Formula:
US Dollar = Rupiah / 10000
Answer: Rate of exchange
• Problem Definition
Start
Read Rupiah
Dollar = Rupiah / 10000
Print Dollar
End
Answer: Rate of exchange
Start
Print “Input amount of Rupiah = “
Read Rupiah
Dollar = Rupiah / 10000
Print “Amount of Dollar = ”
Print Dollar
End
Answer: Rate of exchange
• Testing:
– Data 1 : Rupiah = 10000
– Data 2 : Rupiah = 25000
Expected Output :
Data 1 Data 2
Dollar 1 2.5
Example: Number Calculation
• A program needed to read 2 numbers and do calculation
(addition, subtraction, multiplication, division). Final result
will be show to the screen.
Example: Number Calculation
• Problem Definition
Input Proses Output
Bil_1 Read num_1, Addition
Bil_2 num_2 Subtraction
Do addition Multiplication
Do subtraction Division
Do multiplication
Do division
Print addition
Print subtraction
Print multiplication
Print division
Example: Number Calculation
• Solution Algorithm
BEGIN
Read num_1, num_2
addition = num_1 + num_2
subtraction = num_1 - num_2
multiplication = num_1 * num_2
division = num_1 / num_2
print addition, subtraction, multiplication, division
END
• Testing:
– Data 1 : Num_1 = 10 and Num_2 = 5
– Data 2 : Num_1 = 20 and Num_2 = 10
Expected output:
Data 1 Data 2
Addition 15 30
Subtraction 5 10
Multiplication 50 200
Division 2 2
Pseudo Code Example 1
Create an algorithm pseudo code to calculate area
of rectangle with following formula:
area = length* width
Length = 5
Width = 3
Answer of Example1
• Pseudo code
Start
Print “Calculate Area of Rectangle”
Length = 5
Width = 3
Print “Area of Rectangle = ”
Area = Length * Width
Print Area
End
Pseudo Code Example 2
• Create an algorithm pseudo code to calculate an area of
rectangle with the same formula of previous example, with
new condition where the length and width is given by user
(input).
Answer of Example 2
• Pseudocode
Start
Print “Input & Calculate Area of Rectangle”
Print “Input Length = ”
Input Length
Print “Input Width = ”
Input Width
Area = Length * Width
Print “Area of Rectangle = ”
Print Area
End
Exercise
1. Create a pseudo code to convert inputted seconds into
minutes and hours
Formula: Seconds = Hours*3600 + Minutes*60
• Description
The word “Monday” will be printed out if Day’s value
equal to 1, else it will print out the sentence
“Obviously not Monday”.
Selection
• Example :
IF Menu=‘1’ THEN
Discount = 0.1 * price
ELSE
Discount = 0.2 * price
ENDIF
• Description
The word Discount value will multiply by 10% if menu
equal to , else will multiply by 20%.
Selection
• Type of selection:
1. Simple IF
2. Simple IF – ELSE
3. Switch Case
Simple IF selection
• Selection with only one IF (one condition)
• Statement will be executed with only one TRUE condition
• Example:
IF Balance > 300 THEN
Interest = Balance * 0.1
ENDIF
Simple IF selection
• IF Construction
false true
condition
statements
Selection: IF in C Program
Syntax :
if (boolean expression) statement;
or
if (boolean expression) {
statement1;
statement2; Block of
statements
……
}
false true
condition
statements 2 statements 1
Selection: IF-ELSE in C Program
Syntax :
if (boolean expression) statement1;
else statement2;
or If boolean
if (boolean expression){ expression
resulting in TRUE,
statement1; then statement1 or
statement2; Block statement1 block statement1
…… will be executed, if
} FALSE then
else { statement2 or
statement3; block statement2
be executed.
statement4; Block statement2
…
}
T0016 - Algorithm and Programming
Simple IF ELSE selection
• Example
IF balance < 300 THEN
interest = 0.05 false true
ELSE Saldo < $300
interest = 0.1
ENDIF
Bunga = 0.1 Bunga = 0.05
Combined IF selection
• Combined Selection is defined for checking more than one
conditions as a statement.
AND AND
TRUE FALSE
OR Logic
OR OR
TRUE TRUE
Combined Selection
• Truth Table
A B NOT A A AND B A OR B
True True False True True
True False False False True
False True True False True
False False True False False
Combined Selection
• Example:
IF balance> 300 AND code= 1 THEN
interest= balance* 0.1
ELSE
interest= balance* 0.05
ENDIF
• Syntax:
switch (expression) {
case constant1 : statements1; break;
.
.
case constant2 : statements2; break;
default : statements;
}
T0016 - Algorithm and Programming
Selection: SWITCH-CASE
Flow Chart of SWITCH-CASE Statement
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)