Answer to Question #344171 in C++ for Nayo

Question #344171




A teacher wants to maintain the attendance for the classroom. After storing the attendance, he wishes to print the details of the top 3 attendance percentage holders. Create a class with the private data members: name (string), roll_no (integer), percent (float). Create a constructor which will initialize name with NULL, roll_no with 0 and percent with 0.0. Public data members: A function get_details() – which will accept all the details from the user, a function show_details() – which will display all the details. Define another function called get_attendance() which will return the attendance of the calling object. Using the get_attendance() function, get the attendance and sort the records in descending order.




Show the details of the top 3 attendance percentage holders by invoking the




show_details() function. Define a destructor which prints “Objects destroyed successfully”.

1
Expert's answer
2022-05-23T16:24:53-0400


#include <iostream>
#include <cstring>
using namespace std;
class Student{
    private:
        string name;
        int roll_no;
        float percent;
    public:
    Student(){
        name = "NULL";
        roll_no = 0;
        percent = 0.0;
    }
    ~Student(){
        cout<<"Objects destroyed successfully"<<endl;
    }
    void get_details(string n, int r, float p){
        name = n;
        roll_no = r;
        percent = p;
    }
    void show_details(){
        cout<<"Name: "<<name<<endl;
        cout<<"roll_no: "<<roll_no<<endl;
        cout<<"Attendance percent: "<<percent<<endl;
    }
    float get_attendance(){
        return percent;
    }


    };
int main()
{
    int n = 10; // amount of Students 
    Student classroom[n];
    
    for(int i = 0; i< n;i++)
        classroom[i].get_details("Kyle", 24+i, 25.6+i); // values for a test. 
    
    float attendance[n];
    
    for(int i = 0; i<n; i++){
        attendance[i] = classroom[i].get_attendance();
    }
    // basic descending sorting. You can make it a function if you want
    for (int i = 0, temp; i < n; ++i){
        for (int j = i + 1; j < n; ++j){
            if (attendance[i] < attendance[j]){
                temp = attendance[i];
                attendance[i] = attendance[j];
                attendance[j] = temp;
            }
        }
    }
    //
    
    cout<<"Attendance leaderboard"<<endl;
    for(int i=0; i< n; i++){
        cout<<attendance[i]<<endl;
    }
    return 0;
}

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