Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 44

Subject : COMP6047

ALGORITHM AND PROGRAMMING


Year : 2018

Algorithm & Programming and


Introduction to C Programming
Textbooks
• Main Textbook
Paul Deitel & Harvey Deitel. (2016). C how to program : with an
introduction to C++. 08. Pearson Education. Hoboken. ISBN:
9780133976892.

• Additional Textbook
Jeri R. Hanly , Elliot B. Koffman. 2009. Problem Solving and
Program Design in C. ADWES. Boston, Massachusetts, USA.
ISBN:978-0321535429

COMP6047 - Algorithm and Programming 2


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.

COMP6047 - Algorithm and Programming 3


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

COMP6047 - Algorithm and Programming 4


Learning Outcomes
At the end of this session, student will be able to:
• Define algorithm theory and design (LO1)

COMP6047 - Algorithm and Programming 5


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.

COMP6047 - Algorithm and Programming 6


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

COMP6047 - Algorithm and Programming 7


Algorithm Development Steps

PROBLEM PROCESS SOLUTION

Algorithm Source Code Executable Code

Problem Model Algorithm Writing


Writing Code
code
Definition Development Design
COMPILE

Syntax Err

Executable code:
=> Run

Output Err

COMP6047 - Algorithm and Programming Documentation


8
Representing Algorithm
• How to develop an algorithm?
We can use:
– Writing
Structure English and Pseudo-code.
– Drawing
Flow Chart

COMP6047 - Algorithm and Programming 9


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.

COMP6047 - Algorithm and Programming 10


Pseudo-code
Basic Computer Operation:
1. Input
2. Output
3. Compute
4. Storing value to an identifier (Store)
5. Compare (Selection)
6. Repetition (Loop)

COMP6047 - Algorithm and Programming 11


Pseudo-code Example
Example : Algorithm using a calculator to sum values
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

COMP6047- Algorithm and Programming 12


Pseudo-code Example
Example : Algorithm to count average grade of a class
Start
Set total to zero
Set grade counter to one
While grade counter is less than or equal to ten
Input next grade
Add grade into total
Add one to grade counter
Set class average equal to total divided by ten
Print the class average.
End

COMP6047- Algorithm and Programming 13


Flow Chart

COMP6047 - Algorithm and Programming 14


Flow Chart Example
Start

Input a,b,c

d = b^2 – 4ac

d<0 Y

T
x1=(-b+sqrt(d))/2a Print message
x2 =(-b-sqrt(d))/2a “Imaginary”

Print: x1, x2

Stop

COMP6047 - Algorithm and Programming 15


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

COMP6047 - Algorithm and Programming 16


Structure Theorem
Structure theorem which makes the computer programming
possible using only three control structure, which are:
1. Sequence
2. Selection
3. Repetition

COMP6047 - Algorithm and Programming 17


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

COMP6047 - Algorithm and Programming 18


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)

COMP6047 - Algorithm and Programming 19


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)

COMP6047 - Algorithm and Programming 20


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 from 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 Ritchie’s 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 21


Why Using C
• Flexibility
Close to low level machine language yet easy to understand

• Portability
Used from 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

COMP6047 - Algorithm and Programming 22


C Standard Library
When programming in C, you’ll 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

COMP6047 - Algorithm and Programming 23


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 (;)

COMP6047 - Algorithm and Programming 24


C Structure
main() main()
1. { 3. {
statements; statements;
} return(0);
}

void main() int main()


2. { 4. {
statements; statements;
} return(0);
}

COMP6047 - Algorithm and Programming 25


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:
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (“Hello, BINUSIAN\n”);

}
// This program will simply print out a message

COMP6047 - Algorithm and Programming 26


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

COMP6047 - Algorithm and Programming 27


Character
• C program is written using ASCII character subset:
- Capital letters A…Z
- Lower Case a…z
- Digit 0…9
- Special characters ‘!’, ‘&’, ‘+’, ‘\’, ‘_’, etc.

• ASCII
American Standard Code for Information Interchange
http://www.asciitable.com/

COMP6047 - Algorithm and Programming 28


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

COMP6047 - Algorithm and Programming 29


Keywords
• Keywords/reserved words are words that have special meaning to the C
compiler.
Keywords
• Example:
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 added in C99


_Bool _Complex _Imaginary inline restrict
COMP6047 - Algorithm and Programming 30
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;

COMP6047 - Algorithm and Programming 31


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:

int x; int y; int z;


COMP6047 - Algorithm and Programming 32
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

COMP6047 - Algorithm and Programming 33


Data Type
DATA TYPE SYNTAX MEMORY RANGE

character char 1 byte -128 to 127

unsigned char 1 byte 0 to 255


Data type and its range on TURBO C 2.0 (DOS)
integer int 4 byte –2,147,483,648 to 2,147,483,647

unsigned int 4 byte 0 to 4,294,967,295

short int 2 byte –32,768 to 32,767

unsigned short int 2 byte 0 to 65,535

long int 4 byte –2,147,483,648 to 2,147,483,647

unsigned long int 4 byte 0 to 4,294,967,295

long long 8 byte –9,223,372,036,854,775,808 to


9,223,372,036,854,775,807
unsigned long long 8 byte 0 to 18,446,744,073,709,551,615

float float 4 byte 3.4E-38 to 3.4E+38

double 8 byte 1.7E-308 to 1.7E+308

long double 8 byte 1.7E-308 to 1.7E+308


COMP6047 - Algorithm and Programming 34
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)

COMP6047 - Algorithm and Programming 35


Constant
Constant / symbolic constant does not have 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 constant

COMP6047 - Algorithm and Programming 36


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)

COMP6047 - Algorithm and Programming 37


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)

COMP6047 - Algorithm and Programming 38


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)

COMP6047 - Algorithm and Programming 39


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

COMP6047 - Algorithm and Programming 40


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 (selection), and repetition (loop)
• Structure theorem are sequence, selection, and repetition

COMP6047 - Algorithm and Programming 41


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 (;)

COMP6047 - Algorithm and Programming 42


References
• Paul Deitel & Harvey Deitel. (2016). C how to program : with an
introduction to C++. 08. Pearson Education. Hoboken. ISBN:
9780133976892. 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
• Programming in C: http:// www.cs.cf.ac.uk/Dave/C/
• Pseudocode Examples: http://www.unf.edu/~
broggio/cop2221/2221pseu.htm
• C Language Tutorial:
http://www.physics.drexel.edu/courses/Comp_Phys/General/C_b
asics/

COMP6047 - Algorithm and Programming 43


END

COMP6047 - Algorithm and Programming 44

You might also like