i ntroduction algorithm and data structure by: muhammad zidny naf’an

24
INTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

Upload: posy-oconnor

Post on 23-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

INTRODUCTIONAlgorithm and Data Structure

By: Muhammad Zidny Naf’an

Page 2: I NTRODUCTION 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

Page 3: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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)

Page 4: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 5: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

DATA STRUCTURE

A data structure is a way to store and organize data in order to facilitate access and modifications.

Page 6: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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..

Page 7: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 8: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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/)

Page 9: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 10: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 11: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 12: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 13: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 14: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

VARIABLE

Variable the identifier that used for saving the value and changeable

Must be declared first

Variable Declaration:float celcius, fahrenheit;

Page 15: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

CONSTANT

Constant a fixed value Write after header file

Using #defineex: #define i 2

Or keyword const. ex: const int i = 2;

Page 16: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

PREPROCESSOR DIRECTIVE #DEFINE

For expressing a constant or keywordexample of use #define to define keyword:

Page 17: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

PREPROCESSOR DIRECTIVE #DEFINE

example of use #define to define a constant:

Page 18: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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)

Page 19: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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.

Page 20: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 21: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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

Page 22: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

EXERCISE

Retwrite this code and find the errors

include <iostream.h>

using namespace;

int main()

char ch = ‘a; cout << "karakter \'" << ch "\' bernilai " << (int) ch;}

Page 23: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

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.

Page 24: I NTRODUCTION Algorithm and Data Structure By: Muhammad Zidny Naf’an

CHAPTER 1 - END

Q&A