Wednesday, December 26, 2012

My Programming Code for Calculating CGPA PPSM UKM 2012

Hi everyone..
especially my 1st year friends in PPSM UKM.
I just finished my coding for calculating our CGPA..
It is a success except it looks quite long
I have used function to simplify it but it just shorten about 25%
Perhaps i should used arrays in it
Whatever it is, follow the instruction in the program carefully..
This code is for Dev C++,
if you use other program you can remove the 'system("PAUSE");'

Enjoy, and plan your final pointer from now :)

-----------------------------------------------------------------------------------

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void grading(string&, double&);

int main()
{
    int i, credit, extra;
    double score, calculus, computer, economics, fizis, english, PD, subject, total, overall;
    string grade, add;
 
    credit = 18;
 
    cout<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)<<setprecision(2);
         
    cout <<"Please enter your respective grades for each subjects."<<endl;
    cout <<"A  (85-100%)"<<endl;
    cout <<"A- (75-84%)"<<endl;
    cout <<"B+ (65-74%)"<<endl;
    cout <<"B  (60-64%)"<<endl;
    cout <<"B- (55-59%)"<<endl;
    cout <<"C+ (50-54%)"<<endl;
    cout <<"C  (45-49%)"<<endl;
    cout <<"C- (40-44%)"<<endl;
    cout <<"D+ (35-39%)"<<endl;
    cout <<"D  (30-34%)"<<endl;
    cout <<"E  (0-29%)"<<endl;
    cout <<"Type 'N' if you're not taking the subject"<<endl<<endl;
 
    cout <<"Calculus : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-4;
    }
    calculus = score * 4;
 
    cout <<"\nComputer Programming : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-4;
    }
    computer = score * 4;
 
    cout <<"\nEconomics : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-3;
    }
    economics = score *3;
 
    cout <<"\nSains Fizis : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-3;
    }
    fizis = score * 3;
 
    cout <<"\nFoundation English or Academic Communication : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-2;
    }
    english = score * 2;
 
    cout <<"\nPembangunan Diri : ";
    cin >> grade;
    grading(grade,score);
 
    if (grade == "N")
    {
    score = 0;
    credit = credit-2;
    }
    PD = score * 2;
 
    total = calculus + computer + economics + fizis + english + PD;
     
    for( ; ; )
    {
    cout <<"\nDo you take another additional course:(Y/N) ";
    cin >> add;
 
    if(add == "N")
    break;
 
    else
    {
        cout <<"\nHow much is the course credit hour: ";
        cin >> extra;
        credit = credit + extra;
     
        cout << "\nGrade for the subject: ";
        cin >> grade;
        grading(grade,score);
        subject = score * extra;
        total = total + subject;
    }
}

    overall = total/credit;
 
    cout <<"\n============================================";
    cout <<"\nYour total credits are "<<credit;
    cout <<"\nYour Overall Pointers for Semester 1 is "<<overall<<endl<<endl;
 
    system ("PAUSE");
    return 0;
}

void grading(string& grade, double& score)
{
    if (grade == "A")
    score = 4;
    if (grade == "A-")
    score = 3.67;
    if (grade == "B+")
    score = 3.33;
    if (grade == "B")
    score = 3;
    if (grade == "B-")
    score = 2.67;
    if (grade == "C+")
    score = 2.33;
    if (grade == "C")
    score = 2.00;
    if (grade == "C-")
    score = 1.67;
    if (grade == "D+")
    score = 1.33;
    if (grade == "D")
    score = 1.00;
    if (grade == "E")
    score = 0;

    return;
}

No comments:

Post a Comment