Sunday, January 16, 2011

CPP_Function GPA calculator

C++ Code:
================================================


#include<iostream>
#include<stdio.h>
#include<string>
#define M 20 // Maximun of new subject
using namespace std;
int N,i,score[M],credit[M],ocredit,gradva[M],tlgradva=0,tlcredit=0;
float ogpa,newgpa=0;
string stuname,subname[M],stuid,subid[M];
char grad[M];
int main()
{
    float gpa(float,int,int,int);
    void condition();
    cout<<"\nYour name : ";getline(cin,stuname);
    cout<<"\nYour ID   : ";getline(cin,stuid);
    cout<<"\nOld GPA   : ";cin>>ogpa;
    cout<<"\nOld Credit: ";cin>>ocredit;
    cout<<"\nNumber new subject : ";cin>>N;
    for (i=0;i<N;i++)
    {   cout<<"\n"<<i+1<<".)Name suject\t: ";getchar();
        getline(cin,subname[i]);
        cout<<"\nID of "<<subname[i]<<" : ";
        cin>>subid[i];getchar();
        cout<<"\nScore\t: ";
        cin>>score[i];
        cout<<"\nCredit\t: ";
        cin>>credit[i];
        condition();
        tlgradva+=gradva[i]*credit[i];
        tlcredit+=credit[i];
    }
    newgpa=gpa(ogpa,ocredit,tlgradva,tlcredit);
    cout<<"\n=================Result:=================\n";
    cout<<"\nYour name : "<<stuname;
    cout<<"\nYour ID   : "<<stuid;
    cout<<"\nOld GPA   : "<<ogpa;
    cout<<"\nOld Credit: "<<ocredit;
    cout<<"\nNew Subject Result\n   Subeject\tCredit\tGrade\n";
    for (i=0;i<N;i++)
    {   cout<<i+1<<".)"<<subname[i]<<"\t"<<credit[i]<<"\t"<<grad[i]<<endl;}
    cout<<"\nTotal Credit update : "<<tlcredit+ocredit;
    printf("\nNEW GPA\t: %.2f",newgpa);
}
float gpa(float a,int b,int c,int d) // Function of GPA=(oldgpa*0ldcredit+totalnewgradvalue)/(oldcredit+totalnewcredit)
{
    return ((a*b+c)/(d+b));
}
void condition()
{   if(score[i]<50)
    {   grad[i]='F';gradva[i]=0;}
    else if(score[i]<60)
    {   grad[i]='D';gradva[i]=1;}
    else if(score[i]<70)
    {   grad[i]='C';gradva[i]=2;}
    else if(score[i]<80)
    {   grad[i]='B';gradva[i]=3;}
    else
    {   grad[i]='A';gradva[i]=4;}
}

No comments:

Post a Comment