Selasa, 02 Juni 2015

Script C++ Membuat table dan memberi nilai menggunakan if else

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int nim[10];
char nama[10][10];
int nilai[10];
int i,n,no=1;
cout<<"masukkan jumlah mahasiswa: ";cin>>n;;
for (i=1;i<=n;i++)
{
cout<<"masukkan nim  : ";cin>>nim[i];
cout<<"masukkan nama : ";cin>>nama[i];
cout<<"masukkan nilai: ";cin>>nilai[i];
cout<<"\n";
};
cout<<"jum mahasiswa: "<<n<<endl;
cout<<"\n";
cout<<"|no|\tnim\t|\tnama\t|\tnilai\t|  nilai huruf  |\n";
for (i=1;i<=n;i++)
{
if(nilai[i]>= 80 && nilai[i]<=100){
cout<<"|"<<no++<<" |\t"<<nim[i]<<"\t|\t"<<nama[i]<<"\t|\t"<<nilai[i]<<"\t|\t"<<"A"<<"\t|"<<endl;
}
else if(nilai[i]>=60)
{cout<<"|"<<no++<<" |\t"<<nim[i]<<"\t|\t"<<nama[i]<<"\t|\t"<<nilai[i]<<"\t|\t"<<"B"<<"\t|"<<endl;
}
else{
cout<<"|"<<no++<<" |\t"<<nim[i]<<"\t|\t"<<nama[i]<<"\t|\t"<<nilai[i]<<"\t|\t"<<"C"<<"\t|"<<endl;
}}
cout<<"\n";
getch();
}


Hasil Compile :


Script C++ Selection Sort

#include <iostream>
#include <iomanip>
#include <conio.h>

using namespace std;

int selectionsort (int array[], const int size)
 {
 int i, j, kecil, temp;
 for (i=0; i<size; i++)
 {
 kecil=i;
 for (j=i; j<size; j++)
 {
 if (array[kecil]>array[j])
 {kecil = j;}
 }
 temp = array [i];
 array[i] = array[kecil];
 array[kecil] = temp;

 }
 }

int main()
{
 int NumList[8] = {5, 34, 32, 25, 75, 42, 22, 2};
 int temp;
 cout<<"Data sebelum diurutkan: \n";
 for (int d=0; d<8; d++)
 {
 cout<<setw(3)<<NumList[d];
 }

 cout<<"\n\n";
 selectionsort(NumList,8);

 cout<<"Data setelah diurutkan:\n";
 for (int iii=0; iii<8; iii++)
 cout<<setw(3)<<NumList[iii]<<endl<<endl;
return 0;
}


Hasil Compile :


Program Raptor Membalik Kata






Script C++ Penjumlahan Matrix

#include <iostream>
#include <iomanip>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char *argv[]) {
int i,j,hasil[3][3];
int data[3][3],data2[3][3];
//untuk menampilkan isi matrik
cout<<"Isi variabel data adalah "<<endl;
cout<<"-------------------------\n\n";
cout<<" MATRIK 1 | MATRIK 2 "<<endl;
for (i=0;i<3;i++)
{ for(j=0;j<3;j++)
{cout<<"masukkan data "<<setw(3)<<i<<" "<<j<<":"<<setw(3);
cin>>data[i][j];
cout<<"masukkan data2 "<<setw(2)<<i<<" "<<j<<":"<<setw(3);
cin>>data2[i][j];}}
for (i=0;i<3;i++)
{for(j=0;j<3;j++)
{ cout<<setw(3)<<data[i][j];}
cout<<" | ";
for(j=0;j<3;j++)
{cout<<setw(3)<<data2[i][j];}
cout<<endl;}
//untuk proses penjumlahan
cout<<"\n\n Hasil Penjumlahan kedua Matrik"<<endl;
for (i=0;i<3;i++)
{ for(j=0;j<3;j++)
{hasil[i][j]=data[i][j]+data2[i][j];
cout<<" "<<hasil[i][j]<<" ";}
cout<<endl;}
return 0;
}


Hasil Compile :


Script C++ Memindah Array 1 Dimensi ke 2 Dimensi

#include <cstdlib>
#include <iostream>

using namespace std;

class convert{
friend istream& operator>>(istream&, convert&);
friend ostream& operator<<(ostream&, convert&);

public:
convert();
void pindah();

private:
int x[100],y[100][100];
int l,k,m,n;
};

convert::convert(){
cout<<"===============================\n"<<endl;
cout<<"Memindah Array 1 ke 2 Dimensi\n"<<endl;
cout<<"===============================\n"<<endl;
}

istream& operator>>(istream& in, convert& a){
cout<<"Masukkan Banyak Digit Array 1 Dimensi :";
in>>a.l;
for(int i=0; i<a.l; i++){
cout<<"Imput Data ke ["<<i+1<<"]:";
in>>a.x[i];
}

cout<<"\nMasukkan Baris Array 2 Dimensi :";
in>>a.n;
cout<<"\nMasukkan Kolom Array 2 Dimensi :";
in>>a.m;
return in;}

ostream& operator<<(ostream& out, convert& b){
out<<"Data Array 1 Dimensi :"<<endl;
for(int i=0; i<b.l; i++){
out<<b.x[i]<<" ";
}

out<<"\nData Array Setelah di Pindah :"<<endl;
for(int i=0; i<b.n; i++){
for(int j=0; j<b.m; j++){
out<<b.y[i][j]<<" ";
}
out<<endl;
}
return out;}

void convert::pindah(){
k=0;
for(int i=0; i<n; i++){
for(int j=0; j<m; j++){
y[i][j]=x[k];
k=k+1;
}
cout<<endl;
}
}

int main(int argc, char *argv[])
{
convert marty;
cin>>marty;
marty.pindah();
cout<<marty;

system("PAUSE");
return EXIT_SUCCESS;
}

Hasil Comple :


Script C++ Penggunaan Switch Case

#include <iostream>
#include <cstdlib>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class panji{
public:
void input();
void proses();
void output();
private:
string a;
int b,c,d,e;
};

void panji::input(){
cout<<"DAFTAR MENU"<<endl;
cout<<"1. Paket 1: Nasi + Ayam + Sambal + Lalapan + Es Teh    Harga Rp 9.000,-"<<endl;
cout<<"2. Paket 2: Nasi + Lele + Sambal + Lalapan + Es Teh    Harga Rp 8.000,-"<<endl;
cout<<"3. Paket 3: Nasi + Bebek + Sambal + Lalapan + Es Teh   Harga Rp 10.000,-"<<endl;
cout<<"masukkan nama : ";
cin>>a;
cout<<"masukkan no. kursi : ";
cin>>b;
cout<<"masukkan jenis paket : ";
cin>>c;
};

void panji::proses(){
switch(c){
case 1:
cout<<" Anda memilih paket 1"<<endl;
cout<<" Paket 1: Nasi + Ayam + Sambal + Lalapan + Es Teh    Harga Rp 9.000,-"<<endl;
cout<<" masukkan jumlah uang yang dibayarkan : ";cin>>d;
e=d-9000;
break;
case 2:
cout<<" Anda memilih paket 2"<<endl;
cout<<" Paket 2: Nasi + Lele + Sambal + Lalapan + Es Teh    Harga Rp 8.000,-"<<endl;
cout<<" masukkan jumlah uang yang dibayarkan : ";cin>>d;
e=d-8000;
break;
case 3:
cout<<" Anda memilih paket 3"<<endl;
cout<<" Paket 3: Nasi + Bebek + Sambal + Lalapan + Es Teh   Harga Rp 10.000,-"<<endl;
cout<<" masukkan jumlah uang yang dibayarkan : ";cin>>d;
e=d-10000;
break;
default:
cout<<"Menu yang anda pilih tidak tersedia.";
break;
};
};

void panji::output(){
cout<<"Nama : "<<a<<endl;
cout<<"No. kursi : "<<b<<endl;
cout<<"Paket nomor : "<<c<<endl;
cout<<"Uang yang dibayar : "<<d<<endl;
cout<<"Uang kembalian : "<<e<<endl;
};
int main(int argc, char *argv[]) {
panji wijaya;
wijaya.input();
wijaya.proses();
wijaya.output();
return 0;
}


Hasil Compile :


Script C++ Penggabungan Fungsi Perkalian, Pengurangan dan transpose matrix

#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;

class Array{

      public:
             void input();
             void kurang();
             void kali();
             void output();
      private:
              int n,m,a[10][10], b[10][10], c[10][10],d[10][10];
              };
           
void Array::input(){
     cout<<"Masukkan banyak baris : ";cin>>n;
     cout<<"Masukkan banyak kolom : ";cin>>m;
     cout<<endl;
     cout<<"Input Matriks A"<<endl;
     for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cout<<"Data ke A["<<i<<"]"<<"["<<j<<"] : ";cin>>a[i][j];
}
}
    cout<<endl;
cout<<"Input Matriks B"<<endl;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){

cout<<"Data ke A["<<i<<"]"<<"["<<j<<"] : ";cin>>b[i][j];
}
}}

void Array::kurang(){
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
c[i][j]=a[i][j]-b[i][j];
}
}
void Array::kali(){
for (int i=1;i<=n;i++)
  {
    for (int j=1;j<=m;j++)
    {
    d[i][j]=0;
        for (int k=1;k<=m;k++)
      {
        d[i][j]+= a[i][k]*b[k][j];
      }
    }
  }
}

void Array::output(){
cout<<"Matriks A"<<endl;
cout<<endl;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
cout<<setw(5)<<a[i][j]<<" ";
cout<<endl;
}

cout<<endl;
cout<<"Matriks B"<<endl;
cout<<endl;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
cout<<setw(5)<<b[i][j]<<" ";
cout<<endl;
}
cout<<"Hasil Pengurangan"<<endl;
cout<<endl;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
cout<<setw(5)<<c[i][j]<<" ";
cout<<endl;
}

cout<<"Hasil Perkalian"<<endl;
cout<<endl;
  for(int i=1;i<=n;i++)
 {
   for(int j=1;j<=m;j++)
    {
        cout<<setw(4)<<d[i][j];
   }
   cout<<endl;
  }
     cout<<endl;
cout<<"Transpose Matrik A"<<endl;
cout<<endl;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
cout<<setw(5)<<a[j][i]<<" ";
cout<<endl;
}
}
int main(int argc, char *argv[])
{
    Array x;

x.input();
x.kurang();
x.kali();
x.output();
    system("PAUSE");
    return EXIT_SUCCESS;
}


Hasil Compile :