Skip to content

Instantly share code, notes, and snippets.

@ryanhs
Created May 23, 2016 09:21
Show Gist options
  • Save ryanhs/b61833d9aea029fe20b6877b9ef1eefb to your computer and use it in GitHub Desktop.
Save ryanhs/b61833d9aea029fe20b6877b9ef1eefb to your computer and use it in GitHub Desktop.
hitung ipk, PHP
<?php
function A2N($alphabet) {
$alphabet = strtoupper($alphabet);
$result = -(ord($alphabet) - 69);
return $result > 0 && $result <= 4 ? $result : 0;
}
function calculateGPA($grades) {
$tmp = [0, 0];
foreach ($grades as $grade) {
$tmp[0] += A2N($grade[1]) * $grade[0];
$tmp[1] += $grade[0];
}
return number_format($tmp[0] / $tmp[1], 2);
}
$grades = [
// ['credits', 'alphabet']
// semester 1
[3, 'A'],
[3, 'B'],
[3, 'C'],
[3, 'B'],
// semester 2
[3, 'B'],
[3, 'A'],
[3, 'B'],
[3, 'A'],
// semester 3
[3, 'B'],
[3, 'B'],
[3, 'B'],
[3, 'A'],
];
header('Content-Type: text/plain');
foreach ($grades as $grade) {
echo "sks: {$grade[0]}\tgrade: {$grade[1]}" . PHP_EOL;
}
echo PHP_EOL;
echo 'GPA => ' . calculateGPA($grades);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment