Skip to content

Instantly share code, notes, and snippets.

@krisanalfa
Last active December 15, 2015 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisanalfa/5334596 to your computer and use it in GitHub Desktop.
Save krisanalfa/5334596 to your computer and use it in GitHub Desktop.
Array, variable protoyping, and function in C++
#include <cstdlib>
#include <iostream>
using namespace std;
int mahasiswa [10] = {
2009140507,
2009140508,
1234567890
};
int panjangMahasiswa = sizeof(mahasiswa) / sizeof(*mahasiswa);
int data [3][4] = {
// {"NIM", "Nilai A", "Nilai B", "Nilai C"}
{2009140507, 90, 80, 70},
{2009140508, 70, 80, 90},
{1234567890, 10, 20, 30}
};
int dataRow = sizeof(data) / sizeof(data[0]);
int dataCol = sizeof(data[0]) / sizeof(*data[0]);
// Prototyping hack ;) [I HATE C++]
void masukkanInput();
void proses(int nim) {
cout << "NIM kamu: " << nim << endl;
for (int i = 0; i < dataRow; ++i) {
for (int j = 0; j < dataCol; ++j) {
if (nim == data[i][j]) {
float rataRata = (data[i][1] + data[i][2] +data[i][3]) / 3;
bool lulus;
if(rataRata > 60) {
lulus = true;
} else {
lulus = false;
}
cout << "Nilai A: " << data[i][1] << endl;
cout << "Nilai B: " << data[i][2] << endl;
cout << "Nilai C: " << data[i][3] << endl;
cout << "Nilai Rata-rata: " << rataRata << endl;
if(lulus) {
cout << "Selamat! Kamu udah lulus" << endl;
} else {
cout << "Emang enak kagak lulus!" << endl;
}
}
}
}
}
void callback(bool err, int nim) {
if(err) {
// NIM tidak terdaftar, masukin NIM lagi
cout << "Maaf, NIM yang kamu masukkan salah, coba lagi..." << endl;
masukkanInput();
} else {
// NIM terdaftar, go to process
proses(nim);
}
}
void daftar(int nim) {
for (int i = 0; i < panjangMahasiswa; ++i) {
if (nim == mahasiswa[i]) {
callback(false, mahasiswa[i]);
return;
}
}
callback(true, 0);
}
void masukkanInput() {
int nim;
cout << "Masukkan NIM: ";
cin >> nim;
if(!cin) {
cout << "Error! NIM bersifat numerik kan?" << endl;
return;
}
daftar(nim);
}
void selamatDatang() {
cout << "================================" << endl;
cout << " UNIVERSITAS ANU " << endl;
cout << "================================" << endl;
}
int main() {
selamatDatang();
masukkanInput();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment