Cấu trúc struct trong C


#include<iostream>
using namespace std;
//Khai bao cau truc
struct student
{
    int id;
    float GPA;
    string name;    
};
//Ham nhap vao danh sach sinh vien
void input(student st[], int &n)
{
    for(int i=0;i<n;i++)
    {
        cout<<"Sinh vien thu "<<i+1<<":\n";
        cout<<"Nhap id:";
        cin>>st[i].id;
        cout<<"GPA:";
        cin>>st[i].GPA;
        cin.ignore();
        cout<<"Name:";
        getline(cin,st[i].name);
    }
}
void output(student st[], int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<st[i].id<<" "<<st[i].GPA<<" "<<st[i].name<<endl;
    }
}
//Ham sap xep danh sach sinh vien giam dan theo GPA
void sortDESC(student st[], int n)
{
    for(int i=0;i<n-1;i++)
    {
        for(int j=i+1;j<n;j++)
        {
            if(st[i].GPA<st[j].GPA)
            {
                student tmp = st[i];
                st[i]=st[j];
                st[j]=tmp;
            }
        }
    }
}
int main()
{
    //Khai bao mang student
    student st[10];
    int n;
    cout<<"Nhap so sinh vien:";
    cin>>n;
    input(st,n);
    output(st,n);
    sortDESC(st,n);
    cout<<"Sau khi sap xep giam dan theo GPA\n";
    output(st,n);
    return 0;
}

Trương Đình Huy