30) Write a program in C++ to input roll no and total marks of some students in an array of object arrange and print both roll no and total marks of those student in descending order on total marks?

#include<iostream.h>
#include<conio.h>
class student
{
private:
                int *rn,*tm,sz;
public:
                void stu(int size)
                {
                                sz=size;
                                rn=new int[size];
                                tm=new int[size];
                }
                void setdata();
                void sort();
                void display();
};
void student::setdata()
{
                int i=0;
                for(i=0;i<sz;i++)
                                {
                                cout<<"Enter Roll Number:- ";
                                cin>>rn[i];
                                cout<<"Enter Total Marks:- ";
                                cin>>tm[i];
                                }
}
void student::sort()
{
                int i,j,temp;
                for(i=0;i<sz-1;i++)
                                for(j=i+1;j<sz;j++)
                                                if(tm[i]<tm[j])
                                                                {
                                                                temp=rn[i];
                                                                rn[i]=rn[j];
                                                                rn[j]=temp;
                                                                temp=tm[i];
                                                                tm[i]=tm[j];
                                                                tm[j]=temp;
                                                                }
}
void student::display()
{
                for(int i=0;i<sz;i++)
                                {
                                cout<<"Roll No:- "<<rn[i]<<endl;
                                cout<<"Total Marks:- "<<tm[i]<<endl;
                                }
}
void main()
{
                int x;
                student s;
                clrscr();
                cout<<"Enter Size of Array:- ";
                cin>>x;
                s.stu(x);
                cout<<endl;
                s.setdata();
                s.sort();
                cout<<endl;
                s.display();
                getch();
}

 

Advertisements

Comments are closed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: