CGPA the Golden Egg!

Limits: 1s, 512 MB

Jenith is a student of CSE of Metropolitan University (MU). Recently her fifth term’s result has been published. She obtained CGPA 4 out of 4. Her classmates ask her to calculate their CGPA. Actually she helps her friends to calculate their CGPA from the first term. Sometimes she feels bore, so she wants to write a program now that will help her friends to calculate their CGPA. As you are a very good programmer she needs your help.


This is a companion discussion topic for the original entry at https://toph.co/p/cgpa-the-golden-egg
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,i,c,t;
    double a,b,sum,cg,cr;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>m;
        sum=0;
        cr=0;
        c=0;
        for(int j=0;j<m;j++)
        {
            cin>>a>>b;
            if(a>=80&&a<=100)
            {
               cg=4.00;
            }
            else if(a>=75&&a<80)
            {
                cg=3.75;
            }
            else if(a>=70&&a<75)
            {
                cg=3.50;
            }
            else if(a>=65&&a<70)
            {
                cg=3.25;
            }
            else if(a>=60&&a<65)
            {
                cg=3.00;
            }
            else if(a>=55&&a<60)
            {
                cg=2.75;
            }
             else if(a>=50&&a<55)
            {
                cg=2.50;
            }
             else if(a>=45&&a<50)
            {
                cg=2.25;
            }
             else if(a>=40&&a<45)
            {
                cg=2.00;
            }
             else if(a<40&&a>=0)
            {
                c++;
            }
            cr+=b;
            sum+=cg*b;
        }
        if(c>0)
        {
            if(c==1)
        cout<<"Case "<<i+1<<": "<<"Sorry, you have failed in "<<1<<" courses!"<<endl;
            else
            cout<<"Case "<<i+1<<": "<<"Sorry, you have failed in "<<c<<" courses!"<<endl;
        }
           else
        {
            cout<<"Case "<<i+1<<": "<<fixed<<setprecision(2)<<sum/cr<<endl;
        }
    }
}

what’s the problem this code?

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n,c;
    double m,a,b,sum,sum1,p;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        sum=0,sum1=0,p=0,c=0;
        cin>>m;
        while(m--)
        {
            cin>>a>>b;
            if(a>=80)
            {
                p=4*b;
            }
            else if(a>=75)
            {
                p=3.75*b;
            }
            else if(a>=70)
            {
                p=3.50*b;
            }
            else if(a>=65)
            {
                p=3.25*b;
            }
            else if(a>=60)
            {
                p=3*b;
            }
            else if(a>=55)
            {
                p=2.75*b;
            }
            else if(a>=50)
            {
                p=2.50*b;
            }
            else if(a>=45)
            {
                p=2.25*b;
            }
            else if(a>=40)
            {
                p=2*b;
            }
            sum=sum+b;
            sum1=sum1+p;
            if(a<40)
            {
                c++;
            }
        }
        if(c>0)
        {
            cout<<"Case "<<i<<": Sorry, you have failed in "<<c<<" courses!"<<endl;
        }
        else
        {
            cout<<fixed<<setprecision(2)<<"Case "<<i<<": "<<sum1/sum<<endl;
        }
    }
}

???

Your code can’t handle test case (WA) when both mark and credit is a floating number. try this as input:

1
3
79.5
1.5
88
3
100
3

Vaia 1 ta fail er jonno course hobe courses na… :grin:

I am getting runtime error on Case no 21 , What might be problem in my code bellow?

def gpacalc(marks,credit):

gpc=0

if marks>=80 and marks<=100:

    gp=4.00

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=75 and marks<80:

    gp=3.75

    gpc=float(gpc)

    gpc=gp*credit

    return gpc



if marks>=70 and marks<75:

    gp=3.50

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=65 and marks<70:

    gp=3.25

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=60 and marks<65:

    gp=3.00

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=55 and marks<60:

    gp=2.75

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=50 and marks<55:

    gp=2.50

    gpc=float(gpc)

    gpc=gp*credit

    return gpc



if marks>=45 and marks<55:

    gp=2.25

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

if marks>=40 and marks<45:

    gp=2.00

    gpc=float(gpc)

    gpc=gp*credit

    return gpc

t=int(input())

for i in range(t):

total_credit=0

count=0

sumation=0

gpc=0

course=int(input())

for j in range(1,course+1):

    marks,credit=map(float,input().split())

    total_credit+=credit

    if marks>=40 and marks<=100:

        sumation=(sumation+gpacalc(marks,credit))

        

        gpa=sumation/total_credit

    

    else:

        count+=1

if count==0:

    print("Case {0}: {1:.2f}".format(i+1,gpa))

elif count==1:

    print("Case {}: Sorry, you have failed in 1 course!".format(i+1))

else:

    print("Case {0}: Sorry, you have failed in {1} courses!".format(i+1,count))

import java.text.DecimalFormat;
import java.util.Scanner;

/**
*

  • @author Md Rifat
    /
    public class CGPAthGoldenEgg {
    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int T=sc.nextInt();
    float m,n;
    for (int i =1; i <=T; i++) {
    int C=sc.nextInt();
    float sum=0;
    float cradet=0;
    int course=0,fail=0;
    for (int j = 0; j <C; j++) {
    m=sc.nextFloat();
    n=sc.nextFloat();
    course++;
    if((m>=0)&&(m<=100)&&(n>=1)){
    if((m>=80)&&(m<=100)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(4.00)
    (float)(n);
    }else if((m>=75)&&(m<80)){
    cradet=(float)(cradet+n);
    sum=(float) sum+(float)(3.75)(float)(n);
    }else if((m>=70)&&(m<75)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(3.50)
    (float)(n);
    }else if((m>=65)&&(m<70)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(3.25)(float)(n);
    }else if((m>=60)&&(m<65)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(3.00)
    (float)(n);
    }else if((m>=55)&&(m<60)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(2.75)(float)(n);
    }else if((m>=50)&&(m<55)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(2.50)
    (float)(n);
    }else if((m>=45)&&(m<50)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(2.25)(float)(n);
    }else if((m>=40)&&(m<45)){
    cradet=(float)(cradet+n);
    sum=(float)sum+(float)(2)
    (float)(n);
    }else{
    fail++;
    }
    }else{
    System.exit(0);
    }
    }
    if(fail>0){
    if(fail==1){
    System.out.println(“Case “+i+”: Sorry, you have failed in 1 course!”);
    }else{
    System.out.println(“Case “+i+”: Sorry, you have failed in “+fail+” courses!”);
    }
    }else{

             float result=sum/cradet;
             System.out.println("Case "+i+": "+new DecimalFormat("0.00").format(result));
            
         }
        
     }
    

    }
    }
    whats the problem :nauseated_face:

The problem states what to print if you failed any courses. However, it doesn’t state that “course” needs to be pluralized (“course” if 1 fail, “courses” if > 1). Please update the problem statement.