Code Competitions

Coding competition

Thursday, 30 May 2013

program to sort student record to assign position

#include<iostream.h>
#include<conio.h>
struct student
{
    char *name;
    int sub1;
    int sub2;
    float avg;

};
void main()
{
    struct student *x,*p,*u;
    clrscr();
    int i,n,j;
    x=p;
    cout<<"\nenter the number of students ";
    cin>>n;
    for(i=0;i<n;i++)
    {
        cout<<"\nenter name ";
        cin>>(x+i)->name;
        cout<<"\nsub1 ";
        cin>>(*(x+i)).sub1;
        cout<<"\nsub2 ";
        cin>>(*(x+i)).sub2;
        (*(x+i)).avg=(float) ((*(x+i)).sub1+(*(x+i)).sub2)/2;
    }
    for(i=0;i<n;i++)
    {
        cout<<"\n name "<<(x+i)->name<<" sub1 "<<x[i].sub1<<" sub2 "<<x[i].sub2<<" avg "<<x[i].avg;
    }
    cout<<"\nthe sorted list is: ";
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-i;j++)
        {
            if(((*(x+j)).avg)<(*(x+j+1)).avg)
            {
                *u=*(x+j);
                *(x+j)=*(x+j+1);
                *(x+j+1)=*u;
            }
        }
    }
    for(i=0;i<n;i++)
    {
        cout<<"\n name "<<(x+i)->name<<" sub1 "<<x[i].sub1<<" sub2 "<<x[i].sub2<<" avg "<<x[i].avg;
    }
    getch();
}

No comments:

Post a Comment