Selamat sore rekan - rekan mahasiswa sekalian, kali ini pada postingan ini kita akan membahas "Apa itu struct pada pemrograman bahasa C++". Struct sendiri merupakan sebuah variable yang dapat menyimpan beberapa tipe data yang sama ataupun berbeda yang saling berhubungan. Bedanya dengan array adalah array hanya dapat menyimpan satu jenis variable saja.
Langsung saja kita masuk ke pembahasannya. Berikut merupakan program sederhana dari struct, simak dan perhatikan dengan baik sehingga rekan - rekan dapat paham konsepnya.
#include <iostream>
using namespace std;
struct datasiswa{
string nama;
int nim;
};
struct datanilai{
float nilai_uts,nilai_uas,nilai_quiz,absensi,nilai_tgs,rata;
};
struct indexnilai{
char index;
};
datasiswa siswa[10];
datanilai nilai[10];
indexnilai index[10];
int main(){
int jumlah;
cout << "Jumlah mahasiswa : "; cin >> jumlah;
cout << endl;
cout << "DATA MAHASISWA" << endl;
for(int i=0; i<jumlah; i++){
cout << "Nama ["<<i+1<<"] : "; cin >> siswa[i].nama;
cout << "NIM ["<<i+1<<"] : "; cin >> siswa[i].nim;
cout << "UTS ["<<i+1<<"] : "; cin >> nilai[i].nilai_uts;
cout << "UAS ["<<i+1<<"] : "; cin >> nilai[i].nilai_uas;
cout << "QUIZ ["<<i+1<<"] : "; cin >> nilai[i].nilai_quiz;
cout << "Tugas ["<<i+1<<"] : "; cin >> nilai[i].nilai_tgs;
cout << "Absen ["<<i+1<<"] : "; cin >> nilai[i].absensi;
nilai[i].rata=(nilai[i].nilai_uts+nilai[i].nilai_uas+nilai[i].nilai_quiz+nilai[i].nilai_tgs+nilai[i].absensi)/5;
system("cls");
}
system("pause");
cout << "OUTPUT DATA MAHASISWA" << endl;
for (int i=0; i<jumlah; i++){
cout << "Nama : " << siswa[i].nama << endl;
cout << "NIM : " << siswa[i].nim << endl;
cout << "UTS : " << nilai[i].nilai_uts << " | " << " UAS : " << nilai[i].nilai_uas << endl;
cout << "QUIZ : " << nilai[i].nilai_quiz<< " | " << " TUGAS : " << nilai[i].nilai_tgs << endl;
cout << "Absen : " << nilai[i].absensi << " | " << " Rata : " << nilai[i].rata << endl;
if (nilai[i].rata <=29)
{
index[i].index = 'E';
}
else if ((nilai[i].rata >=30) && (nilai[i].rata <=49))
{
index[i].index = 'D';
}
else if ((nilai[i].rata >=50) && (nilai[i].rata <=64))
{
index[i].index = 'C';
}
else if ((nilai[i].rata >=65) && (nilai[i].rata <=79))
{
index[i].index = 'B';
}
else if ((nilai[i].rata >=80) && (nilai[i].rata <=100))
{
index[i].index = 'A';
}
else
{
cout << "TIDAK VALID" << endl;
}
cout << "INDEX NILAI : " << index[i].index << endl;
cout << "------------------------------------------------------------------------" << endl;
}
system("pause");
return 0;
}
using namespace std;
struct datasiswa{
string nama;
int nim;
};
struct datanilai{
float nilai_uts,nilai_uas,nilai_quiz,absensi,nilai_tgs,rata;
};
struct indexnilai{
char index;
};
datasiswa siswa[10];
datanilai nilai[10];
indexnilai index[10];
int main(){
int jumlah;
cout << "Jumlah mahasiswa : "; cin >> jumlah;
cout << endl;
cout << "DATA MAHASISWA" << endl;
for(int i=0; i<jumlah; i++){
cout << "Nama ["<<i+1<<"] : "; cin >> siswa[i].nama;
cout << "NIM ["<<i+1<<"] : "; cin >> siswa[i].nim;
cout << "UTS ["<<i+1<<"] : "; cin >> nilai[i].nilai_uts;
cout << "UAS ["<<i+1<<"] : "; cin >> nilai[i].nilai_uas;
cout << "QUIZ ["<<i+1<<"] : "; cin >> nilai[i].nilai_quiz;
cout << "Tugas ["<<i+1<<"] : "; cin >> nilai[i].nilai_tgs;
cout << "Absen ["<<i+1<<"] : "; cin >> nilai[i].absensi;
nilai[i].rata=(nilai[i].nilai_uts+nilai[i].nilai_uas+nilai[i].nilai_quiz+nilai[i].nilai_tgs+nilai[i].absensi)/5;
system("cls");
}
system("pause");
cout << "OUTPUT DATA MAHASISWA" << endl;
for (int i=0; i<jumlah; i++){
cout << "Nama : " << siswa[i].nama << endl;
cout << "NIM : " << siswa[i].nim << endl;
cout << "UTS : " << nilai[i].nilai_uts << " | " << " UAS : " << nilai[i].nilai_uas << endl;
cout << "QUIZ : " << nilai[i].nilai_quiz<< " | " << " TUGAS : " << nilai[i].nilai_tgs << endl;
cout << "Absen : " << nilai[i].absensi << " | " << " Rata : " << nilai[i].rata << endl;
if (nilai[i].rata <=29)
{
index[i].index = 'E';
}
else if ((nilai[i].rata >=30) && (nilai[i].rata <=49))
{
index[i].index = 'D';
}
else if ((nilai[i].rata >=50) && (nilai[i].rata <=64))
{
index[i].index = 'C';
}
else if ((nilai[i].rata >=65) && (nilai[i].rata <=79))
{
index[i].index = 'B';
}
else if ((nilai[i].rata >=80) && (nilai[i].rata <=100))
{
index[i].index = 'A';
}
else
{
cout << "TIDAK VALID" << endl;
}
cout << "INDEX NILAI : " << index[i].index << endl;
cout << "------------------------------------------------------------------------" << endl;
}
system("pause");
return 0;
}
Sekian dan terima kasih, semoga tugasnya terbantu !!
Thanks for reading & sharing RZDev: Belajar Programming!
0 comments:
Post a Comment