Thursday, November 29, 2012

Array

Alhamdulilah tugas ke 7 telah selesai dengan materi tentang Array baik 1 dimensi maupun 2 dimensi.. adapun scriptnya :


#include <iostream>
using namespace std;

int main()
{
       //Mengisi & Menampilkan Array
       cout<<"===== Mengisi & Menampilkan Array =====\n";
       int Nama_Array[5];
      
       cout <<" Masukkan nilai pada : "<<endl;
       for (int C=0; C<5; C++)
       {
              cout<<"Elemen ["<<C<<"] = ";
              cin >>Nama_Array [C];
       }
       cout<<"\n";
       cout<<"Tampilkan nilai yang dimasukkan : "<<endl;
       for (int D=0; D<5; D++)
       {
              cout<<"Nilai yang terdapat pada elemen ke : ";
              cout<< D+1 << " = " << Nama_Array[D] << endl;
       }
       cout<<"=============================================";
       cout<<"\n";

       //Inisialisasi Array
       cout<<"========== Inisialisasi Array ===============\n";
       int I[3] = {5,7,13};
      
       cout<<"Nilai awal pada elemen Array : " <<endl;
       cout<<"I [0] = "<<I [0] << endl;
       cout<<"I [1] = "<<I [1] << endl;
       cout<<"I [2] = "<<I [2] << endl;

       //Perubahan nilai element 1 & 2
      
        I [1]= 25;
        I [2]= 25;
       cout<<"Setelah perubahan nilai Array : "<<endl;
       cout<<"I [0] = "<<I[0] << endl;
       cout<<"I [1] = "<<I[1] << endl;
       cout<<"I [2] = "<<I[2] << endl;
       cout<<"=============================================";
       cout<<"\n";
      
       //PEncarian nilai
       cout<<"========== PEncarian Nilai dgn Array ===========\n";
       int data[10] = {4, 1, 0, -9, 8, 5, -1, 2, 3, -7};
       int elemen, ketemu,Z;
       cout << "Data yang dicari : "<<endl;
       cin >> Z;
      
       ketemu = 0;
       for(elemen=0; elemen<= 9; elemen++)
       {
              if (data[elemen] == Z)
                     { ketemu =!ketemu;
                           break;
                     }
       }
              if (ketemu == 0) cout << "Data tidak ditemukan ";
              else cout << "Data ada di elemen : " << elemen<<endl; 

       cout<<"=============================================";
       cout << '\n';

//ARRAY KARAKTER
       cout<<"========== Array Karakter ===========\n";
       char NAMAKU [6] = {'H','A','S','Y','A','\0'};
       char NAMAMU [6] = "TASYA";
      
       for (int X=0; X<6; X++)
              {
                     cout << NAMAKU [X];
              }
       cout << endl;
       cout << NAMAMU;
       cout << endl << endl;
       cout << "Jumlah Elemen NAMAKU : " << sizeof (NAMAKU)<< endl;
       cout << "Jumlah Elemen NAMAMU : " << sizeof (NAMAMU)<< endl << endl;
       cout<<"=============================================";
       cout << '\n';

//array 2 dimensi
       cout<<"========== Array 2 dimensi ===========\n";
       int j, k;
       int DATA[5][3] =
//membuat array dgn 5 baris 3 kolom
       {
       {3, 4, -1},
       {2, 3, 0},
       {1, 1, 2},
       {5, 9, -4},
       {6, 6, 2}
       };

       for (j = 0; j<=4; j++)
       {
       for (k = 0; k<=2; k++)
       cout << "DATA [" << j << "]["<< k << "] = " << DATA[j][k] << endl;
       }


       system("pause");
       return 0;
}



Vidio tutorialnya sebagai berikut

No comments:

Post a Comment