4 20120320 programlatihan (DOC)

Document Sample

```					//nama file: while.cpp
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
int bil=1;
while(bil <= 10) {
cout << bil << " "; ++bil;
}
getch();
}

//nama file: switch.cpp
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main() {
char id_kegiatan;
cout << "Ketik kode kegiatan [A...C]: "; cin >> id_kegiatan;

//seleksi nilai variabel
switch(id_kegiatan)
{
case 'A':
case 'a':
cout << "Sedang main basket"; break;
case 'B':
case 'b':
cout << "Sedang main bola"; break;
case 'C':
case 'c':
cout << "Sedang main tenis"; break;
default:
cout << "Maaf anda salah ketik"; break;
}
getch();
}

//program suhu dengan array 1D
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main() {
float suhu_udara[3];
cout << "Ketik tiga data suhu udara" << endl;

for (int i=0; i<3; i++) {
cout << i << " :"; cin >> suhu_udara[i];
}
cout << "Data suhu yang dimasukkan:" << endl;
for (int i=0; i<3; i++) {
cout << suhu_udara[i] << endl;
}
getch();
}#include <stdio.h>
#include <conio.h>
#include <iostream.h>
void main() {
float suhu_udara[3];
cout << "Ketik tiga data suhu udara" << endl;

for (int i=0; i<3; i++) {
cout << i << " :"; cin >> suhu_udara[i];
}

cout << "Data suhu yang dimasukkan:" << endl;
for (int i=0; i<3; i++) {
cout << suhu_udara[i] << endl;
}
getch();
}

//contoh program if nested
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
float hasil,lembur,bonus,total=0;
cout << "Penghasilan hari ini Rp. "; cin >> hasil;

/*bagian perulangan IF nested*/
if (hasil >= 0 && hasil <= 200000) {
lembur = 20000;
bonus = 0.1 * hasil;
}
else if (hasil <= 500000) {
lembur = 20000;
bonus = 0.15 * hasil;
}
else {
lembur = 20000;
bonus = 0.2 * hasil;
}

/*hitung penghasilan*/
total = lembur + bonus;
cout << "Lembur hari ini Rp. " << lembur << endl;
cout << "Bonus hari ini Rp. " << bonus << endl;
cout << "=================================" << endl;
cout << "Hasil lembur dan bonus Rp. " << total << endl;
getch();
}

/*Program Array 1 Dimensi
Untuk mencari bilangan terkecil dan terbesar*/
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
int x[10] = {45,34,23,34,32,12,65,76,34,23};
int i;
int mak = -1000;

for (i=0; i<10; i++) {
if (x[i] > mak) {
mak = x[i];
}
}
cout << "Nilai Maksimal dalam array: " << mak << endl;
getch();
}

/* program berdimensi tiga penjualan
sepeda motor dalam bentuk tabel */
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
void main() {
int i,j,k;
int data_jual[2] [3] [2];

for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
for (k=0; k<2; k++) {
cout << "Data tahun ke- " << i << endl;
cout << "Data ke-" << j << " "<< k << endl;
cout << "Jumlah Penjualan : ";
cin >> data_jual[i] [j] [k];
}
cout << endl;
}
cout << endl;
}

cout << endl;
cout << "Data Penjualan per tahun" << endl;
cout << "==================================="; cout << endl;
cout << "Tahun|Hasil ke| Tahun Penjualan ke "; cout << endl;
cout << " |     | I      II "; cout << endl;
cout << "==================================="; cout << endl;
cout << endl;

for (i=0; i<2; i++) {
for (j=0; j<3; j++) {
cout << setiosflags(ios::left) << setw(11) << i;
cout << setiosflags(ios::left) << setw(9) << j;
for (k=0; k<2; k++) {
cout << setiosflags(ios::right) << setw(5);
cout << data_jual[i] [j] [k];
cout << " ";
}
cout << endl;
}
cout << endl;
}
cout << "=================================="; cout << endl;
getch();
}

/* menginputkan data menggunakan array */
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
void main() {
char* kal[3];
int i;

for (i=0; i<3; i++) {
kal[i]=new char[256];
cout << "Ketik sebuah kata array ke " << i << " :";
cin >> kal[i];
};

for (i=0; i<3; i++) {
cout << "Isi kata array ke " << i << kal[i] << endl;
};
getch();
}

/* Program berdimensi dua
menampilkan informasi
perkembangan jumlah
pendaftar mahasiswa
baru setiap tahun */
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
void main() {
int i,j;
int data_jual[3] [3];

for (i=0; i<=2; i++) {
for (j=0; j<=2; j++) {
cout << "Data ke-"<<i<<" "<<j<< endl;
cout << "Jumlah Penjualan: "; cin >> data_jual[i] [j];
}
}
cout << "Data Penjualan Per Tahun" << endl;
cout << " Sepeda Motor " << endl;
cout << "========================" << endl;
cout << "No 2003 2004 2005 " << endl;
cout << "========================" << endl;

for (i=0; i<=2; i++) {
cout << setiosflags(ios::left) << setw(5) << i;
for (j=0; j<=2; j++) {
cout << setiosflags(ios::right) << setw(4);
cout << data_jual[i] [j];
cout << " ";
}
cout << endl;
}
cout << "========================" << endl;
getch();
}

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 8 posted: 6/4/2012 language: pages: 5