Writed by :
Wahyu Nurhadiyanto [12100732]
12.2c.07
Bagi rekan-rekan yang ingin belajar mendalami pemrograman C++, berikut ini saya posting contoh soal latihan untuk pemrograman C++ .
Monggo!!! silahkan di pelajari, semoga bisa membantu rekan-rekan semua.
Soal 1,
ARRAY DATA MAHASISWA
#include<stdio.h>
#include"conio.h"
#include"iostream.h"
#include"iomanip.h"
main()
{
int a,n;
char nama[5][20],nim[5][8];
char kelas[5][6],grade[5],ket[5][6];
int uts[5],uas[5],rata[5];
clrscr();
cout<<"Jumlah Data ? ";cin>>n;
for(a=1;a<=n;a++)
{
clrscr();
cout<<"Data Ke: "<<a<<endl;
cout<<"Nama: ";gets(nama[a]);
cout<<"Kelas: ";cin>>kelas[a];
cout<<"Nim: ";cin>>nim[a];
cout<<"Nilai UTS: ";cin>>uts[a];
cout<<"Nilai UAS: ";cin>>uas[a];
rata[a]=(uts[a]+uas[a])/2;
if(rata[a]>=80)
{
grade[a]='A';
strcpy(ket[a],"LULUS");
}
else if(rata[a]>=70)
{
grade[a]='B';
strcpy(ket[a],"LULUS");
}
else if(rata[a]>=60)
{
grade[a]='C';
strcpy(ket[a],"LULUS");
}
else if(rata[a]>=50)
{
grade[a]='D';
strcpy(ket[a],"GAGAL");
}
else
{
grade[a]='E';
strcpy(ket[a],"GAGAL");
}
cout<<endl;
}
clrscr();
cout<<"Daftar Nilai Mahasiswa"<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
cout<<"No Nama Nim Kelas Nilai Nilai Rata Grade Keterangan"<<endl;
cout<<" UTS UAS Rata"<<endl;
cout<<"----------------------------------------------------------------------"<<endl;
for(a=1;a<=n;a++)
{
cout<<setiosflags(ios::left)<<setw(3)<<a;
cout<<setiosflags(ios::left)<<setw(10)<<nama[a];
cout<<setiosflags(ios::left)<<setw(13)<<nim[a];
cout<<setiosflags(ios::left)<<setw(11)<<kelas[a];
cout<<setiosflags(ios::left)<<setw(6)<<uts[a];
cout<<setiosflags(ios::left)<<setw(6)<<uas[a];
cout<<setiosflags(ios::left)<<setw(5)<<rata[a];
cout<<setiosflags(ios::left)<<setw(6)<<grade[a];
cout<<setiosflags(ios::left)<<setw(10)<<ket[a]<<endl;
}
cout<<"----------------------------------------------------------------------"<<endl;
getch();
}
Soal 2,
ARRAY DIMENSI SATU
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<iomanip.h>
main()
{
int i;
char nama[5][20];
float nilai1[5];
float nilai2[5];
float hasil[5];
clrscr();
for(i=1;i<=2;i++)
{
cout<<"Data ke- "<<i<<endl;
cout<<"Nama Siswa : ";gets(nama[i]);
cout<<"Nilai Teori : ";cin>>nilai1[i];
cout<<"Nilai Praktek : ";cin>>nilai2[i];
hasil[i]=(nilai1[i]*0.40)+(nilai2[i]*0.60);
cout<<endl;
}
clrscr();
cout<<endl<<endl;
cout<<"------------------------------------"<<endl;
cout<<"No. Nama Siswa Nilai Nilai Hasil "<<endl;
cout<<" Teori Praktek Ujian "<<endl;
cout<<"------------------------------------"<<endl;
for(i=1;i<=2;i++)
{
cout<<setiosflags(ios::left)<<setw(4)<<i;
cout<<setiosflags(ios::left)<<setw(10)<<nama[i];
cout<<setprecision(2)<<" "<<nilai1[i];
cout<<setprecision(2)<<" "<<nilai2[i];
cout<<setprecision(2)<<" "<<hasil[i]<<endl;
}
getch();
}
Soal 3,
CLASS DATA
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
class data
{
public:
char nim[12],nama[25];
int nilai;
};
main()
{
int i,j;
data siswa[6];
cout<<"Masukkan Jumlah Data = ";cin>>j;
clrscr();
for(i=1;i<=j;i++)
{
cout<<"Data Ke - "<<i<<endl;
cout<<"Masukkan Nama Mahasiswa = ";gets(siswa[i].nama);
cout<<"Masukkan Nim Mahasiswa = ";cin>>siswa[i].nim;
cout<<"Masukkan Nilai Akhir = ";cin>>siswa[i].nilai;
}
cout<<endl;
cout<<"=============================================="<<endl;
cout<<"No Nama Nim Nilai"<<endl;
cout<<"=============================================="<<endl;
for(i=1;i<=j;i++)
{
cout<<setiosflags(ios::left)<<setw(4)<<i;
cout<<setiosflags(ios::left)<<setw(15)<<siswa[i].nama;
cout<<setiosflags(ios::left)<<setw(12)<<siswa[i].nim;
cout<<setiosflags(ios::left)<<setw(5)<<siswa[i].nilai<<endl;
}
getch();
}
MATRIX
#include<iostream.h>
#include<conio.h>
#include<conio.h>
main()
{
int kol_a,kol_b,bar_a,bar_b,i,j;
int a[50][50],b[50][50],c[50][50];
clrscr();
cout<<"MATRIKS A : "<<endl;
cout<<"Masukkan Jumlah Baris Matriks A : ";cin>>bar_a;
cout<<"Masukkan Jumlah Kolom Matriks A : ";cin>>kol_a;
cout<<endl;
cout<<"MATRIKS B : "<<endl;
cout<<"Masukkan Jumlah Baris Matriks B : ";cin>>bar_b;
cout<<"Masukkan Jumlah Kolom Matriks B : ";cin>>kol_b;
cout<<endl;
cout<<"Masukkan Nilai Matriks A : "<<endl;
for(i=1;i<=bar_a;i++)
{
for(j=1;j<=kol_a;j++)
{
cout<<"A["<<i<<","<<j<<"] = ";cin>>a[i][j];
}
}
cout<<endl;
cout<<"Masukkan Nilai Matriks B : "<<endl;
for(i=1;i<=bar_b;i++)
{
for(j=1;j<=kol_b;j++)
{
cout<<"B["<<i<<","<<j<<"] = ";cin>>b[i][j];
}
}
cout<<endl<<endl;
for(i=1;i<=bar_a;i++)
{
for(j=1;j<=kol_a;j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
cout<<endl<<endl;
gotoxy(1,24);
cout<<"A = ";
for(i=1;i<=bar_a;i++)
{
for(j=1;j<=kol_a;j++)
{
gotoxy(3+22*j,22+22*i);
cout<<a[i][j];
}
}
gotoxy(1,32);
cout<<"B = ";
for(i=1;i<=bar_b;i++)
{
for(j=1;j<=kol_b;j++)
{
gotoxy(3+23*j,30+20*i);
cout<<b[i][j];
}
}
gotoxy(1,40);
cout<<"C = ";
for(i=1;i<=bar_a;i++)
{
for(j=1;j<=kol_a;j++)
{
gotoxy(3+23*j,37+20*i);
cout<<a[i][j];
}
}
gotoxy(16,40);
cout<<" + ";
for(i=1;i<=bar_b;i++)
{
for(j=1;j<=kol_b;j++)
{
gotoxy(20+23*j,16+20*i);
cout<<b[i][j];
}
}
gotoxy(32,40);
cout<<" = ";
for(i=1;i<=bar_a;i++)
{
for(j=1;j<=kol_a;j++)
{
gotoxy(33+23*j,16+20*i);
cout<<c[i][j];
}
}
getch();
}
Soal 3,
CRUISE
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <iomanip.h>
main()
{
int i,data,jumbel[5];
char nama[20],lg ;
char napem[20],kode[5],buku[5][20];
long int harga[5],total[5],totalbayar,kembali,bayar;
cruise:
clrscr();
totalbayar=0;
cout<<"...... cruiseamir ......"<<endl;
cout<<"=========="<<endl;
cout<<"nama kasir \t:";gets(nama);
cout<<"nama pembeli \t:";gets(napem);
cout<<"\nbanyak data \t:";cin>>data;
cout<<"=========="<<endl;
for(i=1;i<=data;i++)
{
cout<<"data ke - "<<i<<endl;
cout<<"kode buku [k|m|n] \t:";cin>>kode[i];
if (kode[i]=='k'||kode[i]=='K')
{
strcpy(buku[i],"komik");
harga[i]=5000;
}
else if (kode[i]=='m'||kode[i]=='M')
{
strcpy(buku[i],"majalah");
harga[i]=15000;
}
else if (kode[i]=='n'||kode[i]=='N')
{
strcpy(buku[i],"novel");
harga[i]=25000;
}
cout<<"nama buku \t:"<<buku[i]<<endl;
cout<<"harga buku \t:"<<harga[i]<<endl;
cout<<"jumlah beli \t:";cin>>jumbel[i];cout<<endl;
total[i]=harga[i]*jumbel[i];
}
clrscr();
cout<<"Nama Pembeli : "<<napem<<endl;
cout<<"=============================================================\n";
cout<<"no kode buku nama buku jumlah beli harga total\n";
cout<<"=============================================================\n";
for(i=1;i<=data;i++)
{
cout<<setiosflags(ios::left)<<setw(8)<<i;
cout<<setiosflags(ios::left)<<setw(11)<<kode[i];
cout<<setiosflags(ios::left)<<setw(8)<<buku[i];
cout<<" "<<jumbel[i];
cout<<" "<<harga[i];
cout<<" "<<total[i]<<endl;
totalbayar+=total[i];
}
cout<<"=============================================================\n";
cout<<"\t\t\t total bayar = Rp. "<<totalbayar<<endl;
cout<<"\t\t\t uang bayar = Rp. ";cin>>bayar;
kembali=bayar-totalbayar;
cout<<"\t\t\t kembalian = Rp. "<<kembali<<endl;
cout<<" ingin input data [Y/T] : ";cin>>lg;
if(lg=='Y'||lg=='y')
goto cruise;
}
Soal 4,
FUNGSI SEGITIGA
#include <conio.h>
#include <iostream.h>
luas(float alas,float tinggi);
main()
{
float luas_segitiga,a,t;
clrscr();
cout<<"\nMenghitung Luas Segitiga"<<endl;
cout<<"\nMasukan Alas: ";cin>>a;
cout<<"\nMasukkan Tinggi: ";cin>>t;
luas_segitiga=luas(a,t);
cout<<"\nLuas Segitiga= "<<luas_segitiga<<" cm";
getch();
}
luas(float alas, float tinggi)
{
return(alas*tinggi/2);
}
Soal 5,
STRUCT ARRAY
#include<iostream.h>
#include<iomanip.h>
#include<stdio.h>
#include<conio.h>
main()
{
struct
{
char jenis[10];
int kode;
float lama,harga,pajak,total;
}penyewaan[6];
int i,j;
char nama[25],tgl[10];
float totalbiaya=0,diskon=0,totalbayar=0;
clrscr();
cout<<"Jumlah sewa = ";cin>>j;
cout<<"Nama Sewa = ";cin>>nama;
cout<<"Tanggal Sewa = ";cin>>tgl;
clrscr();
for(i=1;i<=j;i++)
{
cout<<"Data Penyewa Ke - "<<i<<endl;
cout<<"Kode Mobil = ";cin>>penyewaan[i].kode;
cout<<"Jenis Mobil= ";cin>>penyewaan[i].jenis;
cout<<"Lama Sewa = ";cin>>penyewaan[i].lama;
cout<<"Harga Sewa = ";cin>>penyewaan[i].harga;
cout<<endl;
if(penyewaan[i].kode=='1')
penyewaan[i].pajak=150000;
else
penyewaan[i].pajak=100000;
penyewaan[i].total=(penyewaan[i].lama*penyewaan[i].harga)+penyewaan[i].pajak;
totalbiaya+=penyewaan[i].total;
diskon=0.1*totalbiaya;
totalbayar=totalbiaya-diskon;
}
clrscr();
cout<<" DAFTAR PENYEWA MOBIL"<<endl;
cout<<" PT. SAFETY TRANSPORTATION"<<endl;
cout<<"=============================================================="<<endl;
cout<<"No Kode Jenis Lama Harga Pajak Total"<<endl;
cout<<" Mobil Mobil Sewa Sewa"<<endl;
cout<<"=============================================================="<<endl;
for(i=1;i<=j;i++)
{
printf("%d %8s %8s %8.0f %8.0f %8.0f %8.0f",
i,penyewaan[i].kode,penyewaan[i].jenis,penyewaan[i].lama,
penyewaan[i].harga,penyewaan[i].pajak,penyewaan[i].total);
cout<<endl;
}
cout<<"=============================================================="<<endl;
printf("Total Biaya Sewa= Rp. %0.0f",totalbiaya);cout<<endl;
printf("Diskon= Rp. %0.0f",diskon);cout<<endl;
printf("Total Pembayaran= Rp. %0.0f",totalbayar);cout<<endl;
}
No comments:
Post a Comment