Answer to Question #186274 in C++ for RAHUL SINGH

Question #186274

Grade ‘A’ for> 80% marks, ‘B’ for 70 to 80%, ‘C’ for 60 to 70%, D for 50 to 60%, F for <50%.

Base on above information, do the following:

(i) Write a function which prints the name and grades of all students in the ascending order of the

grades. In case of same grade, print all respective names in alphabetical order.

(ii) Write a function which prints the name and grades of all students in the alphabetical order of

the name of the students.

(iii) Write a function to search the grade of a student based on the first name of the student. In

case of multiple entries with same name, print all the names with roll number and respective

grades.

Here, consider Student as a base class whose derived classes are T1T2T3, P1P2, and Attendance.

Further, consider the class Total which inherits the classes T1T2T3, P1P2, and Attendance as

multiple inheritance. Classes Faculty and Administration are independent classes


1
Expert's answer
2021-04-27T14:14:31-0400
#include <bits/stdc++.h> 
#include<iostream>
#include <stdio.h>
using namespace std;




map<string, vector<pair<long int, char>>>m;


class faculty{
	public:
		string fname;	
};


class administration{
	public:
		string department;
};




class student{
	
	public:
		string name;
		int marks;
		char grade;
		long int rollno;
		
		void getGrade(int m)
		{
			if(m>80)
			{
				grade = 'A';
			}
			else if(m>70 && m<=80)
			{
				grade = 'B';
			}
			else if(m>60 && m<=70)
			{
				grade = 'C';
			}
			else if(m>50 && m<=60)
			{
				grade = 'D';
			}
			else{
				grade ='E';
			}
			
		}
		
		void input()
		{
			cout<<"Enter name of student : "<<endl;
			cin>>name;
			cout<<"Enter Roll no : "<<endl;
			cin>>rollno;
			cout<<"Enter marks of student : "<<endl;
			cin>>marks;
			getGrade(marks);
			m[name].push_back(make_pair(rollno, grade));
		}
		
		void display()
		{
			cout<<"Name of student : "<<name<<endl;
			cout<<"Roll No : "<<rollno<<endl;
			cout<<"Marks obtained : "<<marks<<endl;
			cout<<"Grade : "<<grade<<endl;
		}
		
		
};


class T1T2T3 : public student{
	
};


class P1P2 : public student{
	
};


class Attendance : public student{


};


class Total : public T1T2T3, public P1P2, public Attendance{
	
};


bool compareGrade(student a, student b)
{
	if(a.grade == b.grade)
	{
		return a.name < b.name;
	}
	return a.grade < b.grade;
}


bool compareName(student a, student b)
{
	return a.name < b.name;
}


void searchGrade(string nm)
		{
			bool ans = false;
			for(auto it = m.begin(); it!= m.end(); it++)
			{
				if(it->first == nm)
				{
					ans = true;
					for(int i=0; i < (it->second).size(); i++)
					{
						cout<<"Name of Student : "<<nm<<endl;
						cout<<"Roll No : "<<(it->second)[i].first<<endl;
						cout<<"Grade : "<<(it->second)[i].second<<endl;
						cout<<endl;
					}
				}
			}
			
			if(ans == false)
			{
				cout<<"This name is not found"<<endl;
			}
		}
		
		
int main()
{
	cout<<"Enter no. of students : "<<endl;
	int n;
	cin>>n;
	student s[n];
	
	for(int i=0; i<n; i++)
	{
		s[i].input();
	}
	
	sort(s, s+n, compareGrade);
	sort(s, s+n, compareName);
	


	cout<<endl<<endl;
	cout<<"Enter the name you want to see grade : "<<endl;
	string nm;
	cin>>nm;
	cout<<"Details of asked name : "<<endl;
	cout<<"--------------------------"<<endl;
	searchGrade(nm);
	
	cout<<endl<<endl;
	cout<<"Displaying Student info"<<endl;
	cout<<"----------------------------"<<endl;
	for(int i=0; i<n; i++)
	{
		s[i].display();
		cout<<endl;
	}
}










Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS