Masalah utama. Sub Masalah A. Sub Masalah B. Sub Masalah C. Masalah Besar
. Sub Masalah A1. Sub Masalah A2. B. A. C. A1. A2. Strategi umum dalam.
Analisis Top Down input & Output Runtunan Pertemuan 3
Overview o o o o o o o
Deskripsi Tujuan Instruksional Referensi Overview Library Header Analisa Top Down Input & Output Runtunan
IF-UTAMA
Ver/Rev : 1/0
III - 2
Deskripsi Pada pertemuan ini akan dipelajari mengenai Analisa Top Down, input & Output dalam Bahasa C/C++, serta runtunan
IF-UTAMA
Ver/Rev : 1/0
III - 3
1
Tujuan Instruksional Mahasiswa diharapkan dapat : o Menjelaskan analisis Top-Down o Menjelaskan proses input dan output, serta cara penulisannya dalam program o Membedakan proses input dan output o Menjelaskan proses runtunan/sequence o Menggunakan analisis Top-Down o Menggunakan proses input dan output o Menggunakan proses runtunan/sequence
IF-UTAMA
Ver/Rev : 1/0
III - 4
Referensi
1. Deitel, H.M. and Deitel, P.J., “C++ How to Program, 2nd Edition”, Prentice Hall, 1994 (Bab 3 dan 12) 2. Deitel, H.M. and Deitel, P.J., “C How to Program, 4nd Edition”, Prentice Hall, 2004 (bab 5,8,9 dan 21) 3. Herianto,“Presentasi Pemrograman Terstruktur.ppt”,2004
IF-UTAMA
Ver/Rev : 1/0
III - 5
Header File
2
Header Files Common Usages: o Creates an informal module interface (No relation to Java interfaces) o Provides documentation o Each module usually has a header file o Often include the following: function prototypes struct and class declarations typedefs global variable declarations Lots of (high-level) comments o Abstracts code for optimization (less common) IF-UTAMA
Ver/Rev : 1/0
III - 7
Preprocessor Overview The preprocessor is a lexical macro engine run before the compiler sees the code o Performs lexical transforms of text It does not understand C/C++ There is no concept of types o Transforms are based on lexical substitution Think Search and Replace o Preprocess directives (commands) start with ‘#’ Each directive goes on its own line The # must be the first character on a line. o No semicolons!!!!!!!!!!!! Newlines end a preprocessor directive, no a ‘;’. IF-UTAMA
Ver/Rev : 1/0
III - 8
#define o #define Tag Substitution Replaces all following occurrences of Tag with Substitution The Substitution may be the empty string Does not replace Tag if it is inside quotation marks o #define Tag(x,y,z) Substitution Creates a Function-like macro Tag The Substitution may refer to the parameters x, y, or z Only items of the form Tag(x,y,z) will be replaced (Just plain Tag will be ignored) #define MAX_SIZE 80 #define GET_FIELD(a,b) a->b int ar[80]; point->xCoord “lalala”->f03j?
int ar[MAX_SIZE]; GET_FIELD(point, xCoord); GET_FIELD(“lalala", f03j?); IF-UTAMA
Ver/Rev : 1/0
III - 9
3
The #if, and its variants o #if constant-expression Include the following block of text if constant-expression is non-zero o #ifdef Tag Include the following block if Tag is currently defined (via an earlier #define) o #ifndef Tag Include the following block if Tag is currently not defined (via an earlier #define) o #else Used to delimit the else clause in a preprocessor if statement o #elif constant-expression Used to create an else-if clause in a preprocessor if statement o #endif Delimits the end of a preprocessor if statement. There should be exactly one of these for each #if, #ifdef, and #ifndef. IF-UTAMA
Ver/Rev : 1/0
III - 10
#if examples The DPRINT macro when INCLUDE_DEBUG_PRINT is defined, evaluates to printing code. Otherwise, it evaluates to the empty string. #ifdef INCLUDE_DEBUG_PRINT # define DPRINT(msg) \ fprintf(stderr, “DBG: %s”, msg); #else # define DPRINT(msg) #endif Windows calls the open and close functions, _open, and _close respectively. This is annoying, so let’s fix it with preprocessor macros. #ifdef WIN32 # define open _open # define close _close #endif IF-UTAMA
Ver/Rev : 1/0
III - 11
#include #include essentially copies and pastes the given file into the current line o There are two forms of #include o They differ in the order in which directories are search for the given file o The search order is implementation defined o Here is the general pattern for different compilers: #include Searches the “include path,” then the current directory #include “filename” Searches the current directory, then the “include path” IF-UTAMA
Ver/Rev : 1/0
III - 12
4
#undef, #error, #warning o #undef Tag Removes definition of a Tag. (undoes a #define) o #error message Causes compilation to stop at this line with the given error message. Often this is used with #if blocks to indicate an invalid set of #define macros. o #warning message Causes the compiler to output an warning at this line with the given message. Can be used as strong reminders that a file needs work. o #pragma option Used to pass an option to the compiler. Almost all #pragma commands are compiler-dependent.
IF-UTAMA
Ver/Rev : 1/0
III - 13
#pragma, #line o #line number Used to change what line number that the compiler thinks it is on. This is often seen in: Computer Generated Code (from like flex, bison, or something) Code outputed by the preprocessor #line to allows programs to make the compiler’s error messages to correspond to the original source file’s line numbers rather than the generated source file’s line numbers.
IF-UTAMA
Ver/Rev : 1/0
III - 14
Analisis Top Down
5
Top Down Masalah Besar
Sub Masalah A
Sub Masalah A1
Sub Masalah B
Sub Masalah C
Sub Masalah A2
Masalah utama
B
C A1
A A2
Strategi umum dalam penyelesaian masalah besar; kompleks; rumit IF-UTAMA
Ver/Rev : 1/0
III - 16
Contoh Top Down Sistem Informasi Akademis
Mahasiswa
Perkuliahan
Dosen
Entry data
Entry data
Entry data
Hapus data
Hapus data
Hapus data
Laporan data
Laporan data
Laporan data
IF-UTAMA
Ver/Rev : 1/0
III - 17
Implementasi : Metode Modular Bagian Utama …….. Call A ……..
A
A1
……. Call A1 …….
……. …….
Call A2 ……..
B Call B ……..
A2
……. …….
……. …….
C
Call C ……..
……. …….
Dapat diterapkan secara : - Internal : sub program, procedure, function - Eksternal : file unit, header, modul
IF-UTAMA
Ver/Rev : 1/0
III - 18
6
Sequence/Runtunan
Simple Sequence
IF-UTAMA
Ver/Rev : 1/0
III - 20
Sequence o Instruksi dikerjakan secara berurutan. dari atas ke bawah step
1
IF-UTAMA
Instruksi
input jmlBrg, hrgSat
2
harga ← jmlBrg x hrgSat
3
Print harga
Ver/Rev : 1/0
III - 21
7
Contoh Sequence (1) o Program akan dikerjakan dengan input: 2, 1500 step
Instruksi
1
input jmlBrg, hrgSat
2
harga ← jmlBrg x hrgSat
3
Print harga
step
Variabel jmlBrg
hrgSat
IF-UTAMA
Output harga
Ver/Rev : 1/0
III - 22
Contoh Sequence (2) o Mulai dengan langkah-1: input 2, 1500 step
Instruksi
1
input jmlBrg, hrgSat
2
harga ← jmlBrg x hrgSat
3
Print harga
step 1
Variabel jmlBrg
hrgSat
2
1500
IF-UTAMA
Output harga
Ver/Rev : 1/0
III - 23
Contoh Sequence (3) o Langkah-2: Hitung perkalian, simpan hasilnya di variabel harga step
Instruksi
1
input jmlBrg, hrgSat
2
harga ← jmlBrg x hrgSat
3
Print harga
step
IF-UTAMA
Variabel jmlBrg
hrgSat
1
2
1500
2
2
1500
Ver/Rev : 1/0
Output harga
3000
III - 24
8
Contoh Sequence (4) o Langkah-3: Tampilkan isi variabel harga step
Instruksi
1
input jmlBrg, hrgSat
2
harga ← jmlBrg x hrgSat
3
Print harga
step
Variabel jmlBrg
hrgSat
Output harga
1
2
1500
2
2
1500
3000
3
2
1500
3000
IF-UTAMA
3000
Ver/Rev : 1/0
III - 25
Input & Output
I/O C-style C-style IO is an acquired taste. Learn to like it. Basic functions: o printf, scanf, fprintf, fscanf, sprintf, sscanf, etc. o gets, puts, getc, putc, getchar o read, write, fread, fwrite We will cover the basics of the “formated” family of functions (printf, scanf, etc).
IF-UTAMA
Ver/Rev : 1/0
III - 27
9
printf printf(char *format_string, ...); fprintf(FILE*, char *format_string, ...); snprintf(char* buf, size_t n, char *format_string, ...); o In C, all devices are treated like files o Three standard files are: stdin Often the keyboard stdout Often the text console stderr Often the text console o printf(....) is fprintf(stdout, ....) o The format string is a pattern for the output; it describes how to display the arguments to printf. o Snprintf write to the string “buf”. The variable n specifies the size of the buffer. o printf returns the number of characters written IF-UTAMA
Ver/Rev : 1/0
III - 28
format string o Format strings are normal strings with embedded “conversion specifications” which are placeholders for arguments o Conversion specifications are a ‘%’ and a letter with an optional set of arguments in between the ‘%’ and letter. o To print a ‘%’, you need to write ‘%%’ Example: printf(“Here is a number: %d\n”, 10); %d is the conversion specification for signed integers.
IF-UTAMA
Ver/Rev : 1/0
III - 29
Conversion Specifications Converion specifications tell how to translate a data value into a string Conversion Specifications: o %d, %i -- signed integer o %u -- unsigned integer o %f -- floating point number o %c -- character o %s -- string o %x -- hexadecimal value o %p -- pointer
Options: o l -- long (32-bit value) o ll -- long long (64-bit value) o n -- field width of n digits o .n -- precision of n digits o 0 -- fill unused field with 0s
There are many more! Read man pages, or Google it.
IF-UTAMA
Ver/Rev : 1/0
III - 30
10
printf quiz! Figure out the output of the following: o printf(“%.3f rounded to 2 decimals is %.2f\n”, 2.325, 2.325); o printf(“%d in hex is: %04x\n”, 24, 24); o printf(“Quizzes are fun, ja?\n”);
IF-UTAMA
Ver/Rev : 1/0
III - 31
scanf scanf(char *format_string, ...); fscanf(FILE*, char *format_string, ...); sscanf(char*, char *format_string, ...); o scanf(....) is fscanf(stdin, ....) o All arguments ot scanf must be pointers (or arrays) o scanf does almost no size checks. It is easy to get a buffer overflow here. Make sure you use a field length specifier with the %s conversion specifer!!! o scanf returns the number of items read.
IF-UTAMA
Ver/Rev : 1/0
III - 32
scanf Examples int items_read; Read a number: int num; items_read = scanf(“%d”, &num); Read a character: char ch; items_read = scanf(“%c”, &ch);
always check the return value of scanf
Read a string of max length, 79 chars: char buf[80]; buf[79]=‘\0’; // Ensure a terminating NULL. items_read = scanf(“%79s”, buf); Read number after pattern of “a:”: int num; items_read = scanf(“a:%d”, &num);
IF-UTAMA
Ver/Rev : 1/0
III - 33
11
I/O C++-style C++-style IO is easier for simple stuff Basic classes: o iostream (cout, cin, cerr) o ostringstream, istringstream cout