Funkcja sortuje tablice w taki sposób, aby studenci z wiekiem <18 byli po lewej stronie tablicy, a >=18 po prawej stronie tablicy. Program "wywraca się" przy sortowaniu (funkcja sortStudnetsAge), nie bardzo rozumiem dlaczego, mogę liczyć na Waszą pomoc ? :)
#include <iostream>
using namespace std;
struct Students
{
int id;
char sex;
int age;
};
void sortStudnetsAge(Students *student,int students)
{
int i=0;
int a=students-1;
while(i<a)
{
if(student[i].age<18)
{
if(student[a].age>=18)
{
Students tmp = student[i];
student[i]=student[a];
student[a]=tmp;
i++;
}
else
{
a--;
}
}
else
{
i++;
}
}
}
void createStudents(Students *student, int students)
{
for(int i=0; i<students; i++)
{
// cout<<"Podaj Id studenta"<<endl;
// cin >> student[i].id;
cout<<"Podaj wiek studenta"<<endl;
cin >> student[i].age;
// cout<<"Podaj plec studenta (K lub M)"<<endl;
// cin >> student[i].sex;
}
}
void showStudents(Students *student, int students)
{
for(int i=0; i<students; i++)
{
// cout<<i+1<<"- Id "<<student[i].id;
cout<<" | wiek "<<student[i].age;
// cout<<" | plec "<<student[i].sex<<endl;
}
}
int main()
{
cout << "Podaj liczbe studentow"<<endl;
int students;
cin >> students;
Students *student = new Students;
createStudents(student,students);
showStudents(student,students);
sortStudnetsAge(student,students);
cout << endl;
cout << "----------------------------------------------------"<<endl;
cout << endl;
showStudents(student,students);
return 0;
}