int
(クラスの) andchar
の配列を降順でソートしようとしています。生徒の名前と成績です。
クラスは次のように定義されます。
class Student {
public:
char name[20];
int grades;
};
numCount
レコード数の増分値です。
void bubble_sort(Student theResults[], int numCount)
{
bool swapped = true;
while(swapped)
{
swapped = false;
for(int i=1;i<numCount;i++)
{
if(theResults[i-1].grades < theResults[i].grades)
{
int tempHold = theResults[i-1].grades;
theResults[i-1].grades = theResults[i].grades;
theResults[i].grades = tempHold;
swapped = true;
}
}
}
私が抱えている問題はint
、ループの後に値 (成績) が正しくソートされているが、成績と一致するように名前を正しく割り当てるのが難しいことです。
次のコードを使用しましたが、学生の成績が正しく表示されないため機能しません。
char* title_temp = theResults[i-1].name;
theResults[i-1].name[20] = theResults[i].name[20];
theResults[i].name[20] = title_temp[20];