You are on page 1of 46

Algorithm & Programming and

Introduction to C Programming
Subject : T0016 ALGORITHM AND PROGRAMMING
Year : 2013
Textbooks
Main Textbook
Paul J. Dietel, Harvey M. Deitel,. 2010. C : How to Program. PEAPH.
New Jersey. ISBN:978-0-13-705966-9

Additional Textbook
Jeri R. Hanly , Elliot B. Koffman. 2009. Problem Solving and Program
Design in C. ADWES. Boston, Massachusetts, USA. ISBN:978-
0321535429
3 T0016 - Algorithm and
Programming
Course Description
This course comprises algorithm definition, basic principles of
programming with C, how to make a program using C programming
language, problem solving in C, and learning about many functions and
features in C which can be used.

By completing this course, students will have basic knowledge related
with C and able to develop program using C programming language.

This course is prerequisite for Data Structure course.
4 T0016 - Algorithm and
Programming
5
Learning Outcomes
LO1 : Explain kind of algorithms in problem solving
LO2 : Apply syntax and functions in C language in problem solving
LO3 : Construct a program using C language in problem solving
LO4 : Design a program with file processing using C language in
problem solving
LO5 : Choose the best sorting and searching algorithm in problem
solving
T0016 - Algorithm and
Programming
6
Learning Outcomes
At the end of this session, student will be able to:
Define algorithm theory and design (LO1)
T0016 - Algorithm and
Programming
7
Algorithm Definition
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

Derived from the word algoris and ritmis. Introduced
by Al-Khowarizmi.

In the programming domain, algorithm define as method
that consist of structured steps in problem solving using
computer.
T0016 - Algorithm and
Programming
8
Simple Algorithm Example
Rise and Shine Algorithm
(1)Get out of bed
(2)Take off pajamas
(3)Take a shower
(4)Get dressed
(5)Eat breakfast
(6)Carpool to work

T0016 - Algorithm and
Programming
9
Problem
Definition
Model
Development
Algorithm
Design
Writing code
PROBLEM PROCESS SOLUTION
Algorithm
Source Code Executable Code
Writing Code
COMPILE
Syntax Err
Executable code:
=> Run
Output Err
Documentation
Algorithm Development Steps
T0016 - Algorithm and
Programming
10
Representing Algorithm
How to develop an algorithm?
We can use:
Writing
Structure English and Pseudo-code.
Drawing
Flow Chart
T0016 - Algorithm and
Programming
11
Pseudo-code
An artificial and informal language that helps you
develop algorithms
Pseudo-code is similar to everyday English, convenient,
and user friendly
Keywords are used to describe control structure
Example:
if, else, print, set, add, while, etc.
T0016 - Algorithm and
Programming
12
Pseudo-code
Basic Computer Operation:
1. Input
2. Output
3. Compute
4. Storing value to an identifier (Store)
5. Compare
6. Repetition (Loop)
T0016 - Algorithm and
Programming
13
Pseudo-code Example
Example : Algorithm using a calculator
Start
Set the calculator ON
Empty any values
Do
Input price
Push plus button (+)
while all prices have been input
print total price
turn OFF calculator
End
T0016 - Algorithm and
Programming
14
Pseudo-code Example
Example : Algorithm 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
T0016 - Algorithm and
Programming
15
Flow Chart
T0016 - Algorithm and
Programming
16
Start
d = b^2 4ac
d < 0
Print message
Imaginary
x1=(-b+sqrt(d))/2a
x2 =(-b-sqrt(d))/2a
Print: x1, x2
Stop
Y
T
Flow Chart Example
Input a,b,c
T0016 - Algorithm and
Programming
17
Good Algorithm Practice
Having the right logical flow to solve the problem

Producing the correct output in a time efficient manner

Written using unambiguous structured language

Easy implementation into real programming language

All steps and operations are clearly defined and ended
T0016 - Algorithm and
Programming
18
Structure Theorem
Structure theorem which makes the computer programming
possible using only three control structure, which are:
1. Sequence
2. Selection
3. Repetition
T0016 - Algorithm and
Programming
19
1. Sequence
Sequence is series of consecutive commands/statements

Commonly programming language has sequence of
statements flowing from top of the program to its end
T0016 - Algorithm and
Programming
20
2. Selection
Selection control structure is structure that allow us to
choose from several options of statement/command

The first statement will be executed if the condition is
satisfied, if not then the else statement will be executed
(if the other exist)
T0016 - Algorithm and
Programming
21
3. Repetition
A number of statements/commands can be repeated
several times using Repetition structure control

Statements/commands will be repeated while the
looping condition is satisfied
(may use DOWHILE ENDDO)
T0016 - Algorithm and
Programming
22
History of C
C evolved from two previous languages, BCPL and B.BCPL was
developed in 1967 by Martin Richards
In 1970, Ken Thompson used B to create early versions of the
UNIX operating system at Bell Laboratories
C language was evolved form B by Dennis Ritchie at Bell
Laboratories and was originally implemented on DEC PDP-11
computer in 1972
The publication in 1978 of Kernighan and Ritchies book, The C
Programming Language
1983 X3J11 technical committee was created to make a
standard of the language
1989 Standard was approved
1999 The standard was updated
C99 is a revised standard for the C programming language
T0016 - Algorithm and
Programming
23
Why Using C
Flexibility
Close to low level machine language yet easy to understand

Portability
Used form micro computer to super computer

A Well Known Programming Language
It is used in many forms of implementations such as O/S, scientific
application, business application, etc.

Supported With a Large Number of Libraries
T0016 - Algorithm and
Programming
24
C Standard Library
When programming in C, youll typically use the following building
blocks:

C Standard Library Functions
Example:
- <math.h> : Mathematical Functions
- <stdio.h> : Input and Output
- <stdlib.h> : Utility Functions
- <string.h> : String Functions
- <time.h> : Time and Date Functions
Functions you create yourself
Functions other people have created and made available to you
T0016 - Algorithm and
Programming
25
C Structure
C language is a structural programming language
It consists of functions
There is no separation between function and
procedure (if you are from Pascal language
background)
Each C program has one main function called main
Program will be started from the first line of the main
function
C language is case sensitive
Every statement should be ended with a semi-colon (;)
T0016 - Algorithm and
Programming
26
C Structure
T0016 - Algorithm and
Programming
main()
{
statements;
}

main()
{
statements;
return(0);
}
void main()
{
statements;
}

int main()
{
statements;
return(0);
}
1.
2.
3.
4.
27
Comments
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:
T0016 - Algorithm and
Programming
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (Hello, BINUSIAN\n);

}
// This program will simply print out a message
28
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
T0016 - Algorithm and
Programming
29
Character
C program is written using ASCII character subset:
- Capital letters AZ
- Lower Case az
- Digit 09
- Special characters !, &, +, \, _, etc.

ASCII
American Standards Committee for Information Interchange
http://www.asciitable.com/
T0016 - Algorithm and
Programming
30
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
T0016 - Algorithm and
Programming
31
Keywords
Keywords/reserved words are words that have special meaning to
the C compiler.
Example:









Keywords added in C99
_Bool _Complex _Imaginary inline restrict

T0016 - Algorithm and
Programming
Keywords
auto
break
case
char
const
continue
default
do

double
else
enum
extern
float
for
goto
if
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
32
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;
T0016 - Algorithm and
Programming
33
Variable
Variable Declaration:
Variable can be declared at every statement block
Block statement or compound statement is statement
exists between { and } sign
Example:
T0016 - Algorithm and
Programming
int x;
int y;
int z;

or:
int x, y, z;

or:

int x; int y; int z;
34
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
T0016 - Algorithm and
Programming
35
Data Type
Data type and its range on TURBO C 2.0 (DOS)
T0016 - Algorithm and
Programming
DATA TYPE SYNTAX MEMORY RANGE
character unsigned char 1 byte 0 to 255
char 1 byte -128 to 127
integer unsigned int 2 byte 0 to 65535
int 2 byte -32768 to 32767
short int 1 byte -128 to 127
unsigned long 4 byte 0 to 4294967295
long 4 byte -2147483648 to 2147483647
float float 4 byte 3.4E-38 to 3.4E+38
double 8 byte 1.7E-308 to 1.7E+308
long double 16 byte 3.4E-4932 to 1.1E+4932
36
Data Type
Beside used in function identifier type as no return
value, keyword void also used as data type in variable.

Void data type: is data type that can be transform into
any data type (will be discussed later in pointer)

T0016 - Algorithm and
Programming
37
Constant
Constant / symbolic constant does not have address (only value) and
its value can not be changed at run time.
Constant type:
C Integer constant = -5
C Floating-point constant = 3.14
C Character constant = 'C' '1' '$'
C Escape sequence = \n \t \''
C 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 constant

T0016 - Algorithm and
Programming
38
Sizeof
sizeof is an operator to find out size of a data
type in C language

Syntax: sizeof expression

Example :
sizeof(int) = 4 => Dev-V (Windows)
sizeof(int) = 2 => Turbo C ver 2.0 (DOS)
T0016 - Algorithm and
Programming
39
Suffix
C provides suffix for floating point constant:
F or f for float data type
L or l for long double data type
Default double data type

Example :
3.14 (double)
3.14f (float)
3.14L (long double)


T0016 - Algorithm and
Programming
40
Suffix
C provides suffix for a constant integer:
U or u for unsigned integer
L or l for long integer
UL or ul or LU or lu for unsigned long integer
Default integer

Example :
174 (integer)
174u (unsigned integer)
174L (long integer)
174ul (unsigned long integer)
T0016 - Algorithm and
Programming
41
Suffix
Some compilers will give warning for differ in data type, as
can be seen from the following example Visual C++:
Example :
float x;
x = 3.14;
warning: truncation from 'const double' to 'float

How to deal with the issue? You may use casting or suffix
float x;
x = (float)3.14; // casting
x = 3.14f; // or suffix

T0016 - Algorithm and
Programming
42
Summary
Algorithm is a procedure for solving a problem in terms
of the actions to be executed
Algorithm development steps consists of: problem
definition, model development, algorithm design, writing
code, and documentation
We can use writing (Structure English and Pseudo-code)
or drawing (Flow Chart) to represent algorithm
Basic Computer Operation: input, output, compute,
store, compare, and repetition (loop)
Structure theorem are sequence, selection, and
repetition
T0016 - Algorithm and
Programming
43
Summary
C is used because: flexibility, portability, well known
programming language, supported with a large number
of libraries
C language is a structural programming language
C language consists of functions
C program has one main function called main
C language is case sensitive
Every statement in C language should be ended with a
semi-colon (;)
T0016 - Algorithm and
Programming
44
References
Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH.
New Jersey. ISBN:978-0-13-705966-9 Chapter 1 & 2
Writing Your First C Program: http://aelinik.free.fr/c/ch02.htm
Data Types and Names in C: http://aelinik.free.fr/c/ch04.htm
T0016 - Algorithm and
Programming
45
References
Paul J. Dietel,Harvey M. Deitel,. 2010. C : how to program. PEAPH.
New Jersey. ISBN:978-0-13-705966-9 Chapter 3
Programming in C: http:// www.cs.cf.ac.uk/Dave/C/
C Language Tutorial:
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_basic
s/
Pseudocode Examples:
http://www.unf.edu/~broggio/cop2221/2221pseu.htm
Computer & Internet Help : Understanding Flowchart Symbols:
http://www.youtube.com/watch?v=xLoL7tlJYws
T0016 - Algorithm and
Programming

46


END
T0016 - Algorithm and
Programming

You might also like