Skip to content

Instantly share code, notes, and snippets.

@henrybear327
Created September 25, 2017 07:42
Show Gist options
  • Save henrybear327/9c772fdb757e1df3797d73eb5ac944a8 to your computer and use it in GitHub Desktop.
Save henrybear327/9c772fdb757e1df3797d73eb5ac944a8 to your computer and use it in GitHub Desktop.
GPA converter.cpp
#include <bits/stdc++.h>
using namespace std;
double gpa(int cnt[])
{
int sum = cnt[0] + 2 * cnt[1] + 3 * cnt[2] + 4 * cnt[3];
// printf("%d %d %d %d\n", cnt[0], cnt[1], cnt[2], cnt[3]);
int total = cnt[0] + cnt[1] + cnt[2] + cnt[3];
// printf("%d %d\n", sum, total);
return 1.0 * sum / total;
}
int main()
{
int num, credit;
int cnt[4] = {0};
printf("Please enter in the following format grade credit\n"
"e.g. 95 4 is 4 credit of grade 95\n");
while(scanf("%d %d", &num, &credit) == 2) {
if(num >= 90)
cnt[3] += credit;
else if(num >= 80)
cnt[2] += credit;
else if(num >= 70)
cnt[1] += credit;
else
cnt[0] += credit;
}
printf("total credit = %d\n", cnt[0] + cnt[1] + cnt[2] + cnt[3]);
printf("gpa = %.2f\n", gpa(cnt));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment