Sorting array of objects .... please help

Joined
Apr 25, 2011
Messages
1
Reaction score
0
Please someone help me with this program :adore:

Program to create a class Student having data members id,name, marks in 3 subjects and average.
Create an array of objects(say 4 or 5 student entries) and sort the objects based on the average field in ascending order.

Please help me.
I have the following code.



#include<iostream.h>
#include<conio.h>
int i,n;
class student
{
public:
int id;
char name[30];
int test[3];
int avg;

void getdata()
{
cout<<"\nEnter the student id : ";
cin>>id;
cout<<"\nStudent name : ";
cin>>name;
cout<<"\nEnter the mark in 3 subjects : ";
cin>>test[0]>>test[1]>>test[2];
avg=(test[0]+test[1]+test[2])/3;
}
void display()
{
cout<<"\n\n\tStudent ID : "<<id;
cout<<"\n\tNAME : "<<name;
cout<<"\n\tMarks in 3 Subjects : ";
cout<<test[0]<<" "<<test[1]<<" "<<test[2];
cout<<"\n\tAverage : "<<avg;
cout<<"\n\t----";

}


};
void main()
{

student s[10],d[10];
int t=0;
clrscr();
cout<<"\nNumber of student records : ";
cin>>n;
cout<<"\n\n * - Input section - * \n";
for(i=0;i<n;i++)
{
s.getdata();
}
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(s.avg<s[j].avg)
{
d[t]=s;
t++;
}
}
}
cout<<"\n\n* * * - - - - Display section - - - - * * * ";
for(t=0;t<n;i++)
{
d[t].display();
}
cout<<"\n\n* * * - - - - - - - - - - - - - - - - * * * ";
getch();
}


this program executes but it doesnt sort....
i hope only few changes can make it to work.
Please edit it and help me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,900
Latest member
Nell636132

Latest Threads

Top