PPT01-Introduction To Algorithm and Programming
PPT01-Introduction To Algorithm and Programming
TOPIC 01
1. Algorithm Definition
2. Representing Algorithm
3. Structure Theorem
4. C Structure
ALGORITHM DEFINITION
o Algorithm is a procedure for solving a problem in terms of the actions to be executed, and
the order in which these actions are to be executed
o Derived from the word algoris and ritmis. Introduced by Al-Khowarizmi.
o In the programming domain, algorithm define as method that consist of structured steps in
problem solving using computer.
ALGORITHM SHOULD HAVE THE
FOLLOWING CHARACTERISTICS
o Unambiguous
o Input
o Output
o Finiteness
o Feasibility
o Independent
SIMPLE ALGORITHM EXAMPLE
COMPILE
Syntax Err
Output Err
o An informal artificial language similar to everyday English that helps you develop algorithms
before converting them to structured C programs.
o Pseudo-code is similar to everyday English, convenient, and user friendly
o Pseudocode consists only of action and decision statements
o Keywords are used to describe control structure
Example:
if, else, print, set, add, while, etc.
PSEUDO-CODE (CONT)
Structure theorem by Böhm and Jacopini’s which makes the computer programming
possible using only three control structure, which are:
1. Sequence
2. Selection
3. Repetition
STRUCTURE THEOREM
Structure theorem by Böhm and Jacopini’s makes the computer programming possible using only
three control structures, which is:
1. Sequence
o Sequence is a series of consecutive commands/statements
o Commonly programming language has a sequence of statements flowing from the top of the
program to its end
2. Selection
o Selection control structure is a structure that allows us to choose from several options of
statement/command
o The first statement will be executed if the condition is satisfied, if not then the else statement will be
executed (if the other exist)
3. Repetition
o A number of statements/commands can be repeated several times using Repetition structure
control
o Statements/commands will be repeated while the looping condition is satisfied
(may use DOWHILE – ENDDO)
HISTORY OF C
o C evolved from two previous languages, BCPL and B.BCPL was developed in 1967 by Martin
Richards
o In 1970, Ken Thompson used B to create early versions of the UNIX operating system at
Bell Laboratories
o C language was evolved from B by Dennis Ritchie at Bell Laboratories and was originally
implemented on DEC PDP-11 computer in 1972
o The publication in 1978 of Kernighan and Ritchie’s book, The C Programming Language
o 1983 X3J11 technical committee was created to make a standard of the language
o 1989 Standard was approved
o 1999 The standard was updated
o C99 is a revised standard for the C programming language
WHY USING C
o Flexibility
Close to low level machine language yet easy to understand
o Portability
Used from microcomputer to super computer
o A Well Known Programming Language
It is used in many forms of implementations such as O/S, scientific application, business
application, etc.
o Supported With a Large Number of Libraries
C STANDARD LIBRARY
main() main()
1. { 3. {
statements; statements;
} return(0);
}
}
// This program will simply print out a
message
ESCAPE SEQUENCES
o ASCII
American Standard Code for Information Interchange
http://www.asciitable.com/
IDENTIFIER
o The naming mechanism for various elements in a program such as variable, function,
constant, etc.
o Started with a letter or underscore_
o It is case sensitive
o Maximum length varies for every compiler
Example: Turbo 2.0 (DOS), max 32 characters
o Never use reserved word/keyword
(such as: for, while, if, main)
o Example:
name, x1, _total, cubic()
wrong: 1time, int
KEYWORDS
o Keywords/reserved words are words that have special meaning to the C compiler.
o 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
o 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:
o Beside used in function identifier type as no return value, keyword void also used as data type in
variable.
o Void data type is data type that can be transform into any data type (will be discussed later in pointer)
CONSTANT
Constant/symbolic constant does not have an address (only value) and its value can not be changed at run
time.
Constant type:
Integer constant -5
Floating-point constant 3.14
Character constant 'C' '1' '$'
Escape sequence \n \t \''
String constant ''BiNus‘’
⑥ Symbolic constant #define PHI 3.14
const float PHI=3.14;
'H' is a character constant
"H" is a string constant
1 is a integer constant
'1' is a character constant
const float Pi= 3.1415926; Pi is a symbolic constantA constant
SIZEOF
o Example :
sizeof(int) = 4 => Dev-V (Windows)
sizeof(int) = 2 => Turbo C ver 2.0 (DOS)
SUFFIX
o Example :
• 3.14 (double)
• 3.14f (float)
• 3.14L (long double)
SUFFIX (CONT)
o Example :
• 174 (integer)
• 174u (unsigned integer)
• 174L (long integer)
• 174ul (unsigned long integer)
SUFFIX (CONT)
o Some compilers will give warning for differ in data type, as can be seen from the following
example Visual C++:
o Example :
float x;
x = 3.14;
warning: truncation from 'const double' to 'float’
o How to deal with the issue? You may use casting or suffix
float x;
x = (float)3.14; // casting
x = 3.14f; // or suffix
SUMMARY
o Paul Deitel & Harvey Deitel. (2022). C how to program. 09. Pearson Education. Hoboken. ISBN: 978-0-
13-739839-3. Chapter 2, 3 , 4, 5 and 9 .
o https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
o Writing Your First C Program: http://aelinik.free.fr/c/ch02.htm
o Data Types and Names in C: http://aelinik.free.fr/c/ch04.htm
o Programming in C: http:// www.cs.cf.ac.uk/Dave/C/
o Pseudocode Examples: http://www.unf.edu/~broggio/cop2221/2221pseu.htm
o C Language Tutorial: http://www.lysator.liu.se/c/bwk-tutor.html