Minggu, 17 April 2016

Diktat Bab 7

Workshop Diktat

1. Cara menyisipkan huruf,kata, atau kalimat pada kata dan tempat yang kita inginkan ,,??? ,, kita hanya perlu menggunakan prosedur insert ,, lebih jelas nya sebagai berikut...

C++ :

 #include <iostream>
#include <string.h>
#include <cstdlib>
/* 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)
{
    string a,b;int c;
    cout<<"kalimat pertama : ";
    getline(cin, a);
    cout<<"kalimat kedua : ";
    cin>>b;
    cout<<a<<endl;
    cout<<"Menyisipkan kata pada karakter ke : ";cin>>c;
    a.insert(c, " "+b+" ");
    cout<<a;
    return 0;
}


Output :



2. C++ :

 #include <iostream>
#include <string.h>
#include <conio.h>
using namespace std;
int main()
{
int i,jum;

char string[100];

cout<<"Masukan sembarang kalimat:\n";
cin.getline(string,100); //membaca spasi

//mengubah huruf awal menjadi huruf kapital
if((string[0]>='a')&& (string[0]<='z'))
string[0]=(char)string[0]-32;


//mengubah huruf setelah huruf awal menjadi huruf kecil
jum=1;int d=0;
for(i=0;i<(int)strlen(string);i++)
{
    if(i%2==0){
   
if((string[jum]>='A')&& (string[jum]<='Z'))
string[jum]=(char)string[jum]+32;
jum++;}
}

//mengubah huruf setelah spasi menjadi huruf kapital
for(i=1;string[i]!='\0';i++)
{
    if(string[i]==' ') d++;
if((i-1-d)%2==0)
{
if(string[i+1]>='a' && string[i+1]<='z')
string[i+1]=(char)(string[i+1]-32);
}
}


cout<<"Hasil Akhir : "<<string<<endl;
getch();
}


Output :

3. C++ :

 #include <iostream>
using namespace std;
void nama(){
    string kata;
 
    cout<<"mencari panjang nama=";getline(cin,kata);
    cout<<"berisi berapa kata="<<kata.size();
 
    }

int main(){
    nama();
    return 0;a
}


Output :


4.  Sebuah vektor yang disimpan menggunakan array, dapat dipandang sebagai vektor baris maupun vektor kolom. Method penjumlahan_vektor didefinisikan sebagai penjumlahan elemen elemen Vektor A dan Vektor B pada indeks yang sama. Method perkalian_vektor didefinisikan sebagai perkalian Vektor A dengan skalar k. Sementara method beri_nilaiBanyak menunjukkan berapa banyak elemen dari suatu vektor. Lengkapi isi method di atas sehngga sesuai dengan definisi yang diberikan.

C++ :

 #include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
class Vektor {
friend ostream& operator<<(ostream&, Vektor&);
friend istream& operator>>(istream&, Vektor&);
friend class SPL;
public:
Vektor();
void penjumlahan_vektor(const Vektor& A, const Vektor& B);
void perkalian_vektor(float k, const Vektor& A);
void beri_nilaiBanyak(int);
private:
int elemen[100];
int banyak; };
int main(int argc, char** argv) {
   
    return 0;
}
 

Latihan Diktat

1.  C++ :

#include <iostream>
#include <windows.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void masuk(int c[20],int b[20]){
    for (int i=1;i<=2;i++){
        for (int j=1;j<=3;j++){
            cout<<"Vaktor A "<<i<<","<<j<<" =";cin>>b[i,j];
            cout<<"Vaktor B "<<i<<","<<j<<" =";cin>>c[i,j];
        
        }
    }
    system("cls");
}
void proses(int b[20],int c[20],int hasil[20],int kali[20]){

        cout<<"Vaktor A"<<endl;
    for(int i=1;i<=2;i++){
        for(int j=1;j<=3;j++){
            cout<<b[i,j]<<" ";
        }
    }
    cout<<"\n\nVaktor B"<<endl;
        for(int i=1;i<=2;i++){
        for(int j=1;j<=3;j++){
            cout<<c[i,j]<<" ";
        }
    }
    cout<<"\n\nVektor C"<<endl;
    for (int i=1;i<=2;i++){
        for (int j=1;j<=3;j++){
            hasil[i,j]=b[i,j]+c[i,j];
        
            cout<<hasil[i,j]<<" ";
        }
    
    }
    cout<<"\n\nVaktor Hasil Kali"<<endl;
    for (int i=1;i<=2;i++){
        for (int j=1;j<=3;j++){
            kali[i,j]=b[i,j]*hasil[i,j];
        
            cout<<kali[i,j]<<" ";
        }
    
    }
    cout<<"\n\nBanyak Elemen ="<<12;
}

int main(int argc, char** argv) {
    int hasil[20];
    int kali[20];
    int b[20];
    int c[20];
    cout<<"Menghitung faktor"<<endl;
    masuk(b,c);
    proses(b,c,hasil,kali);
    return 0;
}


Output :


2. C++ :

#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
int i,jum;

char string[100];

cout<<"Masukan sembarang kalimat:\n";
cin.getline(string,100); //membaca spasi

//mengubah huruf awal menjadi huruf kapital
if((string[0]>='a')&& (string[0]<='z'))
string[0]=(char)string[0]-32;


//mengubah huruf setelah huruf awal menjadi huruf kecil
jum=1;int d=0;
for(i=0;i<(int)strlen(string);i++)
{
            if(i%2==0){
          
if((string[jum]>='A')&& (string[jum]<='Z'))
string[jum]=(char)string[jum]+32;
jum++;}
}

//mengubah huruf setelah spasi menjadi huruf kapital
for(i=1;string[i]!='\0';i++)
{
            if(string[i]==' ') d++;
if((i-1-d)%2==0)
{
if(string[i+1]>='a' && string[i+1]<='z')
string[i+1]=(char)(string[i+1]-32);
}
}


cout<<"Hasil Akhir : "<<string<<endl;
getch();
}                   

3. C++ :

 #include <iostream>
 #include <cstdlib>
 using namespace std;
int main()
{
      char kalimat[100];
      int i, spasi=0;

      cout << "\n\t\t - Program Menghitung Kata Dalam Sebuah Kalimat -\n";
      cout << "\t==================================================================\n\n";
      cout << "\t Masukan Sebuah Kalimat : ";
      cin.getline(kalimat, sizeof(kalimat));

      for(i=0; kalimat[i]; i++)
      {
            if(isspace (kalimat[i]) || ispunct (kalimat[i]))
            {
                  spasi++;
            }
      }
      cout << "\t Jumlah Kata = " << spasi +1 << "\n\n";
      cout << "\t Kata yang anda masukan adalah : \n";
      cout << "\t ";

      for(i=0; kalimat[i]; i++)
      {
            if(isspace (kalimat[i]) || ispunct (kalimat[i]))
            {
                  spasi++;
                  cout << "\n";
                  cout << "\t";
            }
            cout << kalimat[i];
      }

      cout << "\n";
    
      cout << "\t==================================================================\n\n";
      cout << "\n";
      getchar();
}

Output :


0 komentar: