i ntroduction algorithm and data structure by: muhammad zidny naf’an
TRANSCRIPT
INTRODUCTIONAlgorithm 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..
7BASICS OF A TYPICAL C++ ENVIRONMENT
Phases of C++ Programs:1. Edit2. Preprocess3. Compile4. Link5. Load6. Execute
Loader
PrimaryMemory
Program is created inthe editor and storedon disk.
Preprocessor programprocesses the code.
Loader puts programin memory.
CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.
CompilerCompiler createsobject code and storesit on disk.
Linker links the objectcode with the libraries,creates a.out andstores it on disk
Editor
Preprocessor
Linker
CPU
PrimaryMemory
.
.
.
.
.
.
.
.
.
.
.
.
Disk
Disk
Disk
Disk
Disk
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++ STRUCTUREcomment
Header file
Standard library
Variable declaration
Display in monitor
Start of function main
End of function main
Input value from keyboard
Function name with integer return
Return value
Hold the program until return key pressed
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 intshort struct unsigned breakcontinue else for long signedswitch void case default enumgoto register sizeof typedef volatilechar do extern if return staticunion while
KEYWORD IN C++ ONLY
asm dynamic_cast namespacereinterpret_cast trybool explicitnew static_cast typeidcatch falseoperator template typenameclassfriend private this usingconst_cast inline publicthrow virtualdelete mutableprotected true wchar_t
IDENTIFIERIdentifier 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 not allowed used, ex: character (‘!’, ‘-’, etc)
• Case sensitive
• Not same with keyword.
• Easy to read and easy for understanding
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 #defineex: #define i 2
Or keyword const. ex: const int i = 2;
PREPROCESSOR DIRECTIVE #DEFINE
For expressing a constant or keywordexample 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 devicecontoh : cin (character in)
Copyright © 2003 Pearson Education, Inc. Slide 19
RUNNING A C++ PROGRAMC++ 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 20
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.”
1.4
Copyright © 2003 Pearson Education, Inc. Slide 21
PROGRAM ERRORS
Syntax errorsViolation of the grammar rules of the
languageDiscovered by the compiler
Error messages may not always show correct location of errors
Run-time errorsError conditions detected by the computer
at run-time Logic errors
Errors in the program’s algorithmMost difficult to diagnoseComputer does not recognize an error
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