2013-11-15 3 views
0

선택 정렬에 문제가 있습니다. 알파벳순으로 학생 이름을 정렬하려고합니다. 컴파일하고 VS에서 전체 오류를 보여줍니다.구조체 배열을 사용하여 선택 정렬

나는 내 디스플레이 모든 학생들 기능을 수행 할 수 있다고 생각 해달라고, 나는 그것을 감사 SortByName 및 SortByScoreFunctions

도움과 더 있다고 생각 감사합니다!

여기 내 프로그램의 구조체입니다.

struct StudentType 
{ 
    string studentName; 
    int testScore; 
    char grade; 
}; 


    void SortStudentsByName(StudentType student[], int numStudents) 
    { 
     int startScan, minIndex, FirstInAlphabet; 
     for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++) 
     { 
      minIndex = startScan; 
      FirstInAlphabet = student[0]; 
      for(int index = startScan+1; index < NUM_STUDENTS; index++) 
      { 
       if (student[index] > FirstInAlphabet) 
       { 
        FirstInAlphabet = student[index]; 
        minIndex = index; 
       } 
      } 
     } 
    } 



     void SortStudentsByScore(StudentType student[], int numStudents) 
{ 
    int startScan, 
     minIndex, 
     lowest; 
    for (startScan = 0; startScan < (NUM_STUDENTS-1); startScan++) 
    { 
     minIndex = startScan; 
     lowest = student[0].testScore; 
     for (int index = startScan+1; index < NUM_STUDENTS; index++) 
     { 
      if(student[index].testScore < lowest) 
      { 
       lowest = student[index].testScore; 
       minIndex = index; 
      } 
     } 
     student[minIndex].testScore = student[startScan].testScore; 
     student[startScan].testScore = lowest; 
     cout <<"List of Students sorted by Score from Highest to Lowest" << endl; 
     DisplayAllStudents(student, numStudents); 
    } 
} 



void DisplayAllStudents(const StudentType student[], int numStudents) 
{ 
    cout << endl; 
    FormatNameScoreGrade(cout); 
    for(int i = 0; i < numStudents; i++) 
    { 
      cout << setw(20) << student[i].studentName << setw(10) << student[i].testScore << setw(10) << student[i].grade << endl; 
    } 
    cout << endl; 
    EndOfList(cout); 
} 

내가 여기에 컴파일

내가

이 이름으로 정렬 내 출력 결과입니다

를받는 출력

Fibonacci, Leonardo  63   D 
    Huffman, David  79   C 
     Augusta, Ada  91   A 
Goldbach, Christian  81   B 
     Venn, John  100   A 
    Church, Alonzo  72   C 
    Fermat, Pierre  84   B 
    Kruskal, Joseph  66   D 
     Cantor, Georg  67   D 
     Turing, Alan  85   B 
    Chebysheva, PL  100   A 
DeMorgan, Augustus  79   C 
    Karnaugh, Maurice  72   C 
    Babbage, Charles  98   A 
     Hooper, Grace  95   A 

는 여기

작업이 출력하지 내 고등 학년의 경우

Student Name   Test Score  Grade 
------------------------------------------ 
        -858993460   D 
    Huffman, David-858993460   C 
     Augusta, Ada-858993460   A 
Goldbach, Christian-858993460   B 
     Venn, John-858993460   A 
    Church, Alonzo-858993460   C 
    Fermat, Pierre-858993460   B 
    Kruskal, Joseph-858993460   D 
     Cantor, Georg-858993460   D 
     Turing, Alan-858993460   B 
    Chebysheva, PL-858993460   A 
DeMorgan, Augustus-858993460   C 
    Karnaugh, Maurice-858993460   C 
    Babbage, Charles-858993460   A 
     Hooper, Grace-858993460   A 
+0

오류를 정렬하고 첫 번째 오류를 표시하십시오. –

+0

'sort'가 엔트리를 전혀 바꾸지 않습니다. 점수 별 정렬은 점수를 움직이는 것처럼 보이지만 이름이나 성적은 아닙니다. –

+0

용의자 인'SortStudentsByX' 함수를 디버깅하기 위해 수행 한 작업은 무엇입니까? * 여기에 게시하는 것 외에 * 여기에 게시 하시겠습니까? – WhozCraig

답변

0

이렇게하려면 std :: string 클래스의 compare를 사용해야합니다. 비교보기에 대한 정보는 here입니다. 또 다른 문제는 학생을 스왑하지 않는 것입니다. Selection Sort에 대한 위키를 방문하십시오.

이것은 내 문제를 해결하기위한 것입니다. 나는 그것이 작동 할 것이라는 희망을 시험하지 않았다.

void SortStudentsByName(StudentType student[], int numStudents) 
{ 
    for(int i = 0;i < numStudents - 1; ++i) 
    { 
     int min = i; 
     for(int j = i + 1;j < numStudents; ++j) 
     { 
      if(student[i].studentName.compare(student[j].studentName) < 0) 
      { 
       min = j; 
      } 
     } 
     if(min != i) 
     { 
      StudentType temp = student[i]; 
      student[i]  = student[min]; 
      student[min]  = temp; 
     } 
    } 
} 
+0

덕분에 완벽하게 작동했지만 한 가지 질문이있었습니다. 그 선택 정렬인가? 왜냐하면 나에게 선택 정렬과 버블 정렬은 매우 비슷해 보입니다. –

+0

예! 선택 정렬입니다. 그것들은 같은 복잡성 O (n)을 가지고 있지만 구현이 다릅니다. 이 알고리즘은 위키 백과에서 아주 잘 설명되어 있습니다. [selection.gif] (http://en.wikipedia.org/wiki/File:Selection-Sort-Animation.gif) 및 [buble.gif] (http : //en.wikipedia)를 보면서 그 차이를 쉽게 이해할 수 있습니다. .org/wiki/File : Bubble-sort-example-300px.gif). –

+0

난 정말 고마워, 정말 고마워! 아무도 이걸 도와주고 싶지 않았어. –

0

코드에서 부등호를 변경하면 나에게이 코드가 전송됩니다.

void SortStudentsByName(StudentType student[], int numStudents) 
    { 

      int startScan, 
      minIndex; 

     for (startScan = 0; startScan < (numStudents-1); startScan++) 
     { 
      minIndex = startScan; 
      for (int index = startScan; index < numStudents; index++) 
      { 
       if(student[index].studentName < student[minIndex].studentName) 
        minIndex = index; 
      } 
      if(minIndex!=startScan) 
      { 
       StudentType temp = student[minIndex]; 
       student[minIndex] = student[startScan]; 
       student[startScan] = temp; 
      } 
     } 

    cout << endl; 
    cout << "List of Students sorted Alphabetically "<< endl; 
    DisplayAllStudents(student, numStudents); 

    } 



    void SortStudentsByScore(StudentType student[], int numStudents) 
    { 

     int startScan, 
      minIndex; 

     for (startScan = 0; startScan < (numStudents-1); startScan++) 
     { 
      minIndex = startScan; 
      for (int index = startScan; index < numStudents; index++) 
      { 
       if(student[index].testScore>student[minIndex].testScore) 
        minIndex = index; 
      } 
      if(minIndex!=startScan) 
      { 
       StudentType temp = student[minIndex]; 
       student[minIndex] = student[startScan]; 
       student[startScan] = temp; 
      } 
     } 

     cout <<"List of Students sorted by Score from Highest to Lowest" << endl; 
     DisplayAllStudents(student, numStudents); 
    }