Finding student division using a function – CSKC
Sign In

Search

Solved Problem #CPP#5032    siteicon   siteicon  

Problem Statement - Finding student division using a function

Write a function named DIVISION to pass maximum marks and marks obtained by a student. Return division as integer value 1, 2 3 or 0 as follows

=60 % as first division – return 1
>=45 <60 % as Second Division – return 2
>=33 <45 % as Third Division – return 3
<33 as Fail – return 0

Show implementation of above function in main routine with some fixed values.

Solution

TC++ #5032

 

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

int DIVISION(float MM,float MO) {
float MP=MO/MM*100; //Calculating percentage marks.

Below we follow the successive if else ladder check with lowest check first so that we do not need to check the range.
if(MP<33) return 0; //Last condition of fail is checked first
else if(MP<45) return 3; //Check for third division
else if(MP<60) return 2; //Check for second division
else return 1; //check for first division
}

int main() { cout<<“Division :”<<DIVISION(500,350)<<endl; return 0 ;}
Here we just demonstrate the use of function with fixed value. with above fixed values Division: 1 will be displayed.

Notes

  • Some student prefer to write ranges exactly as given like
    if(mp>=60) return 1;
    else if (mp>=45 && mp<60) return 2;
    else if(mp>=33 && mp<45) return 4;
    else return 0;

    This is okay as far as functionality goes, but it is little less efficient. If you have difficulty in making successive check logic then above variation if okay for you.

Common Errors

  • Sometimes students put the = (equal to sign)  at incorrect places and in that case ambiguity may result. For e.g.
    if(mp>=60) return 1;
    else if (mp>=45 && mp<=60) return 2; //in mp<=60, equal to is already covered in previous case mp>=60.
    else if(mp>=33 && mp<=45) return 3; //in mp<=45 equal to is already covered in previous case mp>=45.
    else return 0;


Share

Created: 25-Nov-2018 01:16 | Updated: 29-Nov-2018 17:18|