Introduction Algorithm and Data Structure By

Download Report

Transcript Introduction Algorithm and Data Structure By

INTRODUCTION
Algorithm and Data Structure
By: Muhammad Zidny Naf’an
PRACTICE ALGORITHM AND DATA
STRUCTURE
Credit: 1
 Lecture: Muhammad Zidny Naf’an
 Email: [email protected]
 Phone: 085281668860
 Blog: derlaz.wordpress.com or
myretrieval.blogspot.com

PRACTICE ALGORITHM AND DATA
STRUCTURE
Programming language using C++
 IDE using Dev C++
(http://www.bloodshed.net/dev/devcpp.html)
 References:







Rinaldi Munir. 2005. Algoritma dan Pemrograman dalam
Bahasa Pascal dan C. Penerbit Informatika, Bandung.
Antony Pranata. 2000. Algoritma dan Pemrograman. Penerbit
J&J Learning, Jogjakarta
Jogiyanto Hartono. 2003. Konsep Dasar Pemrograman Bahasa
C. Jogjakarta: Penerbit Andi.
Abdul Kadir. 1995. Pemrograman C++. Jogjakarta: Penerbit
Andi.
Thomas H. Cormen, el. 2002. Introduction to Algorithm. MIT
Press.
Pearson Education. (www.cs.ucr.edu)
ALGORITHM
An algorithm is any well-defined computational
procedure that takes some value, or set of values,
as input and produces some value, or set of
values, as output.
 An
algorithm is thus a sequence of
computational steps that transform the input
into the output.
 We can also view an algorithm as a tool for
solving a well-specified computational problem

DATA STRUCTURE

A data structure is a way to store and organize
data in order to facilitate access and
modifications.
C++ LANGUAGE
Extend from C Language (diciptakan oleh Brian
W. Kernighan dan Dennis M. Ritchie in 1970,Bell
Telephone Laboratories Inc. now AT&T Bell
Laboratories.
 C++ created by Bjarne Stroustup one dekade
after C at AT&T Bell Laboratories.
 More compatible than C
 Using C as a base because portability of C
 C++ Compiler: Dev C++, Borland C++, Turbo
C++, etc..

7
BASICS OF A TYPICAL C++ ENVIRONMENT
Editor
Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute
Preprocessor
Compiler
Linker
Disk
Program is created in
the editor and stored
on disk.
Disk
Preprocessor program
processes the code.
Disk
Compiler creates
object code and stores
it on disk.
Disk
Primary
Memory
Linker links the object
code with the libraries,
creates a.out and
stores it on disk
Loader
Disk
Loader puts program
in memory.
..
..
..
Primary
Memory
CPU
..
..
..
CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
DEV C++
Dev-C++, developed by Bloodshed Software, is a
fully featured graphical IDE (Integrated
Development Environment),
 which is able to create Windows or console-based
C/C++ programs using the MinGW compiler
system. MinGW (Minimalist GNU* for Windows)
uses GCC (the GNU g++ compiler collection),


(http://www.uniqueness-template.com/devcpp/)
C++ STRUCTURE
comment
Header file
Standard library
Function name with integer return
Start of function main
Variable declaration
Display in monitor
Input value from keyboard
Hold the program until return key pressed
Return value
End of function main
KEYWORD
Keyword  set of words that have used by
compiller
 C++ has 32 keywords where C has to and C++
has 30 new keywords for it self

KEYWORD IN C AND C++
auto const double float int
short struct unsigned break
continue else for long
signed
switch void case default enum
goto register sizeof typedef
volatile
char do extern if return static
union while
KEYWORD IN C++ ONLY
asm dynamic_cast namespace
reinterpret_cast trybool explicit
new static_cast typeidcatch false
operator template typenameclass
friend
private this
usingconst_cast inline public
throw
virtualdelete
mutable
protected true
wchar_t
IDENTIFIER
Identifier is the name that use by programmer for
declare variable, constant, function, or label
Rule for identifier:
•
•
•
Length less than 32 characters
First character must a letter or underscore ( _ ), ex:
halo123 _123halo
The special character
character (‘!’, ‘-’, etc)
not
allowed
•
Case sensitive
•
Not same with keyword.
•
Easy to read and easy for understanding
used,
ex:
VARIABLE
Variable  the identifier that used for saving the
value and changeable
Must be declared first
Variable Declaration:
float celcius, fahrenheit;
CONSTANT
Constant a fixed value
 Write after header file



Using #define
ex: #define i 2
Or keyword const.
ex: const int i = 2;
PREPROCESSOR DIRECTIVE #DEFINE

For expressing a constant or keyword
example of use #define to define keyword:
PREPROCESSOR DIRECTIVE #DEFINE
example of use #define to define a constant:
SIMPLE INPUT/OUTPUT STATEMENT
Input Statement is a statement or function
used for reading data from input device
(keyboard/mouse),
ex: cout (character out)
 Output Statement adalah is a statement or
function used for writing data to output device
contoh : cin (character in)

RUNNING A C++
PROGRAM
C++
source code is written with
a text editor
The compiler on your system
converts source code to object
code.
The linker combines all the
object code into an executable
program.
Copyright © 2003 Pearson Education, Inc.
Slide 19
1.4
TESTING AND DEBUGGING

Bug


A mistake in a program
Debugging


Eliminating mistakes in programs
Term used when a moth caused a failed relay
on the Harvard Mark 1 computer. Grace Hopper
and other programmers taped the moth in logbook
stating:
“First actual case of a bug being found.”
Copyright © 2003 Pearson Education, Inc.
Slide 20
PROGRAM ERRORS
 Syntax


errors
Violation of the grammar rules of the language
Discovered by the compiler

Error messages may not always show correct location
of errors
 Run-time

Error conditions detected by the computer at
run-time
 Logic



errors
errors
Errors in the program’s algorithm
Most difficult to diagnose
Computer does not recognize an error
Copyright © 2003 Pearson Education, Inc.
Slide 21
EXERCISE

Retwrite this code and find the errors
include <iostream.h>
using namespace;
int main()
char ch = ‘a;
cout << "karakter \'" << ch
}
"\' bernilai " << (int) ch;
EXERCISE
 There
are two variables, A and B. A’s
value s 10, and B’s value is 20. make
program that will swap the value of
both. The result are A = 20 and B =
10.
CHAPTER 1 - END

Q&A