Saturday, December 24, 2022

Algoritma dan Struktur Data Pertemuan 11

Assalamualaikum wr.wb                                                                                                                   

Nama: Muhamad Syarif Ramadhan

Nim  : 3420227001
Prodi: Teknik Informatika


Contoh 1

                                                Lampiran Contoh 1 sofware Borland C++


 

 /* Program Membuat Garis Menggunakan Fungsi */


#include "stdio.h"         //Nama  : Muhamad Syarif Ramadhan

#include "iostream.h"      //NIM   : 3420227001

#include "conio.h"

#include "iomanip.h"


void garis() {

    printf ("\t----------------------------------------------------\n");

}


/* Program Utama */

void main() {

   garis(); //Memanggil Fungsi Garis

   cout << "\t|\t Belajar Algoritma Dan Strutur Data. \t   |" << endl;

   garis(); //Memanggil Fungsi Garis

   int a[5];

   a[0]=9; a[1]=90; a[2]=900; a[3]=9000; a[4]=90000;

   printf ("\t| Nilai A = %d\n", a[0]);

   gotoxy (60,4); cout << "|" << endl;

   printf ("\t| Nilai B = %d\n", a[1]);

   gotoxy (60,5); cout << "|" << endl;

   printf ("\t| Nilai C = %d\n", a[2]);

   gotoxy (60,6); cout << "|" << endl;

   printf ("\t| Nilai D = %d\n", a[3]);

   gotoxy (60,7); cout << "|" << endl;

   printf ("\t| Nilai E = %d\n", a[4]);

   gotoxy (60,8); cout << "|" << endl;

   getch();

}

Contoh 2

                                                Lampiran Contoh 2 sofware Borland C++


/*

***************************************

Penggunaan Prototype Pada Fungsi

***************************************

*/

#include "stdio.h"        //Nama  : Muhamad Syarif Ramadhan

#include <conio.h>        //NIM   : 3420227001

#include "iostream.h"     

#include <iomanip.h>


//Prototype Fungsi

float total(int hrg, int jml);


void garis() {

   cout << "===================================================================================" << endl;

}


void judul() {

   garis();

   cout << "\t\t\t\t SHAFA STORE PROGRAM" << endl;

   garis();

}


void main() {

   char name[20], goods[20];

   int price, jumbel;

   judul();

   cout << " Enter the Buyer's Name : "; cin >> name;

   cout << " Name of Goods          : "; cin >> goods;

   cout << " Price                  : "; cin >> price;

   cout << " Purchase Amount        : "; cin >> jumbel;

   garis();

   cout << " Thankyou " << name << " have shopped at our store." << endl;

   cout << " What you Buy is " << goods << " with price Rp. " << price << endl;

   cout << " The Amount you Buy is " << jumbel << " so Pay is";

   printf (" Rp. %.2f", total(price, jumbel));

   cout << endl;


   getch();

}


float total (int hrg, int jml) {

   return hrg * jml;

}


Contoh 3

                                                Lampiran Contoh 3 sofware Borland C++


/* Contoh Pemanggilan Fungsi By Value */


#include "stdio.h"           //Nama  : Muhamad Syarif Ramadhan

#include "iostream.h"        //NIM   : 3420227001

#include "conio.h"

#include "iomanip.h"


void tambah(int m, int n);

void garis();


void main() {

   int a, b;

   a = 9; b = 2;

   garis();

   cout << "\t||    Nilai Variable sebelum Fungsi digunakan" << endl;

   gotoxy (58,2); cout << "||" << endl;

   cout << "\t||    Nilai A adalah " << a << " Nilai B adalah " << b << endl;

   gotoxy (58,3); cout << "||" << endl;

   garis();

   // Pemanggilan Fungsi Tambah

   tambah (a, b);

   garis();

   cout << "\t||    Nilai Variable setelah Fungsi digunakan" << endl;

   gotoxy (58,8); cout << "||" << endl;

   cout << "\t||    Nilai A adalah " << a << " Nilai B adalah " << b << endl;

   gotoxy (58,9); cout << "||" << endl;


   getch();

}


void tambah(int m, int n){

   m += 3; n += 8;

   cout << "\t||    Nilai di dalam Fungsi Tambah." << endl;

   gotoxy (58,5); cout << "||" << endl;

   cout << "\t||    Nilai M adalah " << m << " Nilai N adalah " << n << endl;

   gotoxy (58,6); cout << "||" << endl;

}


void garis() {

   cout << "\t===================================================\n";

}


Contoh 4

                                                Lampiran Contoh 4 sofware Borland C++
/* Contoh Pemanggilan Fungsi By Reference */

#include "stdio.h"          //Nama  : Muhamad Syarif Ramadhan
#include "iostream.h"       //NIM   : 3420227001
#include "conio.h"
#include "iomanip.h"

void tambah(int *m, int *n);
void garis();

void main() {
   int a, b;
   a = 3; b = 5;
   garis();
   cout << "\t||  Nilai Variable sebelum Fungsi digunakan" << endl;
   gotoxy (70,2); cout << "||" << endl;
   cout << "\t||  Nilai A adalah " << a << " dan Nilai B adalah " << b << endl;
   gotoxy (70,3); cout << "||" << endl;
   garis();
   /*
    Pemanggilan Fungsi Tambah
    Nilai yang masuk ke Fungsi tambah bukan nilai dari Variable a dan b
    tetapi Alamat dari Variable a dan b
   */
   tambah (&a, &b);
   garis();
   cout << "\t||  Nilai Variable setelah Fungsi digunakan" << endl;
   gotoxy (70,8); cout << "||" << endl;
   cout << "\t||  Nilai A adalah " << a << " dan Nilai B adalah " << b << endl;
   gotoxy (70,9); cout << "||" << endl;

   getch();
}

void tambah(int *m, int *n){
   /*
      Nilai yang ada di Alamat m di tambah 5
      Hal ini akan merubah Nilai Variable a karena Variable a ada di alamat m
   */
   *m = *m + 5;
   /*
      Nilai yang ada di Alamat n di tambah 7
      Hal ini akan merubah Nilai Variable b karena Variable b ada di alamat n
   */
   *n += 7;
   cout << "\t||  Nilai di dalam Fungsi Tambah." << endl;
   gotoxy (70,5); cout << "||" << endl;
   cout << "\t||  Nilai M adalah " << m << " dan Nilai N adalah " << n << endl;
   gotoxy (70,6); cout << "||" << endl;
}

void garis() {
   cout << "\t===============================================================\n";
}

Terimakasih
Wassalamu'alaikum Wr. Wb.




No comments:

Post a Comment