saikaizu / belajar_ci

MIT License
1 stars 0 forks source link

Source Code yang belum di eksekusi #1

Open saikaizu opened 1 year ago

saikaizu commented 1 year ago

Source Code Praktikum Struktur Data //Tugas 2.4

include

include

using namespace std;

int main() { int Nilai[20]; int Posisi[20]; int i, N, Bilangan, Banyak = 0; bool ketemu = false;

cout << "Masukan Banyaknya Bilangan = ";
cin >> N;
cout << endl;

// Membaca elemen Array 
for (i = 0; i < N; i++) {
    cout << "Masukan elemen ke-" << i << " = ";
    cin >> Nilai[i];
}

// Membaca Elemen Array
cout << "\n\nDeretan Bilangan = ";
for (i = 0; i < N; i++) {
    cout << Nilai[i] << " ";
}

cout << "\n\nMasukan Bilangan yang akan dicabut = ";
cin >> Bilangan;

// Melakukan pencarian 
for (i = 0; i < N; i++) {
    if (Nilai[i] == Bilangan) {
        ketemu = true;
        Posisi[Banyak] = i;
        Banyak++;
    }
}

if (ketemu) {
    cout << "Bilangan " << Bilangan << " ditemukan sebanyak " << Banyak << " kali pada posisi ke = ";
    for (i = 0; i < Banyak; i++) {
        cout << Posisi[i] << " ";
    }
} else {
    cout << "Bilangan " << Bilangan << " tidak ditemukan";
}

getch();
return 0;

}

//Tugas 2.5

include

using namespace std;

int main() { int A[3][4] = {{1, 3, 4, 5}, {2, 4, 6, 8}, {3, 5, 7, 9}};

// Mencetak elemen-elemen matriks A
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
        cout << A[i][j];
    }
    cout << endl;
}

return 0;

}

// Tugas 3.1

include

include

include

using namespace std;

struct Mahasiswa { char Nim[9]; char Nama[25]; char Alamat[40]; short Umur; };

int main() { Mahasiswa Mhs; cout << "Nim : "; cin.getline(Mhs.Nim, 9); cout << "Nama : "; cin.getline(Mhs.Nama, 25); cout << "Alamat : "; cin.getline(Mhs.Alamat, 40); cout << "Umur : "; cin >> Mhs.Umur; cout << "\n\n\nNim : " << Mhs.Nim; cout << "\nNama : " << Mhs.Nama; cout << "\nAlamat : " << Mhs.Alamat; cout << "\nUmur : " << Mhs.Umur; getch(); return 0; }

include

include

include

using namespace std;

struct Mahasiswa { char Nim[9]; char Nama[25]; char Alamat[40]; short Umur; };

int main() { Mahasiswa Mhs; cout << "Nim : "; cin.getline(Mhs.Nim, 9); cout << "Nama : "; cin.getline(Mhs.Nama, 25); cout << "Alamat : "; cin.getline(Mhs.Alamat, 40); cout << "Umur : "; cin >> Mhs.Umur; cout << "\n\n\nNim : " << Mhs.Nim; cout << "\nNama : " << Mhs.Nama; cout << "\nAlamat : " << Mhs.Alamat; cout << "\nUmur : " << Mhs.Umur; getch(); return 0; }

//Tugas 3.2

include

include

include

using namespace std;

struct Tgl { int Tanggal; int Bulan; int Tahun; };

struct Tinggal { char Jain[40]; char Kota[15]; char Pas[5]; };

struct Mahasiswa { char Nim[9]; char Nama[25]; Tinggal Alamat; Tgl Lahir; };

int main() { Mahasiswa Mhs; cout << "NIM : "; cin.getline(Mhs.Nim, 9); cout << "Nama : "; cin.getline(Mhs.Nama, 25); cout << "Alamat : " << endl; cout << "\tJalan : "; cin.getline(Mhs.Alamat.Jain, 40); cout << "\tKota : "; cin.getline(Mhs.Alamat.Kota, 15); cout << "\tKode Pos : "; cin.getline(Mhs.Alamat.Pas, 5); cout << "Tanggal Lahir: " << endl; cout << "\tTanggal : "; cin >> Mhs.Lahir.Tanggal; cout << "\tBulan : "; cin >> Mhs.Lahir.Bulan; cout << "\tTahun : "; cin >> Mhs.Lahir.Tahun; cout << "\n\nMencetak Kembali Nilai Anggota\n\n"; cout << "NIM : " << Mhs.Nim << endl; cout << "Nama : " << Mhs.Nama << endl; cout << "Alamat : " << endl; cout << "\tJalan : " << Mhs.Alamat.Jain << endl; cout << "\tKota : " << Mhs.Alamat.Kota << endl; cout << "\tKode Pos : " << Mhs.Alamat.Pas << endl; cout << "Tanggal Lahir : " << Mhs.Lahir.Tanggal << "."; cout << Mhs.Lahir.Bulan << "-" << Mhs.Lahir.Tahun; getch(); return 0; }

include

include

using namespace std;

struct Tinggal { char Jain[40]; char Kota[15]; char Pos[5]; };

struct Tgl { int Tanggal; int Bulan; int Tahun; };

struct Mahasiswa { char Nim[9]; char Nama[25]; Tinggal Alamat; Tgl Lahir; };

int main() { Mahasiswa Mhs;

cout << "NIM     : ";
cin.getline(Mhs.Nim, 9);

cout << "Nama    : ";
cin.getline(Mhs.Nama, 25);

cout << "Alamat  : " << endl;
cout << "\tJalan   : ";
cin.getline(Mhs.Alamat.Jain, 40);
cout << "\tKota    : ";
cin.getline(Mhs.Alamat.Kota, 15);
cout << "\tKode Pos: ";
cin.getline(Mhs.Alamat.Pos, 5);

cout << "Tanggal Lahir: " << endl;
cout << "\tTanggal: ";
cin >> Mhs.Lahir.Tanggal;
cout << "\tBulan  : ";
cin >> Mhs.Lahir.Bulan;
cout << "\tTahun  : ";
cin >> Mhs.Lahir.Tahun;

cout << "\n\nMencetak Kembali Nilai Anggota\n\n";
cout << "NIM     : " << Mhs.Nim << endl;
cout << "Nama    : " << Mhs.Nama << endl;
cout << "Alamat  : " << endl;
cout << "\tJalan   : " << Mhs.Alamat.Jain << endl;
cout << "\tKota    : " << Mhs.Alamat.Kota << endl;
cout << "\tKode Pos: " << Mhs.Alamat.Pos << endl;
cout << "Tanggal Lahir: " << Mhs.Lahir.Tanggal << "." << Mhs.Lahir.Bulan << "." << Mhs.Lahir.Tahun << endl;

system("pause");
return 0;

}

//Tugas 3.3

include

include

include

using namespace std;

struct Mahasiswa { char Nim[9]; char Nama[25]; char Alamat[40]; int Umur; };

int main() { Mahasiswa Mhs[5]; int i; for (i=0; i<5; i++) { cout << "Nim : "; cin >> Mhs[i].Nim; cout << "Nama : "; cin >> Mhs[i].Nama; cout << "Alamat : "; cin >> Mhs[i].Alamat; cout << "Umur : "; cin >> Mhs[i].Umur; }

for (i=0; i<5; i++) {
    cout << "\n\nNim : " << Mhs[i].Nim;
    cout << "\nNama : " << Mhs[i].Nama;
    cout << "\nAlamat : " << Mhs[i].Alamat;
    cout << "\nUmur : " << Mhs[i].Umur;
}

getch();
return 0;

}

include

include

include

struct Mahasiswa { char Nim[9]; char Nama[25]; char Alamat[40]; int Umur; };

int main() { Mahasiswa Mhs[5]; int i;

for (i=0; i<5; i++) {
    std::cout << "Nim: ";
    std::cin >> Mhs[i].Nim;
    std::cout << "Nama: ";
    std::cin >> Mhs[i].Nama;
    std::cout << "Alamat: ";
    std::cin >> Mhs[i].Alamat;
    std::cout << "Umur: ";
    std::cin >> Mhs[i].Umur;
}

for (i=0; i<5; i++) {
    std::cout << "\n\nNim: " << Mhs[i].Nim;
    std::cout << "\nNama: " << Mhs[i].Nama;
    std::cout << "\nAlamat: " << Mhs[i].Alamat;
    std::cout << "\nUmur: " << Mhs[i].Umur;
}

getch();
return 0;

}

//Tugas 3.4

include

include

include

struct Tinggal { char Jalan[40]; char Kota[15]; char Pos[5]; };

struct TglLahir { int Tanggal; int Bulan; int Tahun; };

struct Mahasiswa { char Nim[9]; char Nama[25]; Tinggal Alamat; TglLahir Lahir; };

int main() { Mahasiswa Mhs[5]; int i;

for (i=0; i<3; i++) {
    std::cout << "NIM: ";
    std::cin >> Mhs[i].Nim;
    std::cout << "Nama: ";
    std::cin >> Mhs[i].Nama;
    std::cout << "Alamat:\n";
    std::cout << "\tJalan: ";
    std::cin >> Mhs[i].Alamat.Jalan;
    std::cout << "\tKota: ";
    std::cin >> Mhs[i].Alamat.Kota;
    std::cout << "\tKode Pos: ";
    std::cin >> Mhs[i].Alamat.Pos;
    std::cout << "Tanggal Lahir: \n";
    std::cout << "\tTanggal: ";
    std::cin >> Mhs[i].Lahir.Tanggal;
    std::cout << "\tBulan: ";
    std::cin >> Mhs[i].Lahir.Bulan;
    std::cout << "\tTahun: ";
    std::cin >> Mhs[i].Lahir.Tahun;
}

std::cout << "\n\nMencetak Kembali Nilai Anggota\n\n";

for (i=0; i<3; i++) {
    std::cout << "\nNIM: " << Mhs[i].Nim;
    std::cout << "\nNama: " << Mhs[i].Nama;
    std::cout << "\nAlamat:\n";
    std::cout << "\tJalan: " << Mhs[i].Alamat.Jalan;
    std::cout << "\n\tKota: " << Mhs[i].Alamat.Kota;
    std::cout << "\n\tKode Pos: " << Mhs[i].Alamat.Pos;
    std::cout << "\nTanggal Lahir: " << Mhs[i].Lahir.Tanggal << "-" << Mhs[i].Lahir.Bulan << "-" << Mhs[i].Lahir.Tahun;
}

getch();
return 0;

}

include

include

include

struct Tinggal { char Jalan[40]; char Kota[15]; char Pos[5]; };

struct TglLahir { int Tanggal; int Bulan; int Tahun; };

struct Mahasiswa { char Nim[9]; char Nama[25]; Tinggal Alamat; TglLahir Lahir; };

int main() { Mahasiswa Mhs[5]; int i;

for (i = 0; i < 3; i++) {
    std::cout << "NIM: ";
    std::cin >> Mhs[i].Nim;
    std::cout << "Nama: ";
    std::cin >> Mhs[i].Nama;

    std::cout << "Alamat:\n";
    std::cout << "\tJalan: ";
    std::cin >> Mhs[i].Alamat.Jalan;
    std::cout << "\tKota: ";
    std::cin >> Mhs[i].Alamat.Kota;
    std::cout << "\tKode Pos: ";
    std::cin >> Mhs[i].Alamat.Pos;

    std::cout << "Tanggal Lahir:\n";
    std::cout << "\tTanggal: ";
    std::cin >> Mhs[i].Lahir.Tanggal;
    std::cout << "\tBulan: ";
    std::cin >> Mhs[i].Lahir.Bulan;
    std::cout << "\tTahun: ";
    std::cin >> Mhs[i].Lahir.Tahun;
}

std::cout << "\n\nMencetak Kembali Nilai Anggota\n\n";
for (i = 0; i < 3; i++) {
    std::cout << "\nNIM: " << Mhs[i].Nim;
    std::cout << "\nNama: " << Mhs[i].Nama;
    std::cout << "\nAlamat:\n";
    std::cout << "\tJalan: " << Mhs[i].Alamat.Jalan;
    std::cout << "\n\tKota: " << Mhs[i].Alamat.Kota;
    std::cout << "\n\tKode Pos: " << Mhs[i].Alamat.Pos;
    std::cout << "\nTanggal Lahir: " << Mhs[i].Lahir.Tanggal << "-" << Mhs[i].Lahir.Bulan << "-" << Mhs[i].Lahir.Tahun;
}

getch();
return 0;

}

//Tugas 3.5

//Tugas 4.1

include

using namespace std;

int main() { int x = 8; int ptr = &x; // pointer ptr menunjuk ke alamat x cout << "Nilai x: " << x << endl; cout << "Alamat x: " << &x << endl; cout << "Alamat ptr: " << ptr << endl; cout << "Nilai pada alamat ptr: " << ptr << endl; // output nilai pada alamat yang ditunjuk oleh ptr *ptr = 10; // mengubah nilai pada alamat yang ditunjuk oleh ptr menjadi 10 cout << "Nilai x setelah diubah: " << x << endl; return 0; }

//Tugas 4.2

include

using namespace std;

int main() { int x = 8; int& xRef = x; // membuat reference xRef yang merujuk pada variabel x cout << "Nilai x: " << x << endl; cout << "Alamat x: " << &x << endl; cout << "Alamat xRef: " << &xRef << endl; cout << "Nilai pada alamat xRef: " << xRef << endl; // output nilai pada alamat yang direferensikan oleh xRef xRef = 10; // mengubah nilai pada alamat yang direferensikan oleh xRef menjadi 10 cout << "Nilai x setelah diubah: " << x << endl; return 0; }

include

using namespace std;

int main() { int x = 8; int xPtr = &x; // pointer xPtr menunjuk ke alamat x int y = xPtr; // y diinisialisasi dengan nilai yang ditunjuk oleh xPtr cout << "Nilai x: " << x << endl; cout << "Alamat x: " << &x << endl; cout << "Alamat xPtr: " << xPtr << endl; cout << "Nilai pada alamat yang ditunjuk oleh xPtr: " << *xPtr << endl; cout << "Nilai yang disimpan pada alamat " << xPtr << " adalah " << y << endl; return 0; }

//Tugas 4.3

include

include

using namespace std;

int main() { int Nilai[] = {45, 23, 50, 8, 12, 10, 15}; int* Ptr_Nilai; int i;

Ptr_Nilai = Nilai;

cout << "\nNilai Ptr_Nilai: " << *Ptr_Nilai;
cout << "\nAlamat array Nilai: " << Ptr_Nilai;
cout << "\nNilai " << *Ptr_Nilai << " yang ada pada alamat " << Ptr_Nilai << " adalah ";
cout << *Ptr_Nilai;

cout << "\nElemen Array indeks pertama " << Nilai[0];

cout << "\n\nElemen Array (dgn Array): ";
for (i = 0; i < 7; i++) {
    cout << Nilai[i] << " "; //mencetak elemen array
}

cout << "\n\nElemen Array (dgn Pointer): ";
for (i = 0; i < 7; i++) {
    cout << *(Ptr_Nilai + i) << " "; //mencetak elemen array
}

getch();
return 0;

}

//Tugas 4.4

include

include

using namespace std;

int main() { int x = 8; int* xPtr1; int** xPtr2;

xPtr1 = &x;
xPtr2 = &xPtr1;

cout << "Nilai x = " << x << endl;
cout << "Nilai xPtr1 = " << *xPtr1 << endl;
cout << "Nilai xPtr2 = " << **xPtr2 << endl;

getch();
return 0;

}

include

include

main() { int x = 8; int xPtr; xPtr = &x; cout<<"Nilai x = "<< x <<endl; cout<<"Alamat x = "<< &x <<endl; cout<<"Alamat xPtr = "<< xPtr <<endl; cout<<"Nilai yang disimpan pada alamat "<< xPtr <<" adalah "<< xPtr <<endl; getch(); return 0; }

//Tugas 4.5

include

include

using namespace std;

int main() { int arr[] = {1, 2, 3, 4, 5}; int *ptr = arr; int i = 0;

while (i < 5) {
    cout << "Nilai array ke-" << i+1 << " = " << *ptr << endl;
    ptr++;
    i++;
}

getch();
return 0;

}

//Tugas 5.1

saikaizu commented 1 year ago

Laptop engselnya patah, jadi pos di sini dulu make hp