Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sorting array of objects .... please help

Reply
Thread Tools

Sorting array of objects .... please help

 
 
arjunkg673 arjunkg673 is offline
Junior Member
Join Date: Apr 2011
Posts: 2
 
      04-25-2011
Please someone help me with this program

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[i].getdata();
}
for(i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(s[i].avg<s[j].avg)
{
d[t]=s[i];
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting an array list containing objects? Rob Meade ASP .Net 4 11-04-2012 04:29 AM
Re: Array objects get changed when sorting the array Roedy Green Java 1 06-25-2009 08:25 PM
Re: Array objects get changed when sorting the array markspace Java 1 06-25-2009 06:22 PM
class objects, method objects, function objects 7stud Python 11 03-20-2007 06:05 PM
Sorting an Array of String Objects TheVooDooChild Java 12 03-25-2005 01:05 AM



Advertisments