You are here:

C++/Function & Average

Advertisement


Question
I want to write a program that determine a student’s grade. It reads three test scores (between 0-100) and calls a function the calculate and return a student’s grade based on following rules:
(a)   If the average score is 90% and more, the grade is ‘A’
(b)   If the average scores 70% or more & less than 90%, it checks the third score. If the third score is more than 90%, the grades is ‘A’ otherwise than grade is ‘B’
(c)   If the average score is 50%, it checks the average of second & third scores. If the average of them is greater than 70%, the grade is ‘C’, otherwise it is ‘D’.
(d)   If the average score is less than 50%, then the grade is ‘F’

The program’s main() contains only call statements. At least 3 subfunctions are required:
1.   One function to read score
2.   One to determine the grade
3.   One to print the result


Answer
Hai Dear Mazlin !

 Thanks for your question , Here is the code i have written as per the needs !!


 #include<iostream.h>

int scores[3];

void ReadMarks()
{
  for( int i = 0 ; i < 3 ; i++){
     cout<<"Enter Mark :";
     cin>>scores[i];
  }

}

int CalcResult()
{
  int total = 0;
  float average = 0.0;

  for( int i = 0 ; i < 3 ; i++)
     total += scores[i];
  
  average =(float) total/3.0;

  if( average >= 90.0)
     return 1;
  else if( average >= 70.0 && average <= 89.9)
  {
     if( scores[3] >= 90)
        return 1;
     else
        return 2;
  }
  else if ( average >= 50.0 && average <= 69.9)
  {
     if( ((scores[2] + scores[3] )/2.0) >= 70.0)
        return 3;
     else
        return 4;
  }
  else if( average <= 49.9)
     return 5;
}

void PrintResult()
{
  int grade = CalcResult();

  switch(grade)
  {
  case 1:
     cout<<"Grade A";
     break;
  case 2:
     cout<<"Grade B";
     break;
  case 3:
     cout<<"Grade C";
     break;
  case 4:
     cout<<"Grade D";
     break;
  case 5:
     cout<<"Grade F";
     break;
  }

}

void main()
{
  ReadMarks();
  PrintResult();
}


Note : Its working fine , bur i didn't test this program ,i kindly request you to do the same  and revert me in case of issues !

Thanks and Regards !
Prince M. Premnath

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2016 About.com. All rights reserved.