1 / 19

Algoritma dan Pemrograman 2C

Algoritma dan Pemrograman 2C. Scope Variabel dan Memori. Aurelio Rahmadian. Variabel. Variabel digunakan dalam program untuk menyimpan suatu nilai, dan nilai yang ada padanya dapat diubah selama eksekusi program berlangsung. Variabel.

yardan
Download Presentation

Algoritma dan Pemrograman 2C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AlgoritmadanPemrograman 2C Scope VariabeldanMemori Aurelio Rahmadian

  2. Variabel • Variabel digunakan dalam program untuk menyimpan suatu nilai, dan nilai yang ada padanya dapat diubah selama eksekusi program berlangsung.

  3. Variabel • Umumnyadideklarasikandengan format:typevar_name. • typemenandakanjenisvariabel.

  4. CiriVariabel • Made up of letters, digits and underscore. • Must start with a non-digit. • Case sensitive.

  5. Scope Variabel • VariabelOtomatis • VariabelEksternal • VariabelStatis • VariabelDinamis

  6. VariabelOtomatis • Variabelygdidefinisikandalamsuatufungsiberlakusebagaivariabellokalbagifungsidengansifat: • Variabelhanyaakandiciptakanpadasaatfungsidipanggil. • Saatfungsiberakhir, variabelotomatisakanhilang. • Tidakperluadainisialisasipadavariabel. • Hanyadapatdiaksesdalamfungsisaja.

  7. #include <iostream.h> #include <math.h> int jml2bil( int x1, int x2 ) // fungsi standard { // var x1 dan x2 adlhvarotomatis intjml; jml = x1 + x2 ; return(jml); } int main(void) { inty,a,b; cout << "Masukkan 2 bilangan \n"; cin >> a >> b; y = jml2bil(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0); }

  8. VariabelEksternal • Adalahsuatuvariabelygdidefinisikandiluarfungsi, ygdikenalsebagaivariabelglobal. • Catatan: • Usahakanmenggunakanseminimmungkinsuatuvariabel global. • Variabel ini mudah sekali berubah oleh pernyataan penugasaan yang letaknya bisa dimana saja. Ini bisa menimbulkan efek samping yang sulit untuk melacaknya, terutama untuk program yang besar. • Variabelglobal mempunyaidurasiselama program dieksekusiatau, memoriygdigunakanuntukvariabelinidipertahankanselama program dieksekusi.

  9. #include <iostream.h> #include <math.h> IntA=5; // vareksternal int jml2bil( int x1, int x2 ) // fungsi standard { intjml; jml = x1 + x2 + A; return(jml); } int main(void) { inty,a,b; cout << "Masukkan 2 bilangan \n"; cin >> a >> b; y = jml2bil(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0); }

  10. VariabelStatis • Variabelinidapatberupavariabellokalatau global dengansifat: • Jikaberupavariabellokalmakavariabeltetaphanyadapatdiaksespadafungsiygmendefinisikannya. • Variabeltidakhilangsaatberakhir. • Inisialisasihanyadilakukansekali. • Jikaberupavar global dapatdiaksesdarisemua file ygdidefinisikan (hatihatidalammenggunakannya). • Caranyadenganmenambahkatastatic type-data var-name.

  11. #include <iostream.h> intfncstatis(int); void main( ) { intin,out; in = 1 ; while(in!=0) { cout<< "Enter input value:"; cin>>in; out=fncstatis(in); cout<<"Result:"<< out; } cout<<" End of Program"<< out; } intfncstatis(int x) { static int a=0; static int b=0; a++; b=b+x; return(b/a); }

  12. Memori • Memoridapatmenyimpan program dan data. • Setiap data merupakankumpulandaribeberapa bit. • Umumnyamemoridiaturdalamkumpulan 8 bit. • Setiap byte memiliki address.

  13. MEMORY Address 0 1 2 3 4 5 81345 81346 81347 PenggambaranMemori CPU

  14. Program Sederhana # Instruction 1 Set memory[801] to hold 00000001 2 Set memory[802] to hold 00000000 3 If memory[802] = 10 jump to instruction #8 4 increment memory[802] 5 set memory[803] to 2 times memory[801] 6 put memory[803] in to memory[801] 7 jump to instruction #3 8 print memory[801]

  15. Penggambaran Program MEMORY Address 0 1 2 3 4 5 801 802 803 Instruction #1 Instruction #2 CPU Instruction #3 Instruction #4 Instruction #5 Instruction #6

  16. Human vs Machine Programs • The computer can only understand the bits (the encoded program) • Humans don't like to deal with bits, so they developed english-like abbreviations for programs. Machine Language Assembly Language

  17. Assembly & Machine Language Assembly Language Machine Language ST 1,[801] ST 0,[802] TOP: BEQ [802],10,BOT INCR [802] MUL [801],2,[803] ST [803],[801] JMP TOP BOT: LD A,[801] CALL PRINT 00100101 11010011 00100100 11010100 10001010 01001001 11110000 01000100 01010100 01001000 10100111 10100011 11100101 10101011 00000010 00101001 11010101 11010100 10101000 10010001 01000100

  18. High Level Language x=1; i=0; while (i!=10){ i++; x=x*2; } printf("%d",x); set memory[801] to hold 00000001 set memory[802] to hold 00000000 if memory[802] = 10 jump to instruction #8 increment memory[802] set memory[803] to 2 times memory[801] put memory[803] in to memory[801] jump to instruction #3 print memory[801] }

  19. Tata LetakMemori • STACK • HEAP • DATA • KODE

More Related