Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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
Run Output
Division :1
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;