0% found this document useful (0 votes)
32 views

PPT01-Introduction To Algorithm and Programming

Uploaded by

foobarworm
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

PPT01-Introduction To Algorithm and Programming

Uploaded by

foobarworm
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Course : Algorithm and Programming

Effective Period : 2022

TOPIC 01

INTRODUCTION TO ALGORITHM AND


PROGRAMMING
LEARNING OUTCOMES

At the end of this session, students will be able to:


• LO 1: Apply syntax and functions in C language for problem-solving
OUTLINE

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

Rise and Shine Algorithm


o Get out of bed
o Take off pajamas
o Take a shower
o Get dressed
o Eat breakfast
o Carpool to work
ALGORITHM DEVELOPMENT STEPS
PROBLEM PROCESS SOLUTION

Algorithm Source Code Executable Code

Problem Definition Model Development Algorithm Design Writing code

COMPILE

Syntax Err

Executable code: => Run

Output Err

Pic 1.1. Intro tp data, information and knowledge.


Source : C how to program. Paul J. Deitel & Harvey. Deitel (2022). Documentation
REPRESENTING ALGORITHM

o How to develop an algorithm?


We can use:
• Writing
Structure English and Pseudo-code.
• Drawing
Flow Chart
PSEUDO-CODE

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)

Basic Computer Operation:


1. Input
2. Output
3. Compute
4. Storing value to an identifier (Store)
5. Compare (Selection)
6. Repetition (Loop)
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
FLOW CHART

o The flowchart is a graphical representation of an algorithm or of a portion of an algorithm.


o You draw flowcharts using certain special-purpose symbols such as rectangles, diamonds,
rounded rectangles, and small circles, connected by arrows called flowlines.
FLOW CHART EXAMPLE
STRUCTURE THEOREM

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

When programming in C, you’ll typically use the following building blocks:

o 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
o Functions you create yourself
o Functions other people have created and made available to you
C STRUCTURE

o C language is a structural programming language


o It consists of functions
o There is no separation between function and procedure (if you are from a Pascal language
background)
o Each C program has one main function called the main
o Program will be started from the first line of the main function
o C language is case sensitive
o Every statement should be ended with a semi-colon (;)
C STRUCTURE (CONT)

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

void main() int main()


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

o Used for readability of the program


o Not accounted as a command/statement by the compiler
o Using /* and */
o Using // at the beginning of line for one line comment
o Example:
/*--------------------------
My First Program
--------------------------*/
#include<stdio.h>
void main(){
printf (“Hello, BINUSIAN\n”);

}
// This program will simply print out a
message
ESCAPE SEQUENCES

o \a bell, alert, system beep


o \b back space
o \t horizontal tab
o \n new line, line feed
o \v vertical tab
o \r carriage return
o \’ single quote
o \” double quote
o \\ backslash
o \xdd hexadecimal notation
o \ddd octal notation
CHARACTER

o C program is written using ASCII character subset:


- Capital letters A…Z
- Lower Case a…z
- Digit 0…9
- Special characters ‘!’, ‘&’, ‘+’, ‘\’, ‘_’, etc.

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 Keywords added in C99


_Bool _Complex _Imaginary inline restrict
VARIABLE

o Identifier for storing data/information


o Each variable has its name, address (L-value), type, size and data (R-value)
o Data or variable value can be modified at run time
o Declaration format:
<data type> <variable name>;
<data type> <variable name> = <initial value>;
o Example:
int a, b, c, total;
float salary, bonus;
int num_students = 20;
VARIABLE (CONT)

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:

int x; int y; int z;


DATA TYPE

o 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 (CONT)
DATA TYPE SYNTAX MEMORY RANGE
character char 1 byte -128 to 127
unsigned char 1 byte 0 to 255
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
DATA TYPE (CONT)

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 sizeof is an operator to find out size of a data type in C language


o Syntax: sizeof expression

o Example :
sizeof(int) = 4 => Dev-V (Windows)
sizeof(int) = 2 => Turbo C ver 2.0 (DOS)
SUFFIX

o 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

o Example :
• 3.14  (double)
• 3.14f  (float)
• 3.14L  (long double)
SUFFIX (CONT)

o 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

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 Algorithm is a procedure for solving a problem in terms of the actions to be executed


o Algorithm development steps consist of problem definition, model development,
algorithm design, writing code, and documentation
o We can use writing (Structure English and Pseudo-code) or drawing (Flow Chart) to
represent the algorithm
o Basic Computer Operation: input, output, compute, store, compare (selection), and
repetition (loop)
o Structure theorem are sequence, selection, and repetition
o C is used because flexibility, portability, well-known programming language, supported by
a large number of libraries
o C language is a structural programming language
o C language consists of functions
o C program has one main function called main
o C language is case sensitive
o Every statement in C language should be ended with a semi-colon (;)
ThankYOU...
REFERENCES

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

You might also like