Sorting array of objects .... please help

Discussion in 'C Programming' started by arjunkg673, Apr 25, 2011.

  1. arjunkg673

    arjunkg673

    Joined:
    Apr 25, 2011
    Messages:
    1
    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.
     
    arjunkg673, Apr 25, 2011
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade
    Replies:
    4
    Views:
    30,417
    rahul891
    Nov 4, 2012
  2. TheVooDooChild

    Sorting an Array of String Objects

    TheVooDooChild, Mar 22, 2005, in forum: Java
    Replies:
    12
    Views:
    40,017
    Thomas G. Marshall
    Mar 25, 2005
  3. KK
    Replies:
    2
    Views:
    594
    Big Brian
    Oct 14, 2003
  4. markspace
    Replies:
    1
    Views:
    388
    markspace
    Jun 25, 2009
  5. Roedy Green
    Replies:
    1
    Views:
    445
    Roedy Green
    Jun 25, 2009
Loading...

Share This Page